Why does the cin.fail() to work properly when I input a string or char?

EDIT.
Can someone please tell me why this crashes my IDE? so what I do is it prompts me to choose an option from a menu, and I input (for an example) wordssss. I made a while(cin.fail()) to catch it. here is my code.
(the code does simple math for you and will continue to run until the user inputs 5 to exit when prompted to enter input)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181






#include <iostream>
#include <string>

using namespace std;

double add(double , double );
double subt(double,double);
double multi(double,double);
double divi(double,double);


int main() {
    double x,z = 0;
    int r = 1;
    double xx, yy;
    
 do{
r = 1;
cout<<"Welcome to Glen's Handy Calculator"<<endl;
cout<<"     1. Addition "<<endl;
cout<<"     2. Subtraction "<<endl;
cout<<"     3. Multiplication "<<endl;
cout<<"     4. Division "<<endl;
cout<<"     5. Exit "<<endl;
cout<<"What would you like to do? ";

    
   cin >> x;
   
   while(r == 1){
   if (x ==1){
       cout<<"\nplease input two numbers seperated by spaces: ";
       
       cin>>xx>>yy;
       if(cin.fail()) {
        cout << "Error not an double please type something than hit enter: " << endl;
        cin.clear();
        cin.ignore(256,'\n');
        break;
       }
       else{
       cout<<endl;
       cout<<"this is the answer: "<<add(xx,yy)<<endl;
       r = 123;
       break;
       }
       
       
   }
   else if (x ==2){
       cout<<"\nplease input two numbers seperated by spaces: ";
       
       cin>>xx>>yy;
       cout<<endl;
        if(cin.fail()) {
        cout << "Error not an double please type something than hit enter: " << endl;
        cin.clear();
        cin.ignore(256,'\n');
        break;
       }
       else{
       cout<<"this is the answer: "<<subt(xx,yy)<<endl;
       r = 123;
       break;
       }
       
   }
   
      else if (x ==3){
       cout<<"\nplease input two numbers seperated by spaces: ";
       
       cin>>xx>>yy;
       cout<<endl;
       if(cin.fail()) {
        cout << "Error not an double please type something than hit enter: " << endl;
        cin.clear();
        cin.ignore(256,'\n');
        r = 3; 
        break;
       }
       else{
       cout<<"this is the answer: "<<multi(xx,yy)<<endl;
       r = 123;
       break;
       }
       
   }
   
         else if (x ==4){
       cout<<"\nplease input two numbers seperated by spaces: ";
       
       cin>>xx>>yy;
       cout<<endl;
       if(cin.fail()) {
        cout << "Error not an double please type something than hit enter: " << endl;
        cin.clear();
        cin.ignore(256,'\n');
        r = 123;
        break;
       }
       else{
       cout<<"this is the answer: "<<divi(xx,yy)<<endl;
       r = 123;
      break;
       }
      
       
   }
   
   else if(x == 5){
       cout<<"GOOD BYE";
       
       x = 69;
       r = 123;
       z = 12;
       break;
   }
   
else if(x>5 || x<=0){
cout<<"\nPlease try again"<<endl;
      r = 123;
       break;
}
   
   else{
    
        cout << "Error not an double please type something than hit enter: " << endl;
      r = 123;
        break;
    
   }
    
 }
 }while(z==0);



    
    
   
    

    return 0;
}

double add(double x, double y){
    double sum = x+y; 
    return sum;
}

double subt(double x, double y){
    double sub  = x-y;
    return sub;
}

double multi(double x, double y){
    double mul = x*y;
    return mul;
}
double divi(double x, double y){
    double divis = x/y;
    return divis;
}













TY,
I took of the while loop in the else statement because if all fails it has to go there, but it still crashes
Last edited on
Examine line 105 carefully. Are you comparing?
ty meant to do == on 105
so when I am prompted with the menu if I type in any char or string it crashes. But if I chose a menu option and then input a string or a char, the error will prompt.

EDIT.
After I noticed on the test and run for this website, it just shows that, if i am prompted with the menu and i input a string or char, it will be an infinite loop of the menu and saying the error message which makes no sense why it should be doing that.
Last edited on
Take a look at line 34. After you get a user input you want to check if that user input was compatible with whatever is being used to store said input.

1
2
3
4
5
6
std::cin >> x;
if(std::cin.fail())
{
    std::cin.clear();
    std::cin.ignore(256, '\n');
}
Topic archived. No new replies allowed.