I am working on reading the contents of a file that looks like this
Larry 4 M 40
Leslee 6 F 99
Mary 9 F 39
Hank 6 M 88
Eden 7 F 71
Drake 5 M 94
Ava 4 F 28
Seth 4 M 11
Renaldo 3 M 77
Carrie 8 F 68
I have been able to read the data in and print it to the screen by doing this
my problem is that I am trying to get the data into variable so that I can work with it. I need to add up the scores and average them, So I have taken them in this wayCode:cout << fixed << showpoint << setprecision(2); //getting the information for the file cout << "Please enter the full path to the file: "; getline(cin, inputFileName); //Open the input file. inputFile.open(inputFileName.c_str()); //check to see that the file was opened if(!inputFile.is_open()) { cout << "Unable to opent the input file" << endl; system("PAUSE"); } //set up a while loop to read to the end of the file while(inputFile.peek() != EOF) { getline(inputFile, line); cout << line << endl; }
that was my test code to see if it is working but it's not printing anything on the screen. I should mention that I am a beginner and I have only been working with this code for 3 days. So if you help, please explain in newbie termsCode:while(!inputFile.eof()) { inputFile >> firstName >> age >> gender >> score; cout << firstName << age << gender << score << endl; }


Reply With Quote
Bookmarks