calcuate average test grade problem

This is a discussion on calcuate average test grade problem within the C++ Programming forums, part of the General Programming Boards category; I'm currently taking a C++ class and this is a task we have to complete. I've got most of it ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    5

    calcuate average test grade problem

    I'm currently taking a C++ class and this is a task we have to complete. I've got most of it completed, but I have one problem.

    The way the program is suppose to work is it's suppose to read an infile that has an unknown number of students with their 5 best test grades. It's suppose to calculate the average for each student, then the average for the all the students. It's suppose to then count the numbers of A's, B's C's...etc there are within the students.

    Here's what I have...

    Code:
    void main ()
    { ... calls all functions and any couts ..}
    
    void take_input(int &test1, int &test2, int &test3, int &test4, int &test5, int &students, string &student)
    {
    	int numOfStud =0;
    	ifstream inData;
    	inData.open("in.txt");
    	while (!inData.eof())
    	{
    		inData>>student>>test1>>test2>>test3>>test4>>test5;
    		cout<<student<<test1<<test2<<test3<<test4<<test5<<endl;
    		students++;
    	}
    }
    
    float calculateAverage(int &test1, int &test2, int &test3, int &test4, int &test5, int &students, string &student, float &average)
    {
    	int sum;
    	float classAve, x = 0;
    	for (int i = 1; i <= students; i++)
    	{
    		sum = test1 + test2 + test3 + test4 + test5;
    		average = static_cast <float> (sum) / 5; 
    		x = x + average;
    	}
    
    	classAve = x / students;
    	return classAve;
    }
    
    void calculateGrade(int &test1, int &test2, int &test3, int &test4, int &test5, int &countA, int &countB, int &countC, int &countD, int &countF, int &students, string &student, float &average)
    {
    	float grade;
    	for (int i = 1; i <= students; i++)
    	{
    		grade = average;
    		if ((90 <= grade) && (grade <= 100))
    			countA++;
    		else if ((80 <= grade) && (grade <= 89.99))
    			countB++;
    		else if ((70 <= grade) && (grade <= 79.99))
    			countC++;
    		else if ((60 <= grade) && (grade <= 69.99))
    			countD++;
    		else if ((0 <= grade) && (grade <= 59.99)) 
    			countF++;
    	}
    }
    The problem I'm having is that it only takes the last person's grade and does the calculations with those numbers.

    Example:


    infile:
    bob 88 96 85 76 99
    suzy 91 84 72 94 74
    johnney 100 98 79 93 86

    outfile:
    johnney 100 98 79 93 86
    johnney 100 98 79 93 86
    johnney 100 98 79 93 86
    A appeared 3 times.
    B appeared 0 times.
    C appeared 0 times.
    D appeared 0 times.
    F appeared 0 times.


    In reality, Bob and Suzy got B's. I narrowed it down to there being something wrong with my loop, but I can't figure it out.

    Any help would be appreciate it. Thanks in advance!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    A few things to note:
    • void main() should be int main()
    • You should not use inData.eof() to control the loop.
    • Instead of test1, test2, etc, you should have an array of arrays, or better yet, design a student class that models a student with test grades, then store an array (or rather, a vector) of students. The failure to do this is the reason why only the grade of the last student are computed.
    • calculateAverage() does not appear to need to take any variables by reference.
    • calculateGrade() appears to have several variables that are unused.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    5
    I tried using arrays and it just wasn't working. I believe it wouldn't read the data for some reason. I was getting frustrated; I asked the professor and he said the problem can be solved without arrays anyway, so that's what I was trying to do here. And we haven't gotten into vectors yet. It's a pretty basic C++ class.

    I do appreciate the other comments though. I'll make those changes, but I consider them minor compared to the original problem. I need to figure out a way to have it read all the students first.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    Quote Originally Posted by syrup890
    I tried using arrays and it just wasn't working. I believe it wouldn't read the data for some reason. I was getting frustrated; I asked the professor and he said the problem can be solved without arrays anyway, so that's what I was trying to do here.
    Ah yes, that would actually be a more space efficient solution. This means that you need to keep a running total for the overall grade points, as well as running totals for each alphabetic grade. Oh, and you need a running total of the number of students in order to compute the overall average, but you already have such a variable.

    As soon as you read the data for a student, add each numeric grade to the overall running total. Then compute the average of the student's grades and print it out. Use this average to determine the alphabetic grade of the student, and increment the corresponding running total.

    After processing the data for all the students, compute the overall average and print it along with the running totals of the alphabetic grades.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  2. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 08:43 PM
  3. Spot the problem..a test question, pls help!
    By Unregistered in forum C++ Programming
    Replies: 10
    Last Post: 02-05-2002, 12:40 AM
  4. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 07:34 PM
  5. A problem with my linked lists test.
    By valar_king in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21