Thread: Question about reading from files

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    18

    Question about reading from files

    we are learning about reading from text files in my class. I have a list of student names along with their grade on a history test. I need to find the class average.
    Here is my code so far:

    Code:
    // File: Gradeclass.cpp
    // Class: ITCS 1214 TR 5pm
    // Purpose: To classify student's grade by 
    // Satisfactory, Unsatisfactory, and Outstanding.
    
    #include <iostream>
    #include <fstream>
    
    int main()
    {
    	string name_1[10],
    			 name_2[10];
    	int test_score[10],
    		 class_total = 0,
    		 index;
    	float grade[10],
    			class_average;
    			
    	index = 0;
    	
    	ifstream in_file;
    	
    	in_file.open("History_Exam1.txt", ios::in);
    	
    	for(index = 0; index < 10; index = index + 1)
    	{
    		in_file >> name_1[index];
    		in_file >> name_2[index];
    		in_file >> test_score[index];
    	}
    	
    	for(index = 0; index < 20; index = index + 1)
    	{
                            // this is where I am stuck
    		class_average = test_score[index]
    For that last part, I need to find the class average so what I basically need to know is how to write the sum of the test scores so I can divide it by the number of students.

    Thanks in advance for the help.

  2. #2
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Is this what your looking for?
    Code:
    class_average = 0;
    for(index = 0; index < 20; index++)
    	class_average += test_score[index];
    class_average /= 20;

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    18

    Question More trouble!!

    Quote Originally Posted by Yarin View Post
    Is this what your looking for?
    Code:
    class_average = 0;
    for(index = 0; index < 20; index++)
    	class_average += test_score[index];
    class_average /= 20;
    Yeah I believe so. Except I just realized there is only 10 students in the file, so I've changed that. Thanks.
    I've gotten it to compile now but when I run it, it's caught in an endless loop.
    Here's the code and part of the output:
    Code:
    // Purpose: To classify student's grade by 
    // Satisfactory, Unsatisfactory, and Outstanding.
    
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main()
    {
    	int test_score[10],
    		 index;
    	float grade[10],
    			class_average;
    	string firstname[10],
    			 lastname[10];
    	
    	ifstream in_file;
    	
    	in_file.open("History_Exam1.txt", ios::in);
    	
    	for(index = 0; index < 10; index = index + 1)
    	{
    		in_file >> firstname[index];
    		in_file >> lastname[index];
    		in_file >> test_score[index];
    	}
    	
    	class_average = 0;
    	
    	for(index = 0; index < 10; index = index ++)
    	{
    		class_average += test_score[index];
    		class_average /= 10;
    	
    		if(test_score[index] > class_average + 10.0)
    		{
    			cout << firstname[index] << " " << lastname[index] << "'s grade: " << test_score[index];
    			cout << " OUTSTANDING.";
    		}
    		else
    		{
    			if(test_score[index] < class_average - 10)
    			{
    				cout << firstname[index] << " " << lastname[index] << "'s grade: " << test_score[index];
    				cout << " UNSATISFACTORY.";
    			}
    			else
    			{
    				cout << firstname[index] << " " << lastname[index] << "'s grade: " << test_score[index];
    				cout << " SATISFACTORY.";
    			}
    		}
    	}
    	return (0);
    }
    Code:
    ichard Miller's grade: 55 OUTSTANDING.Richard Miller's grade: 55 OUTSTANDING.Ric
    hard Miller's grade: 55 OUTSTANDING.Richard Miller's grade: 55 OUTSTANDING.Richa
    rd Miller's grade: 55 OUTSTANDING.Richard Miller's grade: 55 OUTSTANDING.Richard
     Miller's grade: 55 OUTSTANDING.Richard Miller's grade: 55 OUTSTANDING.Richard M
    iller's grade: 55 OUTSTANDING.Richard Miller's grade: 55 OUTSTANDING.Richard Mil
    ler's grade: 55 OUTSTANDING.Richard Miller's grade: 55 OUTSTANDING.Richard Mille
    r's grade: 55 OUTSTANDING.Richard Miller's grade: 55 OUTSTANDING.Richard Miller'
    s grade: 55 OUTSTANDING.Richard Miller's grade: 55 OUTSTANDING.Richard Mi
    The two problems I see is that it's obviously looping forever, and that it is giving an "outstanding" grade to richard miller, which it shouldn't.
    Outstanding should be more than 10 points above class average.
    Satisfactory should be within 10 points above or below the class average.
    Unsatisfactory should be more than 10 points below class average.

    What am I doing wrong??!!!

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Why do you think the loop is not terminating?

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    18
    Quote Originally Posted by medievalelks View Post
    Why do you think the loop is not terminating?
    I fixed the loop now, I put another "for" statement before the if-else. The problem now is just that it is giving "Outstanding" to everyone!

    Code:
    Richard Miller's grade: 55 OUTSTANDING.
    Frank Apple's grade: 71 OUTSTANDING.
    Donald Duck's grade: 84 OUTSTANDING.
    Kevin Ham's grade: 93 OUTSTANDING.
    James Bond's grade: 74 OUTSTANDING.
    Abe Beemer's grade: 70 OUTSTANDING.
    Tom Jones's grade: 84 OUTSTANDING.
    Walter Mays's grade: 78 OUTSTANDING.
    Richard Daley's grade: 64 OUTSTANDING.
    Robert Williams's grade: 82 OUTSTANDING.
    I think I may not have written the if else statement correctly. I don't know how to though.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I put another "for" statement before the if-else.
    That's not the right fix.

    Why do you think the loop wasn't terminating? Figure that out first, and then you will see where you can make a much smaller change than adding a new loop.

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    18
    Quote Originally Posted by Daved View Post
    >> I put another "for" statement before the if-else.
    That's not the right fix.

    Why do you think the loop wasn't terminating? Figure that out first, and then you will see where you can make a much smaller change than adding a new loop.
    I don't know what I did, but, I removed the additional loop to figure it out, and it worked fine.
    I saved the program and it worked fine after removing the additional loop.
    That's weird.
    It's still giving an "Outstanding" grade to everyone though!

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You are calculating class_average wrong.

    1st time through the loop, test_score[0] is 55. This is used to calculate a class_average of 5.5 (55/10). 55 is more than 15.5 (5.5+10) so you get OUTSTANDING printed.

    2nd time through the loop, test_score[1] is 71. This is used to calculate a class_average of 7.65 ((71+5.5) / 10). 71 is more than 17.65 (7.65+10) so you again get OUTSTANDING printed.

    You need to have one loop go through and sum up all the values, then at the end do the division by 10 (the number of students) to figure out the average. And then have another loop to display the student scores and compare their individual scores with the class average.

    There is only one class average but you are calculating a new one every iteration of the loop and then comparing that moving target to each individuals score.
    Last edited by hk_mp5kpdw; 04-22-2008 at 10:22 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Registered User
    Join Date
    Mar 2008
    Posts
    18
    Quote Originally Posted by hk_mp5kpdw View Post
    You are calculating class_average wrong.

    1st time through the loop, test_score[0] is 55. This is used to calculate a class_average of 5.5 (55/10). 55 is more than 15.5 (5.5+10) so you get OUTSTANDING printed.

    2nd time through the loop, test_score[1] is 71. This is used to calculate a class_average of 7.65 ((71+5.5) / 10). 71 is more than 17.65 (7.65+10) so you again get OUTSTANDING printed.

    You need to have one loop go through and sum up all the values, then at the end do the division by 10 (the number of students) to figure out the average. And then have another loop to display the student scores and compare their individual scores with the class average.

    There is only one class average but you are calculating a new one every iteration of the loop and then comparing that moving target to each individuals score.
    This is how you calculate the class_average, right?
    Code:
    	for(index = 0; index < 10; index = index + 1)
    	{
    		class_average += test_score[index];
    		
    		class_average /= 10;

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> This is how you calculate the class_average, right?
    Not quite. Your code is doing the division every time. That's not what you want.

    Try it out by hand, do you see why that's bad or are you having trouble figuring out how to achieve that.

  11. #11
    Registered User
    Join Date
    Mar 2008
    Posts
    18
    Quote Originally Posted by Daved View Post
    >> This is how you calculate the class_average, right?
    Not quite. Your code is doing the division every time. That's not what you want.

    Try it out by hand, do you see why that's bad or are you having trouble figuring out how to achieve that.
    No I got it now. I was basically making sure that was how you find the sum of the test scores.
    Once again this forum has helped me figure out my hw! The program is working now! Thanks!

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    You can get a total of the scores for calculating the class average when you read the file in the first loop.

    Code:
    class_average = 0;
    
    for(index = 0; index < 10; index = index + 1)
    {
    	in_file >> firstname[index];
    	in_file >> lastname[index];
    	in_file >> test_score[index];
    
            class_average += test_score[index];
    }
    
    // now calculate the average
    class_average /= 10;

  13. #13
    Registered User
    Join Date
    Mar 2008
    Posts
    18
    Quote Originally Posted by DaveH View Post
    You can get a total of the scores for calculating the class average when you read the file in the first loop.

    Code:
    class_average = 0;
    
    for(index = 0; index < 10; index = index + 1)
    {
    	in_file >> firstname[index];
    	in_file >> lastname[index];
    	in_file >> test_score[index];
    
            class_average += test_score[index];
    }
    
    // now calculate the average
    class_average /= 10;
    Yes, it took me a while to figure it out, thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about IOStream and reading strings from files
    By kingpinzs in forum C++ Programming
    Replies: 22
    Last Post: 12-13-2005, 11:29 AM
  2. reading files
    By hiya in forum C++ Programming
    Replies: 7
    Last Post: 05-21-2005, 11:40 AM
  3. Question About Reading Files
    By Zildjian in forum C Programming
    Replies: 1
    Last Post: 09-28-2003, 05:31 PM
  4. Reading files in a directory
    By roktsyntst in forum Windows Programming
    Replies: 5
    Last Post: 02-07-2003, 10:04 AM
  5. problem reading files in C
    By angelfly in forum C Programming
    Replies: 9
    Last Post: 10-10-2001, 11:58 AM