Free Scan: Update Your PC's Outdated Drivers to Optimize Performance
October 2nd, 2008, 06:39 PM
|
#1 (permalink)
| | Junior Member
Join Date: Mar 2007
Posts: 16
| please help with a c++ program
I have to modify a sequence class that uses overloaded operator to add arrays of different lengths. I was able to use most of the code out of the book, but i can't get the darn thing to compile.
this is the header file. Code: //Aljejandro W. Dukes
//CSC 210 Program 1
//September 25, 2008
//Header file(sequence1.h)
#include <cstdlib>
#include <iostream>
#include <fstream>
// TYPEDEFS and MEMBER CONSTANTS
typedef double value_type;
typedef int size_type;
static const size_type CAPACITY = 30;
using namespace std;
class sequence
{
//Friend functions
friend sequence operator+ (sequence& list1, const sequence list2);
//Precondition: list1.size() + list2.size() <= CAPACITY.
//Postcondition: The array returned is the sum of list1 and list2.
friend sequence operator* (sequence& list1, const sequence list2);
//Precondition: list1.size() + list2.size() <= CAPACITY.
//Postcondition: The array returned is the product of list1 and list2.
friend ifstream &operator >> (ifstream &input, sequence &list);
//Postcondition: The array are read in from the text file.
friend ofstream &operator << (ofstream &output, sequence &outlist);
//Postcondition: The product and sums of list1 and list2 are read out to the
//text file output.
//Public Area
public:
// CONSTRUCTOR
sequence( );
//Postcondition: The sequence has been initialized as an empty sequence.
// Iterators
void start( );
//Postcondition: The first item in the sequence becomes the current item, but if
//the sequence is empty, there is no current item.
void advance( );
//Precondition: is_item returns true.
//Postcondition: If the current item was already the last item in the sequence,
//then there is no longer any current item. Otherwise, the new current item is
//the item immediately after the original current item.
void insert(const value_type entry);
//Precondition: size() < CAPACITY.
//Postcondition: A new copy of entry has been inserted in the sequence.
// CONSTANT MEMBER FUNCTIONS
int size( ) const;
//Postcondition: The return value is the number of items in the sequence.
bool is_item( ) const;
//Postcondition: A true return value indicates that there is a valid "current"
//that may be retrieved by the current member function. A false return value
//indicates that there is no valid current item.
value_type current( ) const;
//Precondition: is_item is true.
//Postcondition: The item returned is the current item in the sequence.
//Private members
private:
value_type data[CAPACITY];
size_type used;
}; Implementation: Code: //Aljejandro W. Dukes
//CSC 210 Program 1
//Documentation file sequence1.cpp
#include <cassert>
#include <iostream>
#include "sequence1.h"
using namespace std;
{
sequence::sequence()
{
used = 0;
}
void sequence::start()
{
assert (size() > 0);
current_index = 0;
}
void sequence::advance()
{
assert (is_item());
++current_index;
}
void sequence::insert(const value_type& entry)
{
assert (size() < CAPACITY);
data[used] = entry;
++used;
}
sequence::int sequence::size() const
{
return used;
}
bool sequence::is_item() const
{
return (used > 0 && current_index != used);
}
sequence::value_type sequence::current()const
{
assert (is_item());
return (data[current_index]);
}
sequence operator+ (const sequence& list1, const sequence& list2)
{
sequence list3;
int i;
i = 0;
while (i < list1.size()) && (i < list2.size())
{
list3[i] = list1[i] + list2[i];
i++;
}
while (list1.size() < list2.size())
{
list3[i] = list2[i];
i++;
list3.size() = list2.size();
}
while (list1.size() > list2.size())
{
list3[i] = list1[i];
i++;
list3.used() = list1.used();
}
return list3;
}
sequence operator* (const sequence list1, const sequence list2)
{
sequence list4;
int i;
i = 0;
while (i < list1.size()) && (i < list2.size())
{
list3[i] = list1[i] * list2[i];
i++;
}
while (list1.size() < list2.size())
{
list4[i] = list2[i];
i++;
list4.used() = list2.used();
}
while (list1.size() > list2.size())
{
list4[i] = list1[i];
i++;
list4.used() = list1.used();
}
return list4;
}
ifstream &operator >> (ifstream& input, const sequence& list)
{
for (int i=0; i < CAPACITY; i++)
input >> list[i];
return list;
}
ofstream &operator << (ofstream& output, const sequence& outlist)
{
for (int i=0; i < CAPACITY; i++)
output << outlist;
return outlist;
}
} actual program that uses the class. I can't even get past the part where i need it to open the text files. I am pretty sure that the class is correct, but i don't understand the compiler errors. Code: // Aljejandro Dukes
// CSC 210 Program 1
//September 25, 2008
#include "sequence1.h"
#include <iostream>
#include <fstream>
#include <cassert>
#include <cstdlib>
using namespace std;
int main(void)
{
ifstream input;
ofstream output;
//open data file
input.open("input.txt");
//if not open give error message
if (!input)
{
cout << "Did not open input.txt" <<endl;
return 1;
}
//open output file
output.open("output.txt");
//if not give error message
if (!output)
{
cout << "Did not open output.txt" << endl;
return 1;
}
input.close();
output.close();
system ("PAUSE");
return 0;
} Any help would be greatly appreciated. |
| |
October 2nd, 2008, 06:53 PM
|
#2 (permalink)
| | Junior Member
Join Date: Mar 2007
Posts: 16
|
This is the errors that the compiler are calling out.
Code:
11 E:\addarray.cpp In file included from E:\addarray.cpp
9 E:\sequence1.cpp expected nested-name-specifier before "namespace"
9 E:\sequence1.cpp expected unqualified-id before "namespace"
9 E:\sequence1.cpp expected `;' before "namespace"
9 E:\sequence1.cpp expected unqualified-id before "namespace"
10 E:\sequence1.cpp expected unqualified-id before '{' token
39 E:\addarray.cpp expected unqualified-id at end of input
39 E:\addarray.cpp expected `,' or `;' at end of input |
| |
October 3rd, 2008, 03:35 PM
|
#3 (permalink)
| | Real gangstas sip on Yacc
Join Date: Oct 2001 Location: Suckas-ville
Posts: 4,540
|
What compiler are you using?
__________________
Signatures blow hard
If your signature contains an ad of any kind, congratulations, you're on my ignore list.
|
| |
October 3rd, 2008, 05:06 PM
|
#4 (permalink)
| | Junior Member
Join Date: Mar 2007
Posts: 16
| |
| |
October 5th, 2008, 10:08 AM
|
#5 (permalink)
| | Ultimate Member
Join Date: Mar 2005 Location: Out of my mind
Posts: 2,742
|
Have you got this to work yet?
What does the input file look like? |
| |
October 5th, 2008, 07:06 PM
|
#6 (permalink)
| | Junior Member
Join Date: Mar 2007
Posts: 16
|
no not yet, still working on it...will post new code in a few |
| |
October 6th, 2008, 12:55 AM
|
#7 (permalink)
| | Junior Member
Join Date: Mar 2007
Posts: 16
| need help with overloaded operators in c++
Okay, I worked on it for the last two days and I need to use the class sumclass and add two arrays of different length together. I start completely over and this is what I got.
sumclass.h: Code: // Aljejandro Dukes
// CSC 210 Program 1
// September 25 2008
//Sumclass Header File "sumclass.h"
#include <iostream>
#include <fstream>
#include <cstdlib>
//TYPEDEF and MEMBER CONSTANTS
const int CAPACITY = 30;
typedef float value_type;
using namespace std;
class sumclass
{
//Friend Functions
friend sumclass operator+ (const sumclass& list1, const sumclass& list2);
//Precondition: list1.size() + list2.size() <= CAPACITY.
//Postcondition: The array returned is the sum of list1 and list2.
friend sumclass operator* (const sumclass& list1, const sumclass& list2);
//Precondition: list1.size() + list2.size() <= CAPACITY.
//Postcondition: The array returned is the product of list1 and list2.
friend ifstream& operator >> (ifstream& input, sumclass& list);
//Postcondition: The array are read in from the text file.
friend ofstream& operator << (ofstream& output, sumclass& outlist);
//Postcondition: The product and sums of list1 and list2 are read out to the
//text file output.
//PUBLIC AREA
public:
//CONSTRUCTORS
sumclass();
//Postcondition: The sequence has been initialized as an empty sequence.
//ITERATORS
void insert(const value_type& entry);
//Precondition: is_item is true.
//Postcondition: The item returned is the current item in the sequence.
//CONSTANT MEMBER FUNCTIONS
int size( ) const;
//Postcondition: The return value is the number of items in the sequence.
bool is_item( ) const;
//Postcondition: A true return value indicates that there is a valid "current"
//that may be retrieved by the current member function. A false return value
//indicates that there is no valid current item.
value_type current( ) const;
//Precondition: is_item is true.
//Postcondition: The item returned is the current item in the sequence.
//PRIVATE MEMBERS
private:
value_type data[CAPACITY];
int used;
int current_index;
}; sumclass.cpp: Code: //Aljejandro W. Dukes
//CSC 210 Program 1
//Documentation File "sumclass.cpp"
#include <cassert>
#include <iostream>
#include <fstream>
#include "sumclass.h"
sumclass::sumclass()
{
used=0;
}
bool sumclass::is_item() const
{
return (used > 0 && current_index != used);
}
int sumclass::size() const
{
return used;
}
void sumclass::insert(const value_type& entry)
{
assert(size() < CAPACITY);
data[used]=entry;
++used;
}
value_type sumclass::current() const
{
assert (is_item());
return (data[current_index]);
}
ifstream &operator >> (ifstream& input, sumclass& list)
{
input >> list;
return input;
}
ofstream & operator << (ofstream& output, sumclass& outlist)
{
output << outlist;
return output;
}
sumclass operator+ (const sumclass& list1, const sumclass& list2)
{
sumclass list3;
int i;
i = 0;
while (i < list1.size() && i < list2.size())
{
list3.data[i] = list1.data[i] + list2.data[i];
i++;
}
while (list1.size() < list2.size())
{
list3.data[i] = list2.data[i];
i++;
list3.used = list2.used;
}
while (list1.size() > list2.size())
{
list3.data[i] = list1.data[i];
i++;
list3.used = list1.used;
}
return list3;
}
sumclass operator* (const sumclass& list1, const sumclass& list2)
{
sumclass list4;
int i;
i = 0;
while (i < list1.size() && i < list2.size())
{
list4.data[i] = list1.data[i] * list2.data[i];
i++;
}
while (list1.size() < list2.size())
{
list4.data[i] = list2.data[i];
i++;
list4.used = list2.used;
}
while (list1.size() > list2.size())
{
list4.data[i] = list1.data[i];
i++;
list4.used = list1.used;
}
return list4;
} addarray.cpp: Code: //Aljejandro Dukes
//CSC 210 Program 1
// Addarray file "addarray.cpp"
//Program is to use overloaded + and * to add and multiply two arrays of
//different lengths.
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
#include "sumclass.cpp"
int main()
{
ifstream infile1, infile2; //input files
ofstream outfile; //output file
sumclass list1, list2, list3, list4;
infile1.open ("input1.txt");
if (!infile1)
{
cout << "Failure to open input1.txt" << endl;
return 1;
}
infile2.open ("input2.txt");
if (!infile2)
{
cout << "Failure to open input2.txt" << endl;
return 1;
}
outfile.open ("outlist.txt");
if (!outfile)
{
cout << "Failure to open outlist.txt" << endl;
return 1;
}
infile1 >> list1;
infile2 >> list2;
outfile << list1 << " ";
outfile << endl;
outfile << list2 << " ";
outfile << endl;
cout << "THE END" << endl;
system ("PAUSE");
infile1.close();
infile2.close();
outfile.close();
return 0;
} input1 and input2: Code: input1: 21.8 66.3 57.9 91.3 46.7 76.3 31.4 14.7
input2: 14.2 87.3 23.7 18.7 97.3 65.5 43.2 9.7 8.3 91.4 19.7 Now it compiles, but it won't create any output. I am not real good with using overloaded operators, and could really use the help.  thanks for taking a look. |
| |
October 7th, 2008, 12:27 PM
|
#8 (permalink)
| | Ultimate Member
Join Date: Mar 2005 Location: Out of my mind
Posts: 2,742
|
You need to use the sumclass INSERT method to load the arrays from the file.
while not end of file input1
list1.insert(data from file)
end while
while not end of file input2
list2.insert(data from file)
end while
that should give you a input1.data[] and an input2.data[]. Oh, but you can't access data! It's private! Good
now, if your overloaded operators are correct: cout << "list 1 plus list 2 = " << list1 + list2; |
| |
October 8th, 2008, 12:06 AM
|
#9 (permalink)
| | Junior Member
Join Date: Mar 2007
Posts: 16
|
i used your suggestion and got it to work. Thanks for your help. i'm not sure how to close the thread, but it is closed and again thanks for your help. Oh yeah, the thread is closed. |
| | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | |
Posting Rules
| You may post new threads You may post replies You may not post attachments You may not edit your posts HTML code is Off | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |