Average .txt to Array Problem

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2008
Posts: 5
Reputation: aquariusdragon2 is an unknown quantity at this point 
Solved Threads: 0
aquariusdragon2 aquariusdragon2 is offline Offline
Newbie Poster

Average .txt to Array Problem

 
0
  #1
Apr 15th, 2008
I have this coded out but it keeps giving me the error message "ifstream" does not have a type. I don't know what to do, sigh. Any help would get appreciated.

const int MAXSIZE = 100;
double votes[MAXSIZE]; // declaration for array
double vote;
int count = 0;
double total = 0.0;
double average;
ifstream myFile; // declaring input file

myFile.open ( "votes.txt" );
if ( !myFile )
{
cout << "Error opening input file: votes.txt";
system("PAUSE");
exit(1);
}

while ( !( myfile.eof ( ) ) )
{
myFile >> vote >> ws;
total += vote;
votes[count] = vote;
count++;
} // end while

if ( count != 0 )
average = total / count;
else
average = 0.0;

myFile.close ( );
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,675
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Average .txt to Array Problem

 
0
  #2
Apr 15th, 2008
You do have
  1. #include <fstream>
  2. using namespace std;
in your file, right?
Note that you misspell myFile in the while loop.
And your use of the eof( ) method will give erroneous results in the case that the file is empty.
On the other hand, your use of the ws manipulator seems a clever way to handle eof detection after last value
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 5
Reputation: aquariusdragon2 is an unknown quantity at this point 
Solved Threads: 0
aquariusdragon2 aquariusdragon2 is offline Offline
Newbie Poster

Re: Average .txt to Array Problem

 
0
  #3
Apr 15th, 2008
Originally Posted by vmanes View Post
You do have
  1. #include <fstream>
  2. using namespace std;
in your file, right?
Note that you misspell myFile in the while loop.
And your use of the eof( ) method will give erroneous results in the case that the file is empty.
On the other hand, your use of the ws manipulator seems a clever way to handle eof detection after last value
i did have it but i did forget to copy it into the thread lol, but i still seem be getting a constructor destructor or type conversion before . token error, sigh. any ideas? Oh and i can't seem to mind the misspelling, but that may be because I've been staring at this for a while. Here's the code again, thanx

  1. #include <fstream>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. const int MAXSIZE = 100;
  6. double votes[MAXSIZE]; // declaration for array
  7. double vote;
  8. int count = 0;
  9. double total = 0.0;
  10. double average;
  11. ifstream myFile; // declaring input file
  12.  
  13. myFile.open ( "votes.txt" );
  14. if ( !myFile )
  15. {
  16. cout << "Error opening input file: votes.txt";
  17. system("PAUSE");
  18. exit(1);
  19. }
  20.  
  21. while ( !( myFile.eof ( ) ) )
  22. {
  23. myFile >> vote >> ws;
  24. total += vote;
  25. votes[count] = vote;
  26. count++;
  27. } // end while
  28.  
  29. if ( count != 0 )
  30. average = total / count;
  31. else
  32. average = 0.0;
  33.  
  34. myFile.close ( );
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,675
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Average .txt to Array Problem

 
0
  #4
Apr 15th, 2008
If you're claiming that what you just posted is your program, you're still a bit short.

Without int main( ) { at the beginning and return 0; } at the end, it's not a program. I added those, and it works just fine.
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 5
Reputation: aquariusdragon2 is an unknown quantity at this point 
Solved Threads: 0
aquariusdragon2 aquariusdragon2 is offline Offline
Newbie Poster

Re: Average .txt to Array Problem

 
0
  #5
Apr 15th, 2008
Originally Posted by vmanes View Post
If you're claiming that what you just posted is your program, you're still a bit short.

Without int main( ) { at the beginning and return 0; } at the end, it's not a program. I added those, and it works just fine.
omg i can't believe i actually forgot about int main, but thank you so much. However, the program stops running but there are no more error messages. However, nothing is printed, so it can't be that the file isn't opening properly, right?
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,675
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Average .txt to Array Problem

 
0
  #6
Apr 15th, 2008
Where does your code actually display anything, other than file opening failure?
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC