NEED HELP for my first C++ homework

I need to write a programme which continuously requests a grade to be entered. If the grade is less than 0, cout that "this is an invalid grade". Or else the grade should be added up as a total.While a grade of 999 is entered, the programme should exit the loop and compute the average of the valid grades.

this is how I did so far.

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
#include <iostream>;
#include <iomanip>;
using namespace std;
int main() {

  int grade = 0;
  int total_grade = 0;
  int number_of_grade = 0;
  double average = 0.0;

  do {
	cout << "Please enter an integer grade (enter 999 to calculate the average)" << endl;
	cin >> grade;
	if (grade < 0 || grade > 100)
		cout << "This is an invalid grade" << endl;
	else if (grade >= 0 && grade <= 100)
		number_of_grade += 1;
	    total_grade = total_grade + grade;
  } 
 
	while (grade != 999);
  
	average = total_grade / number_of_grade;
	cout << "The average grade you entered is" << average << endl;
  


	system ("pause");
	return 0;
}



but the average it calculate is included those invalid grades.
I dont want the invalid grades to be counted.

what do i need to modify?
thanks
Last edited on
I don't know how to make the programme continuouly requst a grade.

Crack open your book. Read about loops.

and i dont how to compute the average grade since I dont know how many grades will be entered.

Keep track of the number of grades entered.
I did something similar, but still couldnt figure out how to exclude those invalid grades
First of all, please use [.code]Some code[./code] tags (without the dot). It makes reading the code easier. To figure out how to exclude invalid grades, only keep track of the number of valid grades. Check to see if something is valid, and only if it is, count it up.
I tag the code already. =)
Here are some examples of averaging a grade:

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
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int main(int nNumberofArgs,char* pszArgs[])
{
    char studentName[256];
    double studentGpa;
    char grade;
    double gradeE;

    cout << "Enter the student's name: ";
    cin.getline(studentName,256);
    cout << endl;

    cout << "Enter the student's current GPA: ";
    cin >> studentGpa;
    cout << endl;

    cout << "Enter * to quit. " << endl;

    for(;;)
    {
        cout << "Enter the student's grade: ";
        cin >> grade;

        if(grade == '*')
        {
            break;
        }

        switch(grade)
        {
            case 'a':
            case 'A':
            gradeE = 4.0;
            break;
            case 'b':
            case 'B':
            gradeE = 3.0;
            break;
            case 'c':
            case 'C':
            gradeE = 2.0;
            break;
            case 'd':
            case 'D':
            gradeE = 1.0;
            break;
            case 'f':
            case 'F':
            gradeE = 0.0;
            default:
            cout << "Please enter A,B,C,D or F" << endl;
            break;
        }

        studentGpa = gradeE + studentGpa;
        studentGpa = studentGpa / 2;

        if(studentGpa < 0.01)
        {
            studentGpa = 0.0;
        }
        cout << studentName << "'s new GPA is " << studentGpa << endl;
    }
    system("PAUSE");
    return 0;
}


Second example:

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int main(int nNumberofArgs,char* pszArgs[])
{
    int grade;
    int accumulator = 0;
    int total;
    char name[256];
    int number = 0;
    char lgrade;
    char choice;

    cout << "Enter the student's name: ";
    cin.getline(name,256);
    cout << endl;
    for(;;)
    {
        cout << "Enter test grade, enter a negative to quit: " << endl;
        cin >> grade;

        if(grade < 0)
        {
            break;
        }
        if(grade > 100)
        {
            cout << "Enter a number between 100 and 0" << endl;
        }
        if(grade >= 0 && grade <= 100)
        accumulator = grade + accumulator;

        number++;
    }

    total = accumulator / number;

    cout << "Do you want to see a letter grade?(y/n): ";
    cin >> choice;

    if(choice == 'y')
    {
        if(total > 90)
        {
            lgrade = 'A';
        }
        if(total == 90)
        {
            lgrade = 'A';
        }
        if(total > 80 && 90 > total)
        {
            lgrade = 'B';
        }
        if(total == 80)
        {
            lgrade = 'B';
        }
        if(total > 70 && 80 > total)
        {
            lgrade = 'C';
        }
        if(total == 70)
        {
            lgrade = 'C';
        }
        if(total > 60 && 70 > total)
        {
            lgrade = 'D';
        }
        if(total == 60)
        {
            lgrade = 'D';
        }
        if(total > 50 && 60 > total)
        {
            lgrade = 'F';
        }
        if(total < 50)
        {
            lgrade = 'F';
        }
        if(total == 50)
        {
            lgrade = 'F';
        }

        cout << name << "'s letter grade is " << lgrade << endl;
        system("PAUSE");
        return 0;
    }

    cout << name << "'s average grade is " << total << endl;
    system("PAUSE");
    return 0;
}
Topic archived. No new replies allowed.