I am trying to write a program to practice for an upcoming test that is supposed to take in these names and numbers from a file and then take the average of all the numbers of the file. I am not allowed to use getline. Our teacher hinted that you can use cin to stuff 2 things on one line like this;
int x, y;
cin >> x >> y;
but I'm not sure how that works.
The file (Sect51.dat) looks something like this:
Will 66
Matthew 85
Andy 97
Sarah 24
Rachel 76
Mandy 94
This is what I have so far, but it is not working.
Code:#include <fstream> #include <iostream> using namespace std; int main() { ifstream WOW; WOW.open("Sect51.dat"); string x; int y, aNumber; int count = 0; while (WOW >> aNumber) { cin >> x >> y; count++; y += y; } cout << "The average is " << y / count << "." << endl; return 0; }



LinkBack URL
About LinkBacks


