Invalid input check ( obscure exercise )

Hi, guys. I am new to c++ and I have a problem understanding what I should do exactly considering this problem. Below it is my code.

A set of examination marks expressed as percentages are to be processed by computer. Data entry is terminated by entering a negative percentage value. As each examination mark is entered it should be validated as being either a valid
percentage or as being a negative number. The program should ask for the data entry to be repeated until a valid value is entered. The program should then output the number of percentage marks entered, the average mark and how
many marks were above 40. The program should implement the loop by using a do-while loop.

Here is my code:

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
#include<iostream>

using namespace std;

int main()
{
    int i = 0, count = 0, above_forty = 0, sum = 0, num;
    float avg;
    bool valid;
    
    while( i < 10 )
    {
        do
        {
            cout << "Enter number :";
            cin >> num;
            
            if( num < 0 || num > 100 )
            {
                valid = false;
                cout << "Invalid Input" << endl;
            }
            else
            valid = true;
        
        }while(!valid);
        
        count++;  
        sum += num;
        if(num > 40)
            above_forty++;
        i++;
    }
    
    cout << "number of marks entered :" << count << endl;
    cout << "number of marks above forty :" << above_forty << endl;Iwgu1985
    
    avg = sum / count;
    cout << "average = " << avg << endl; 
    
    system("PAUSE");
    return 0;   
}


The problem is that I do not know how to use negative numbers as a sentinel and at the same time check invalid input and ask the user to enter it again.
What is i doing there? Are you supposed to disallow more than 10 inputs?
Anyway. Use a bool variable to indicate that you want to quit.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main(){
    //...
    bool quit = false;
    while(i < 10){
        do{
            if(num < 0) quit = true;
            if(num > 100) valid = false;
            //...
        while(!valid && !quit);

        if(quit) break;//so that count is not increased
        //...
    }
    //...
}
please read the problem statement before my code. And cosidering your question "Are you supposed to disallow more than 10 inputs?" the answer is NO but I do not know what should I exactly do ask the user to enter a new number when he enters an invalid input or exit the loop. Just read the problem statement again and tell me if I miss something.

I appreciate your help.
It's just that your loops have nothing to do with what you want.

The most external loop has to be something like while(!positive). This way, if the input is a negative number, your loop will terminate and you can show the results to the user.

The internal loop has to be while(!valid). Valid here isn't in the sense of being a negative number, but that the value inputted by the user isn't flawed (i.e. the user didn't input "aaa" as a grade). Since num is an int, cin >> num for a user input of "aaa" will fail. That's what you need to check for in terms of input-validity.
Last edited on
Thanks for your reply but I did not find a need to use a boolean variable with the external loop. About validity issue I do not think that it's about if the user enters a string instead of a number it's all about if the number is greater than 100. In my code, if the user entered something like 'aaa' as a grade the program won't handle it correctly.

Here is my code :

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

#include<iostream>

using namespace std;

int main()
{
    int count = 0, above_forty = 0, sum = 0, num;
    float avg;
    bool valid;
    
    while( 1 )
    {
        do
        {
            cout << "Enter number :";
            cin >> num;
            
            if( num > 100 )
            {
                valid = false;
                cout << "Invalid Input" << endl;
            }
            else
            valid = true;
        
        }while(!valid);
        
        if( num < 0 ) 
            break;
        count++;  
        sum += num;
        if(num > 40)
            above_forty++;
    }
    
    cout << "number of marks entered :" << count << endl;
    cout << "number of marks above forty :" << above_forty << endl;
    
    if( count != 0 )
    {
        avg = sum / count;
        cout << "average = " << avg << endl; 
    }
    
    system("PAUSE");
    return 0;   
}
Topic archived. No new replies allowed.