Program wont get out of loop when I use a char during num input

Im just messing around in this program and i noticed that when i get to the number input, say i put in an '=' sign it jus repeats, sorry try again over and over. How do i fix that so that it just reads that '=' or any other char is invalid? I would suggest you compile it because im a sloppy program writer but if you can read it then awesome.

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
#include<iostream>
#include<cstring>
using namespace std;

int main()
{
    system("TITLE Orville .3");
    char username[8];
    char password[8];
    char bday[8];
    char doagain;
    int numbergame1;
    
  do//start of first do statment
  {
    cout << "Welcome, the reason iam going to use, username and things like that" << endl;
    cout << "Is to test using something like string comparision" << endl;
    cout << "Username is Orville, and Password is Rodgers and" << endl;
    cout << "B-day is J101989." << endl;
    cout << endl;
            
    cout << "Username: " << endl;
    cin >> username;
if (strcmp(username, "Orville")== 0){//start of my first if, "if username isnt orville end program"
                                      }else{
                                      cout << "Incorrect" << endl;
                                      system("pause");
                                      return 0;
                                             }//end of entire if statement
                                         
     cout << "Password: " << endl;
     cin >> password;
if (strcmp(password, "Rodgers")== 0){//start of my first if, "if username isnt orville end program"
                                      }else{
                                      cout << "Incorrect" << endl;
                                      system("pause");
                                      return 0;
                                             }//end of entire if statement
     cout << "Birthday" << endl;
     cin >> bday;                                        
if (strcmp(bday, "J101989")== 0){//start of third, same thing.
                                      }else{
                                      cout << "Incorrect" << endl;
                                      system("pause");
                                      return 0;
                                             }//end end of entire if statement
     cout << "So i thought id try to make up a stupid lil game, lets play! :D" << endl;
     cout << "Try to just guess the number i want" << endl;
     do//second do..while loop
     {
     cin >> numbergame1;
if (numbergame1 < 5){
                                       cout << "Sorry try again" << endl;
                                       }
if (numbergame1 > 5){
                                       cout << "Sorry, Try again!" << endl;
                                       }
                                       
      }while(numbergame1 !=5); //end of second do..while loop                             

if (numbergame1 == 5){
                                       cout << "Good Job!" << endl;
                                       }
      cout << "So now my little game is over" << endl;
      cout << "Which is awesome because it means" << endl;
      cout << "Im doing better in C++ being that this" << endl;
      cout << "is the first succesful program attempt"<<endl;
      cout << "back when i have more to add to this code" << endl;
      cout << "Orville .3" << endl;                           
                                                           
      cout << "Again?" << endl;
      cin >> doagain;
      
    }while (doagain=='y' || doagain=='Y'); //end of do..while statemnt
    
      system("pause");
      return 0;                                       
}//end of main code 

Last edited on
Hello,

if you enter a non-number, the cin<<numbergame will cause an error in the cin-stream. You have to repair the stream and clear it.

So line 51 should be:
1
2
3
4
5
6
7
8
9
10
11
12
13
bool go_on;
do
{
   go_on= 1; // if everything goes smoothly, this will end the loop
   if (!cin<<numbergame) // reads the input and checks if it is of the correct datatype
   {
      cin.clear(); // repairs the stream
      cin.ignore(1000,'\n'); // clears the stream of all the garbage in it
      go_on = 0; //tells the loop to reapeat itself
      cout << "you did not input a number. Try again!";
      cout << "Try to just guess the number i want" << endl;
   };
}while{!go_on};


int main
Last edited on
Ok nvm now i have a problem the first time around if i dont guess 5 off the bat then all you get is this stupid Sorry Try Again! messeage no matter what you put in. wheres the mistake?

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
#inclue<iostream>
#include<cstring>
#include<iomanip.h>

using namespace std;
            

int main()
{
    system("TITLE Orville .3");
    char username[8];
    char password[8];
    char bday[8];
    char doagain;
    char z;
    int x;
    int y;
    int numbergame1;
    
  do//start of first do statment
  {       
    cout << "Welcome, the reason iam going to use, username and things like that" << endl;
    cout << "Is to test using something like string comparision" << endl;
    cout << "Username is Orville, and Password is Rodgers and" << endl;
    cout << "B-day is J101989." << endl;
    cout << endl;
            
    cout << "Username: " << endl;
    cin >> username;
if (strcmp(username, "Orville")== 0){//start of my first if, "if username isnt orville end program"
                                      }else{
                                      cout << "Incorrect" << endl;
                                      system("pause");
                                      return 0;
                                             }//end of entire if statement
                                         
     cout << "Password: " << endl;
     cin >> password;
if (strcmp(password, "Rodgers")== 0){//start of my first if, "if username isnt orville end program"
                                      }else{
                                      cout << "Incorrect" << endl;
                                      system("pause");
                                      return 0;
                                             }//end of entire if statement
     cout << "Birthday" << endl;
     cin >> bday;                                        
if (strcmp(bday, "J101989")== 0){//start of third, same thing.
                                      }else{
                                      cout << "Incorrect" << endl;
                                      system("pause");
                                      return 0;
                                             }//end end of entire if statement
     cout << "So i thought id try to make up a stupid lil game, lets play! :D" << endl;
     cout << "Try to just guess the number i want" << endl;
     cin >> numbergame1;
     do//second do..while loop
     {
     bool go_on=1;
     do
     {
     go_on=1;           
if (numbergame1 < 5){
                cin.clear();
                cin.ignore(1000, '\n');
                go_on=0;
                cout << "Sorry Try Again!" << endl;
                                       }
     }while(!go_on);
     do
     {
     go_on=1;                                     
if (numbergame1 > 5){
                cin.clear();
                cin.ignore(1000, '\n');
                go_on=0;
                cout << "Sorry, Try again!" << endl;
                                       }
     }while(!go_on);
     
                                       
      }while(numbergame1 !=5); //end of second do..while loop                             

if (numbergame1 == 5){
                                       cout << "Good Job!" << endl;
                                       }
      cout << "So now we've done my little game" << endl;
      cout << "Which is awesome because it means" << endl;
      cout << "Im doing better in C++" << endl;
      cout << "Back when i have more to add to this code" << endl;
      cout << "Orville .3" << endl; 
      cout << "Lets try, doing simple math functions" << endl;
      cout << "Lets get some numbers :) Any number, big or small will do" << endl;
      cin >> x;
      cout << "Great! Now lets get that second number. Your first number:  " << x << endl;
      cin >> y;
      cout << "Ok, Lastly, we need an operation to do(+,-,/ or *) Your second number:  " << y << endl;
      cin >> z;
          switch (z){
                 case '+':
                      cout << "The answer is: " << x << "+" << y << "=" << (x + y) << endl;
                      break;
                 case '-':
                      cout << "The answer is: " << x << "-" << y << "=" << (x - y) << endl;
                      break;
                 case '/':
                      cout << "The answer is: " << x << "/" << y << "=" << (x / y) << endl;
                      break;
                 case '*':
                      cout << "The answer is: " << x << "*" << y << "=" << (x * y) << endl;
                      break;
                      }
       cout << "Congratulations" << endl;
       cout << "Well what else can I do?" << endl;
       cout << "Ill try to use some enums and classes in the next part" << endl;

       
             
       cout << "Again?" << endl;
       cin >> doagain;
      
      
    }while (doagain=='y' || doagain=='Y'); //end of do..while statemnt
    
      system("pause");
      return 0;                                       
}//end of main code

                                        
Last edited on
Topic archived. No new replies allowed.