home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Join TechIMO for Free!
Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read
Reply Get bargains at  »  Dealighted.com
 
Thread Tools
Currently Active Users: 2684
Discussions: 186,606, Posts: 2,227,046, Members: 230,241
Free Scan: Update Your PC's Outdated Drivers to Optimize Performance
Old October 2nd, 2008, 06:39 PM   Digg it!   #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.

firecookie2k is offline   Reply With Quote
TechIMO.com Ads - Login or register for less ads.
How many errors does your computer have?

You no longer need to guess! This free stability scan and registry cleaner download will give you a complete diagnosis of your Windows registry, identifying errors and conflicts.

FREE instant scan


Guest, Register Free! to remove this ad and get your tech support questions answered in minutes!
Old 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

firecookie2k is offline   Reply With Quote
Old October 3rd, 2008, 03:35 PM     #3 (permalink)
Real gangstas sip on Yacc
 
jkrohn's Avatar
 
Join Date: Oct 2001
Location: Suckas-ville
Posts: 4,540
Send a message via ICQ to jkrohn Send a message via AIM to jkrohn Send a message via Yahoo to jkrohn
What compiler are you using?
__________________
Signatures blow hard
If your signature contains an ad of any kind, congratulations, you're on my ignore list.

jkrohn is online now   Reply With Quote
Old October 3rd, 2008, 05:06 PM     #4 (permalink)
Junior Member
 
Join Date: Mar 2007
Posts: 16
bloodshed's DEV-C++
firecookie2k is offline   Reply With Quote
Old October 5th, 2008, 10:08 AM     #5 (permalink)
Ultimate Member
 
Rootstonian's Avatar
 
Join Date: Mar 2005
Location: Out of my mind
Posts: 2,742
Send a message via AIM to Rootstonian
Have you got this to work yet?

What does the input file look like?
Rootstonian is online now   Reply With Quote
Old 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
firecookie2k is offline   Reply With Quote
Old 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.
firecookie2k is offline   Reply With Quote
Old October 7th, 2008, 12:27 PM     #8 (permalink)
Ultimate Member
 
Rootstonian's Avatar
 
Join Date: Mar 2005
Location: Out of my mind
Posts: 2,742
Send a message via AIM to Rootstonian
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;
Rootstonian is online now   Reply With Quote
Old October 8th, 2008, 12:06 AM     #9 (permalink)
Junior Member
 
Join Date: Mar 2007
Posts: 16
thanks

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.
firecookie2k is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
I need a program that will tell me about my PC... neonblack77 Motherboards 6 May 29th, 2007 07:53 PM
C Program Taros14 Webmastering and Programming 19 March 14th, 2007 05:27 PM
c# program sr71000 Webmastering and Programming 13 October 9th, 2006 07:19 PM
Another program MadMan2k Webmastering and Programming 0 December 7th, 2004 11:21 AM
What program is this? lost-and-found General Tech Discussion 7 February 20th, 2002 08:39 PM

Most Active Discussions
Is It Just Me? (528)
Misery Loves Company... (1849)
Why Does the MOON Grow Bigger as It.. (18)
heatsink issue (10)
New Mobo (18)
UPGRADING C/D DRIVE TO 250GB & .. (14)
SSD's, RAID, and External Backup (7)
1 internet. 1 house. 3 computer. ho.. (13)
Is This A Compatible Gaming PC? (18)
Recent Discussions
firewall (1)
C++ compiler suggestions (4)
Official World of Warcraft Thre.. (4529)
Programming question (2)
UPGRADING C/D DRIVE TO 250GB &a.. (14)
Ubuntu or Xubuntu....anyone!!? (7)
Building my first PC and need s.. (0)
PNY vs Gainward (3)
Linksys WRT54G (2)
FS: Dell 6000 laptop, modded 36.. (2)
Apple iPod touch 16 GB $200 (4)
Six 28-Disc Cross Design Black .. (4)


All times are GMT -4. The time now is 11:53 AM.
TechIMO Copyright 2008 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28