Help with coding error

I have done this short program to process students marks, ca;cu;ate percentage pass and fail but every time i enter the marks I miss one entry.The number wrote is not equal to the sum of number passed and number failed as it should be.

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
 //A college tool for analysing students marks, group according to levels and 
//calculation of percentage pass and fail.

#include <iostream>
using namespace std;

int main()
{
    
    double average, mark,p_Fail, p_Pass, numWrote, numPass, numFail, sumMarks;
    int level1, level2, level3, level4, level5, level6, level7;
    level1 = level2 = level3 = level4 = level5 = level6 = level7 = 0;
            
    while (mark != -1) {
        sumMarks += mark;
        ++numWrote;
        cout << "Enter a mark: ";               // Enter marks to be processed.
        cin >> mark;
            if ((mark >= 0) && (mark <= 29)) {  // Group marks according to their levels
            ++level1;
             }
            else if ((mark >=30) && (mark <= 39)) { 
            ++level2;
            }
            else if ((mark >=40) && (mark <= 49)) {
            ++level3;
            }
            else if ((mark >=50) && (mark <= 59)) {
            ++level4;
            }
            else if ((mark >=60) && (mark <= 69)) {
            ++level5;
            }
            else if ((mark >=70) && (mark <= 79)) {
            ++level6;
            }
            else if ((mark >=80) && (mark <= 100)) {
            ++level7;
            } 
    }
    average = sumMarks / numWrote;
    numPass = level4 + level5 + level6 + level7;
    numFail = level1 + level2 + level3;
    p_Pass = (numPass/numWrote)*100.0;
    p_Fail = (numFail/numWrote)*100.0;
       
    cout << "The average grade is: " << average << endl; //
    cout << "Number wrote = "<<" "<< numWrote<<endl;
    cout << "Number passed ="<<" "<< numPass <<endl;
    cout << "Number failed ="<<" "<< numFail <<endl;
    cout << "Percentage Pass ="<<" "<< p_Pass <<endl;
    cout << "Percentage fail ="<<" "<< p_Fail <<endl;
    cout << "Number in level 1 ="<<" "<<level1<< endl;
    cout << "Number in level 2 ="<<" "<<level2<< endl;
    cout << "Number in level 3 ="<<" "<<level3<< endl;
    cout << "Number in level 4 ="<<" "<<level4<< endl;
    cout << "Number in level 5 ="<<" "<<level5<< endl;
    cout << "Number in level 6 ="<<" "<<level6<< endl;
    cout << "Number in level 7 ="<<" "<<level7<< endl;
    
    return 0;
}
Firstly :

1
2
3
4
5
6
7
double average, mark, p_Fail, p_Pass, numWrote, numPass, numFail, sumMarks;

average = mark = p_Fail = p_Pass = numWrote = numPass =  numFail = sumMarks = 0;

int level1, level2, level3, level4, level5, level6, level7;

level1 = level2 = level3 = level4 = level5 = level6 = level7 = 0;


Variables usually start with random values if left uninitialized. In order for the program to work properly, you should always initialize your variables first just in case.
to avoid the mistakes Half Life G Man pointed out compile with warnings on

here's warning of your code as shown in cpp.sh (with "many warnnings" enabled)

In function 'int main()':
10:21: warning: 'mark' is used uninitialized in this function [-Wuninitialized]
15:25: warning: 'sumMarks' may be used uninitialized in this function [-Wmaybe-uninitialized]
16:19: warning: 'numWrote' may be used uninitialized in this function [-Wmaybe-uninitialized]


also nowhere in the code output you have mentioned that you have to enter -1 to exit the loop, most user's (almost all) don't see the code in real life and will thus be puzzled... so it would be great to mention the fact in your output

also line 15 and 16 should serve better if placed after cin>>marks

hope it helps

PS: welcome to cplusplus.com :)
Thanks Guys.

I did learn from your responses but the problem of and extra mark remained after making changes. Its like the -1 was being captured as a mark despite the while loop. Finally managed to solve it by initializing the numWrote = level1 + level2 + level3 + level4 + level5 + level6 + level7.

Thanks
welcome :)
the problem of and extra mark remained after making changes. Its like the -1 was being captured as a mark despite the while loop.

At line 18, you cin the -1, but don't check for it. You simply continue on as if it were a valid mark. Your while loop won't check it until you get back to the top.

Your if/else tree ignores the -1, but you've already added mark to sumMarks and incremented numWrote. You need to reorder lines 15-18.

15
16
17
18
19
20
21
22
 while (true)   // Don't need to check for -1 here
{  cout << "Enter a mark: ";               // Enter marks to be processed.
    cin >> mark;
    if (mark == -1)
      break;  // exit the loop
    sumMarks += mark;
    ++numWrote;
    // continue with if/else tree  

closed account (48T7M4Gy)
1
2
3
4
while(cin >> mark && mark != -1)
{
      // ...
}
Topic archived. No new replies allowed.