Problem with char

Hello,
Why i press d or D , the problem doesn't exit? :(
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
#include <iostream>
#include <conio.h>


using namespace std;


void main(void)
{
    int x, y, z = 0;
    cout << "Enter students grades:\n(Type D to stop inputting grades)\n";
        do
        {
			cin >> x;
            if ((x == 'd') || (x == 'D'))
                break;
            else if ((x < 0) || (x > 20))
                cout << x << " is invalid \n";
            else
            z += x;
        } while (x >= 0);


            cout << "Sum is" <<z;
            _getch();
}
closed account (48T7M4Gy)
.
Last edited on
I think that the 'break' is fine.

The problem is that variable 'x' is of type 'int', so it cannot contain 'd' or 'D', nor can a character be read from standard input into variable 'x'.

Thank you,but what i do? i want get number but if press d or D ,break
If user is allowed to type in non-numbers, then the program should first read in the user's input as a string. Then it should verify whether the string can be interpreted as a number. If it can be, convert the string to a number...

This question was recently explained in detail in this thread:

http://www.cplusplus.com/forum/beginner/176456/
Topic archived. No new replies allowed.