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 ( );

Recommended Answers

All 5 Replies

You do have

#include <fstream>
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 :idea:

You do have

#include <fstream>
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 :idea:

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

#include <fstream>
   #include <iostream>
using namespace std;
   
   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 ( );

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.

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?

Where does your code actually display anything, other than file opening failure?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.