Problem with while loop

I've been giving the begginer exercises located here http://www.cplusplus.com/forum/articles/12974/ a go, I've been on the first exercise for a little while and while it does as asked I have a problem when I input data that the program doesn't recognise, at least thats what I think the problem is.

When I enter numbers it is fine, but as soon as I enter a letter the loop seems to mess up and the console just repeats the text from line 15 and line 48 over and over again until I close it.

I've fiddled around for quite a while trying to move where the loop ends and trying different things and came up with nothing, probably something very simple I'm missing , I've searched around different sites and haven't been able to find a similar problem, any advice on where i'm going wrong would be apreciated.

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 <iostream>
#include <string.h>
using namespace std;


int grader()
{
string answer = "";
bool running = 1;
int x;


    while(running == 1){

    cout << "Please enter your score: ";

    cin >> x;

    if(x==100){
    cout << "You got a perfect score, A*." << endl;
    }else

    if(x>=90&&x<100){
    cout << "You got an A." << endl;
    }else

    if(x>=80&&x<90){
    cout << "You got a B." << endl;
    }else

    if (x>=70&&x<80){
    cout << "You got a C" << endl;
    }else

    if (x>=60&&x<70){
    cout << "You got a D." << endl;
    }else

    if (x>=50&&x<60){
    cout << "You got an E." << endl;
    }else

    if (x>=0&&x<50){
    cout << "You got an F." << endl;
    }else

    if(x!=0||x>100){                     
     cout << "Invalid entry." << endl;
     }

    cout << "Do you want to continue?" << endl << "y/n: ";

    cin >> answer;

     if(answer=="y"){
     running = 1;
        }else
    if(answer=="n"){
    running = 0;
    }

                        } //end loop

return 0;
}


int main()
{
  cout << grader();
}
Last edited on
Guess I wasn't looking hard enough, just saw something like this on another site, solved by adding

if (!cin)
cin.clear();

on line 18-19.
This is what I did also.:)
Topic archived. No new replies allowed.