inappropriate termination in loop

The program works the way i want it to but the only problem with it is that when the user guesses a non integer i will like to output to console
1
2
cout << "You guess was not a number, Try Again" << endl; 
and continue running program with tries deducted.

1
2
3
4
5
6
7
//HEADER FILE
#ifndef Hi_lo_header_h
#define Hi_lo_header_h

int hi_Lo_Game ();

#endif /* Hi_lo_header_h */ 



1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include "Hi lo header.h"

int main() {
    
    int Game;
    
    Game = hi_Lo_Game();
    
    return 0;
}



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

#include <iostream>
#include <ctime>
#include <cstdlib>

int game_rePlay();

int hi_Lo_Game ()

{
    
    
    using namespace std;
    
    int userMaxRand; int userLowRand; int userGuess; int xRand; int xtries = 7;
    
    //// Intro to game and input
    cout << "Hi Lo Game" << endl;
    
    cout << "Lets Play a game. I'm Thinking of a number. You have 7 tries " <<endl;
    
    
    
    cout << "Enter your Lo number : # ";
    
    while(!( cin >> userLowRand))
    {
        
    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << "Invalid input. Enter your Lo number again : # ";
  
        
    }
    
    
    
    cout << "Enter your Hi number : # ";
    
    while ( !(cin >> userMaxRand ))
    {
        
    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << "Invalid input. Enter your Hi number again : # ";
        
    }
    
    
    //USER GUESES RANDOM NUMBER HERE
    
    cout << "Guess a number between " << userLowRand << " and " << userMaxRand << " : " <<endl;
 
    srand(static_cast<int>(time(0)));
    
    xRand = rand() % userMaxRand + userLowRand;
    
    
    
    // check if guess correct

    
    while (cin >> userGuess)
    // note: At this point the program ends if the input is not an integer
    {
        
        if (userGuess < xRand) {
            cout << "You guessed too low, Try again: "<< endl;
        }
        
        
        else if (userGuess > xRand){
            cout << "you guessed too high, Try again: " <<endl;
        }
        
      
        cout << "Tries left : " << --xtries << endl;
        
      
        switch (xtries) {
            case 0:
                cout << "\nSorry, You Lost.The correct number was " << xRand << "." << endl;
                return game_rePlay();
        }
       
        
        if (userGuess == xRand) {
            cout << "Correct! You win!" <<endl;
            return game_rePlay();
            }
        
    }
    
    //END OF GAME
    
    return xRand;
    
    
}




int game_rePlay(){
    using namespace std;
    
    cout << "Do you want to play again ? (Y/N)" <<endl;
    
    char replay;
    
    
    while ((cin >> replay ))
        
    {
       
        
        if (      ((replay ==  'y') )   ||   ( (replay == 'Y') )             )
        
        {
            cout<< "\n\n";
            return  hi_Lo_Game();
           
        }
        
        else if (  ((replay ==  'n'))   ||   ((replay == 'N')) ){
            cout<< "\n\n";
            cout << "Thank You for playing." <<endl;
            return 0;
        }
        
        else{
            cout << "invalid input " <<endl;
        return game_rePlay();
        }
        
        
        return 0;
        
       
            
          
    }
    
    //END OF GAME_REPLAY
    
    return replay;
}
Last edited on
@Aceix I put in this code but the program still terminates when the input is not an integer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
while (cin >> userGuess)
    
    {
     //additional code to solve problem.

        if(cin.fail())
        {
             cout << "Invalid input"<<endl;
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
           
            }
        
     
        if (userGuess < xRand) {
            cout << "You guessed too low, Try again: "<< endl;
        }
        
Last edited on
Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;

int main(){
    unsigned x=0;

    do{
        cin.clear();
        cin.ignore();
        cout<<"Enter a postive integer: ";
        cin>>x;
    }while(cin.fail());

    cout<<"Good job";
    cout<<"\nYou entered: "<<x;
    cin.get();

    return 0;
}

floating point numbers will be converted to integers though.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
while (cin >> userGuess)
        // while input extraction is successful,    
    {
     //additional code to solve problem.
        // deal with unsucessful input extraction
        if(cin.fail())
        {
             cout << "Invalid input"<<endl;
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
           
            }
        
     
        if (userGuess < xRand) {
            cout << "You guessed too low, Try again: "<< endl;
        }



So.. the only way the body of the while loop is entered into is when an input extraction is successful. But, in the body of the loop you attempt to deal with a failed input extraction.

Something seems a little off there.
Last edited on
Topic archived. No new replies allowed.