Grade Book Assignment?

Hello, I'm in a 10th Grade Computer Science class, and we're currently working on C++. I'm just trying to use a little bit of my knowledge to make this Gradebook program where, the user can input a grade, determine it's percent value, then at the end, get all of the results for the integers. (Test Average, Classwork Average, Quiz Average, Project Average, HomeWork Average, and Grade Average.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>

using std::cout;
using std::cin;

int main()
{
    int grade,worth,totalOfGrades,gradeOfTest,gradeOfClass,gradeOfQuiz,gradeOfProject,gradeOfHomeWork, total, resume, testTotal, classTotal, quizTotal, homeworkTotal, projectTotal, average, testAverage, quizAverage, projectAverage, classAverage, homeworkAverage;
    totalOfGrades = 0;
    total = 0;
    resume = 1;
    while(resume != 2)
    {
        cout << "Please enter a grade number. Then you will be prompted to tell what the grade is worth.\n";
        cin>>grade;
        cout<< "Please decide what this grade is worth. \n";
        cout<< "1. Test \n 2. ClassWork \n 3. Quiz \n 4. Project \n 5. HomeWork \n";
        cin>>worth;
        (totalOfGrades + 1);
        if(worth = 1)
        {
            (grade * .30);
            grade = gradeOfTest;
            gradeOfTest + total;
            gradeOfTest + testTotal;
            (testTotal + 1);
        }
        else if(worth = 2)
        {
            (grade * .10);
            grade = gradeOfClass;
            gradeOfClass + total;
            gradeOfClass + classTotal;
            (classTotal + 1);
        }
        else if(worth = 3)
        {
             (gradeOfQuiz * .20);
             gradeOfQuiz + total;
             gradeOfQuiz + quizTotal;
             (quizTotal + 1);
        }         
        else if(worth = 4)
        {
             (grade * .30);
             grade = gradeOfProject;
             gradeOfProject + total;
             gradeOfProject + projectTotal;
             (projectTotal + 1);
        }
        else if(worth = 5)
        {
             (grade * .10);
             grade = gradeOfHomeWork;
			 gradeOfHomeWork + total;
			 gradeOfHomeWork + homeworkTotal;
			 (homeworkTotal + 1);
        }
        cout << "Do you want to continue inputting grades? \n 1. Yes \n 2. No \n";
        cin>>resume;
    }     
    average = (total / totalOfGrades);
    testAverage = (total / testTotal);
    classAverage = (total / classTotal);
    quizAverage = (total / quizTotal);
    projectAverage = (total / projectTotal);
    
    cout << "Your total average is: "<<average<<"\n";
    cout << "Your total Test Average is: "<<testAverage<<"\n";
    cout << "Your total Class Average is: "<<classAverage<<"\n";
    cout << "Your total Quiz Average is: "<<quizAverage<<"\n";
    cout << "Your total Project Average is: "<<projectAverage<<"\n";
    cout << "Your total HomeWork Average is: "<<homeworkAverage<<"\n";
    
    system("pause");
    return 0;
}   


To be 100% truthful,I know this code is very bad, and very unorganized. I am a very new programmer, so tips and help are definitely accepted. Anyone with info please comment! Thanks!
Last edited on
Your if statement block that checks the value of worth- use two equal signs, not one.
This didn't fix the issue unfortunately. Anybody else?
You're using some values before they've been given a valid value (e.g. they're uninitialized) and you have statements that calculate a value, but that value goes nowhere - it's not assigned back to a variable.

1
2
3
4
5
6
7
8
if(worth == 1)
        {
            (grade * .30); // this calculates a value and doesn't store it anywhere
            grade = gradeOfTest; //what is gradeOfTest supposed to be? it doesn't have a valid value right now
            gradeOfTest + total;//again - calculates a value and doesn't store it anywhere  
            gradeOfTest + testTotal;//ditto and testTotal is uninitialized
            (testTotal + 1);//ditto
        }
Last edited on
Okay, that's fixed, but from the beginning, whenever I press 2(For Exiting the while loop), the program says it has stopped working, and returns a value that's like 8 digits long.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>

using std::cout;
using std::cin;

int main()
{
    int grade,worth,totalOfGrades,gradeOfTest,gradeOfClass,gradeOfQuiz,gradeOfProject,gradeOfHomeWork, total, resume, testTotal, classTotal, quizTotal, homeworkTotal, projectTotal, average, testAverage, quizAverage, projectAverage, classAverage, homeworkAverage, testNum, projectNum, quizNum, classNum, homeworkNum;
    totalOfGrades = 0;
    total = 0;
    resume = 1;
    while(resume != 2)
    {
        cout << "Please enter a grade number. Then you will be prompted to tell what the grade is worth.\n";
        cin>>grade;
        cout<< "Please decide what this grade is worth. \n";
        cout<< "1. Test \n 2. ClassWork \n 3. Quiz \n 4. Project \n 5. HomeWork \n";
        cin>>worth;
        (totalOfGrades + 1);
        if(worth == 1)
        {
            gradeOfTest=(grade * .30);
            total = gradeOfTest + total;
            testTotal = gradeOfTest + testTotal;
            testNum=(testTotal + 1);
        }
        else if(worth == 2)
        {
            gradeOfClass = (grade * .10);
            total = gradeOfClass + total;
            classTotal = gradeOfClass + classTotal;
            classNum = (classTotal + 1);
        } 
        else if(worth == 3)
        {
             gradeOfQuiz = (gradeOfQuiz * .20);
             total = gradeOfQuiz + total;
             quizTotal = gradeOfQuiz + quizTotal;
             quizNum = (quizTotal + 1);
        }         
        else if(worth == 4)
        {
             gradeOfProject = (grade * .30);
             total = gradeOfProject + total;
             projectTotal = gradeOfProject + projectTotal;
             projectNum = (projectTotal + 1);
        }
        else if(worth == 5)
        {
             (grade * .10);
			 total = gradeOfHomeWork + total;
			 homeworkTotal = gradeOfHomeWork + homeworkTotal;
			 homeworkNum = (homeworkTotal + 1);
        }
        cout << "Do you want to continue inputting grades? \n 1. Yes \n 2. No \n";
        cin>>resume;
    }     
    average = (total / totalOfGrades);
    testAverage = (total / testTotal);
    classAverage = (total / classTotal);
    quizAverage = (total / quizTotal);
    projectAverage = (total / projectTotal);
    
    cout << "Your total average is: "<<average<<"\n";
    cout << "Your total Test Average is: "<<testAverage<<"\n";
    cout << "Your total Class Average is: "<<classAverage<<"\n";
    cout << "Your total Quiz Average is: "<<quizAverage<<"\n";
    cout << "Your total Project Average is: "<<projectAverage<<"\n";
    cout << "Your total HomeWork Average is: "<<homeworkAverage<<"\n";
    
    system("pause");
    return 0;
}   

Also looking through this code, is there any possible way I can shorten the amount of integers I'm using? I'm assuming this amount is an overly large amount?
Last edited on
line 19 - this calculates a value but doesn't store it
line 24 - testTotal has no valid value when you try to add it

Variables used for totals need to be initialized to 0.

What if someone doesn't enter grades for a category and a subtotal remains 0. Watch out for divide by zero errors in lines 58-62. And if there is no average for a category - do you still want to output its line in 64-69?

You could just calculate the averages on the fly in the output statements instead of having variables to store them.

e.g. cout << "Your average is " << total/number << "\n";
Last edited on
How would I deal with the situation being nobody enters a grade for a certain variable?
It looks like you're keeping track of how many tests, quizzes etc. are being entered? You could add a check - if the number of tests etc. is equal to zero, don't calculate or output the average. (or - put a message to the effect of "No test scores entered")
I think you may be computing the averages wrong. The test average is the sum of the test scores divided by the number of tests? So I'm saying that that for a test you want to do:
1
2
3
4
5
6
7
        if(worth == 1)
        {
            gradeOfTest=grade;
            total = gradeOfTest * 0.3 + total;
            testTotal = gradeOfTest + testTotal;
            testNum=(testNum + 1);
        }

and initialize testNum to zero at the beginning.

Also, notice that I've moved the weight. The test average is the test average. The weight comes into play when computing to overall average for the class, so it affects the overall total, not the test total.

Then to compute the test average:
testAverage = (testTotal / testNum);

How would I deal with the situation being nobody enters a grade for a certain variable?

Check for it before you compute the average and output the results. Right now you compute all the averages, then you print them all out. You can save some space if you compute and print them one at a time:
1
2
3
4
if (testNum) {
    testAverage = testTotal/testNum;
   cout << "Your total Test Average is: "<<testAverage<<"\n";
}


My advice is to get this one case (test average) working properly. Once you have it working, it should be easy to get the others working too.

Some handy shortcuts:
Instead of a = a+b; you can write a += b;
Instead of a = a + 1; you can write ++a;

Also looking through this code, is there any possible way I can shorten the amount of integers I'm using?


Excellent!!! A good programmer recognizes when something can probably be shortened. It sounds like you've got the knack for this stuff. Get it working first and then we can teach you how to reduce the size. You'll need to use arrays and possibly structures. Have you learned about those yet?
My Code Now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>

using std::cout;
using std::cin;

int main()
{
    int grade,worth,totalOfGrades,gradeOfTest,gradeOfClass,gradeOfQuiz,gradeOfProject,gradeOfHomeWork, gradeOfTestP,gradeOfClassP,gradeOfQuizP,gradeOfProjectP,gradeOfHomeWorkP,total, resume, testTotal, classTotal, quizTotal, homeworkTotal, projectTotal, average, testAverage, quizAverage, projectAverage, classAverage, homeworkAverage, testNum, projectNum, quizNum, classNum, homeworkNum;
    totalOfGrades = 0;
    total = 0;
    resume = 1;
    while(resume != 2)
    {
        cout << "Please enter a grade number. Then you will be prompted to tell what the grade is worth.\n";
        cin>>grade;
        cout<< "Please decide what this grade is worth. \n";
        cout<< "1. Test \n 2. ClassWork \n 3. Quiz \n 4. Project \n 5. HomeWork \n";
        cin>>worth;
        if(worth == 1)
        {
            gradeOfTestP=(grade * .30);				//if functions are to tell the percentage of the integer(test, class,quiz, etc.) then multiplies the grade inputted by the decimal value.
            gradeOfTest = (100 - gradeOfTestP);		//this function actually tells the grade of the test, by subtracting the max score (100) by the percent of how much it's worth to the grade.
            total = gradeOfTest + total;			//total is to take the grade of the test, and average it all out in the end, this takes ALL the scores entered, adds them, then divides by totalOfGrades.
            testTotal = gradeOfTest + testTotal;	//testTotal basically takes the grade and adds it to the testTotal.
            testNum=(testNum + 1);					//testNum adds a number of tests to the amount stored. everytime the statement is used (if) it adds an integer to the counter.
        }
        else if(worth == 2)
        {
            gradeOfClassP = (grade * .10);
            gradeOfClass = (100 - gradeOfClassP);
            total = gradeOfClass + total;
            classTotal = grade + classTotal;
            classNum = (classNum + 1);
        } 
        else if(worth == 3)
        {
             gradeOfQuizP = (grade * .20);
             gradeOfQuiz = (100 - gradeOfQuizP);
             total = gradeOfQuiz + total;
             quizTotal = grade + quizTotal;
             quizNum = (quizNum + 1);
        }         
        else if(worth == 4)
        {
             gradeOfProjectP = (grade * .30);
             gradeOfProject = (100 - gradeOfProjectP);
             total = gradeOfProject + total;
             projectTotal = grade + projectTotal;
             projectNum = (projectNum + 1);
        }
        else if(worth == 5)
        {
             gradeOfHomeWorkP = (grade * .10);
             gradeOfHomeWork = (100 - gradeOfHomeWorkP);
			 total = gradeOfHomeWork + total;
			 homeworkTotal = grade + homeworkTotal;
			 homeworkNum = (homeworkTotal + 1);
        }
        cout << "Do you want to continue inputting grades? \n 1. Yes \n 2. No \n"; //asks the user if they want to continue
        cin>>resume;
        totalOfGrades = totalOfGrades + 1;  //this adds a number to the total number of grades. This may need to be moved somewhere. Not positive.
    }     
    average = (total / totalOfGrades);		//these all take the totals of each variable (class, quiz,test, etc.) and divides them by the number of each.
    testAverage = (testTotal / testNum);
    classAverage = (classTotal / classNum);
    quizAverage = (quizTotal / quizNum);
    projectAverage = (projectTotal / projectNum);
    
    cout << "Your total average is: "<<total/totalOfGrades<<"\n";		//my math is probably incorrect on a lot of this, can someone help?
    cout << "Your total Test Average is: "<<testAverage<<"\n";			//also about declaring if someone has entered no values for a grade type, can someone make a code for me?
    cout << "Your total Class Average is: "<<classAverage<<"\n";
    cout << "Your total Quiz Average is: "<<quizAverage<<"\n";
    cout << "Your total Project Average is: "<<projectAverage<<"\n";
    cout << "Your total HomeWork Average is: "<<homeworkAverage<<"\n";
    
    system("pause");
    return 0;
}   


So... i've added comments for someone to review. As stated, I don't know if my math is entirely correct, and I've tinkered out some of the bugs.
Get it working first and then we can teach you how to reduce the size. You'll need to use arrays and possibly structures. Have you learned about those yet?
I HAVE learned about arrays, just don't know really how to use them, lol?
Can anyone help me? I haven't seen any activity on this page in a few days...
What have you tried in the last few days?

Have you studied structures/classes, functions yet? If so you may want to think about using a structure to group the data and functions to do the calculations to avoid some of the code duplication.

I don't understand your math. Let's try it another way.
First, you need to initialize all of your xyzTotal and xyzNum values to zero.

Then just keep track of the sum and the number of items. For example:
1
2
3
4
5
if(worth == 1) // test
{
      testTotal += grade;
      testNum++;
}


When you get to the end, the test average is testTotal / testNum, but you want this expressed in floating point. Dividing integers just truncates the result. E.g. 20/7 is 2. So you want to print the test average as (double)testTotal / testNum. By converting testTotal to double, testNum will be converted also and the arithmetic will be done with double precision floating point numbers.

Now what about the overall total? That's a weighted average:
overAllAverage = (testTotal*0.3 + classTotal*0.1 + quizTotal*0.2 + projectTotl*0.3 + homeworkTotal*0.1) / (testNum+classNum+quizNum+projectNum+homeworkNum);

As for dealing with cases where there no values where entered, see my earlier post. I already answered it.
Topic archived. No new replies allowed.