Let’s consider a file with the following student information: John Smith 99 Sarah Johnson 100 Jim Robinson 70 Mary Anderson 95 Michael Jackson 92 Each line in the file contains the first name, last name, and test score of a student. Write a program that prompts the user for the file name, then calculates and displays the average score by reading the data from the file.
what language? what part can't you do?
c++
and just finding the average part, would you like to see what i have so far?
Yes. We're supposed to assist people, not do their work for them. Post your code and a specific question.
my question would be how to find the average
because the result i get is not the correct one
@rsmith6559
also i am not sure if i should have assigned 0 to totalSum
#include<iostream> #include<string> #include<fstream> using namespace std; int main() { ifstream ins; string firstName; string lastName; string student; int score; int totalSum=0; float average; cout << "Enter file name: "; cin >> student; ins.open(student.c_str()); while (!ins.eof()) { ins >> firstName >> lastName >> score; totalSum = totalSum + score; } average = totalSum / 5; cout<<average<<" is the average score."; ins.close(); system("pause"); return 0; }
average = totalSum / 5; //assumes that there is alway 5 records read IMO, your while loop should count the number of records read, and then that value is what totalSum should be divided by. Just because I think Windows is a toy, you should close your file as soon as you're done with it. Windows has exclusive file locking, only one program can use (even just read) a file at a time. Not a desireable thing on a server.
okay, so you do think my while loop is structured correctly right, do you think there would be room for error?
okay i closed the file, it did nothing
the program is only reading the first line .-.
how do I get my while loop to count the number of records , and then divide totalSum?
nvm i got it
I had to add a variable x and assign it to 0 and then update it in the while loop and then have totalSum/x
The timing of closing the files is just to get you into good habits. As far as the program reading the first line, open your data file and make sure that there's more than one line in the file.
FWIW, double check your results. I wrote a test program of your code and I had to use an if( ins.eof() ) break; to get the correct answer.
what was the answer you got?
@rsmith6559
this is my updated code
#include<iostream> #include<string> #include<fstream> using namespace std; int main() { ifstream ins; string firstName; string lastName; string student; int score; int totalSum=0,x = 0; float average; cout << "Enter file name: "; cin >> student; ins.open(student.c_str()); while (!ins.eof()) { ins >> firstName >> lastName >> score; totalSum = totalSum+score; x++; } ins.close(); average = totalSum / x; cout<<average<<" is the average score."<<endl; system("pause"); return 0; }
and the answer it gives back is 91 of type int
I made it type int b/c when i made it a float it gave me 91.3333... which is not the answer i calculated manually, which was 91.222..
I found out, with the help of cplusplus.com, that my program was processing the last line of the file twice. It would cause just that sort of discrepency. I don't understand why, I'm wondering if the cin is previewing the eof and unsetting the eof bit so that the while statement evaluates to true again. // Interesting way to HAVE to handle a read loop while( 1 ) { inFile >> firstName >> lastName >> score; if( inFile.eof() ) break; cout << firstName << " " << lastName << "\t" << score << endl;
okay this is what I updated it to, and it gave me the correct average
#include<iostream> #include<string> #include<fstream> using namespace std; int main() { ifstream ins; string firstName; string lastName; string student; int score; int totalSum=0; float average, x = 0; cout << "Enter file name: "; cin >> student; ins.open(student.c_str()); while (!ins.eof()) { ins >> firstName >> lastName >> score; if (ins.eof(), x < 5) { totalSum = totalSum + score; x++; } } ins.close(); average = totalSum / x; cout<<average<<" is the average score."<<endl; system("pause"); return 0; }
@rsmith6559
Just one of those little stinkers that make programming so much fun!
Join our real-time social learning platform and learn together with your friends!