| | |
Average .txt to Array Problem
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2008
Posts: 5
Reputation:
Solved Threads: 0
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 ( );
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 ( );
You do have 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
C++ Syntax (Toggle Plain Text)
#include <fstream> using namespace std;
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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
Join Date: Apr 2008
Posts: 5
Reputation:
Solved Threads: 0
•
•
•
•
You do havein your file, right?C++ Syntax (Toggle Plain Text)
#include <fstream> using namespace std;
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
C++ Syntax (Toggle Plain Text)
#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
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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
Join Date: Apr 2008
Posts: 5
Reputation:
Solved Threads: 0
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?
"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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Please help me!! Newbie in C++
- Next Thread: Problems in Address book programming
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






