Ask your own question, for FREE!
Computer Science 11 Online
OpenStudy (anonymous):

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.

OpenStudy (anonymous):

what language? what part can't you do?

OpenStudy (anonymous):

c++

OpenStudy (anonymous):

and just finding the average part, would you like to see what i have so far?

OpenStudy (rsmith6559):

Yes. We're supposed to assist people, not do their work for them. Post your code and a specific question.

OpenStudy (anonymous):

my question would be how to find the average

OpenStudy (anonymous):

because the result i get is not the correct one

OpenStudy (anonymous):

@rsmith6559

OpenStudy (anonymous):

also i am not sure if i should have assigned 0 to totalSum

OpenStudy (anonymous):

#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; }

OpenStudy (rsmith6559):

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.

OpenStudy (anonymous):

okay, so you do think my while loop is structured correctly right, do you think there would be room for error?

OpenStudy (anonymous):

okay i closed the file, it did nothing

OpenStudy (anonymous):

the program is only reading the first line .-.

OpenStudy (anonymous):

how do I get my while loop to count the number of records , and then divide totalSum?

OpenStudy (anonymous):

nvm i got it

OpenStudy (anonymous):

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

OpenStudy (rsmith6559):

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.

OpenStudy (rsmith6559):

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.

OpenStudy (anonymous):

what was the answer you got?

OpenStudy (anonymous):

@rsmith6559

OpenStudy (anonymous):

this is my updated code

OpenStudy (anonymous):

#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; }

OpenStudy (anonymous):

and the answer it gives back is 91 of type int

OpenStudy (anonymous):

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..

OpenStudy (rsmith6559):

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;

OpenStudy (anonymous):

okay this is what I updated it to, and it gave me the correct average

OpenStudy (anonymous):

#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; }

OpenStudy (anonymous):

@rsmith6559

OpenStudy (rsmith6559):

Just one of those little stinkers that make programming so much fun!

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!