-4

I made this for an assignment but even if I enter a valid input from the desired range I still get prompted. This is happening to both input prompts. I suspect the problem is in the while blocks of the setupGame function.

 #include <iostream>  
 using namespace std;
 bool setupGame(int numberRef, int triesRef);


int main(){
  cout<<"hello world";
  setupGame(4,4);
  cout<<"enough";
}


//SETUP GAME FUNCTION
bool setupGame(int numberRef, int triesRef) { 
 do { 
      cout << "ENTER A NUMBER BETWEEN 1 and 100" << endl; 
      cin >> numberRef;
cin.clear(); 
//your code here 
cout << "You entered: " << numberRef << endl; 
if (numberRef==-1) { 
   cout << "You do not want to set a number " << endl; 
   cout << "so you stopped the program" << endl;  
} 


else if(numberRef >=1 && numberRef <=100) 
   do { 
     cout << "ENTER TRIES BETWEEN 3 and 7" << endl; 
     cin >> triesRef;
     cin.clear();
     //cin.ignore( '\n');   
     cout<< "You entered: "<< triesRef<< endl; 
     if (triesRef==-1) { 
        cout << "You do not want to set tries. "; 
  cout << "so you stopped the program" << endl;
} else if(triesRef <= 3 && triesRef >= 7){
  cout<<"Number of tries should be between 3 and 7";
}

   } 

while(numberRef >=1 && numberRef <=100);{ 

   return true; 
}
} 
   while(triesRef >= 3 && triesRef <= 7);{
return true;
} }
GKE
  • 960
  • 8
  • 21
123456
  • 49
  • 6
  • Also add your input and output, expected and actual. – P.W Dec 31 '18 at 05:19
  • 3
    First of all, please try to reformat your code with some consistent and clear indentation. Secondly, what do you think `do { ... } while (...); { return true; }` would accomplish? That's an *unconditional* `return true;` being executed *after* the loop finish. – Some programmer dude Dec 31 '18 at 05:20
  • 1
    "*I suspect the problem is in the while blocks of the setupGame function.*" Why don't you find out if your suspicion is correct by testing the parts of your code separately? – eesiraed Dec 31 '18 at 20:32

1 Answers1

0

You're tangling yourself up with these nested loops. Your prompt keeps printing because the while loop:

while(numberRef >=1 && numberRef <=100)

is going to keep repeating it's preceding do block until you enter a value less than 1 or greater than 100, same goes for the last while loop as well.

I assume you're using cin.clear() to flush the previous input, if you are then stop it, that is not the purpose of cin.clear(). It's instead use to clear the error state of cin. Please thoroughly read up on it here.

Below is the code to achieve what you want, please observe how I implemented the while loops after each cin prompt to ensure that a valid character is entered.

#include <iostream> 
#include <fstream> 
using namespace std;

bool setupGame(int numberRef, int triesRef);

int main(){
  cout<<"hello world";
  setupGame(4,4);
  cout<<"enough";
}

//SETUP GAME FUNCTION
bool setupGame(int numberRef, int triesRef) { 
    cout << "ENTER A NUMBER BETWEEN 1 and 100" << endl; 
    cin >> numberRef;
        while((numberRef < 1 || numberRef > 100 || cin.fail()) && numberRef != -1) {
            cin.clear(); // Used to clear error state of cin
            cin.ignore(); // Might want to ignore the whole line
            cout<<"Number should be between 1 and 100, or -1 , please try again: ";
            cin>>numberRef;
        }  
    cout << "You entered: " << numberRef << endl; 
    if (numberRef==-1) { 
        cout << "You do not want to set a number " << endl; 
        cout << "so you stopped the program" << endl;  
        }

    if(numberRef >=1 && numberRef <=100) {
        cout << "ENTER TRIES BETWEEN 3 and 7" << endl; 
        cin >> triesRef; 
            while(triesRef < 3 || triesRef > 7 || cin.fail()) {
                cin.clear(); // Used to clear error state of cin
                cin.ignore(); // Might want to ignore the whole line
                cout<<"Tries should be between 3 and 7 , please try again: ";
                cin>>triesRef;
            } 
        cout<< "You entered: "<< triesRef<< endl;

    if (triesRef==-1) { 
        cout << "You do not want to set tries. "; 
        cout << "so you stopped the program" << endl;
        return false;
    } 
}
} 
GKE
  • 960
  • 8
  • 21
  • `while((numberRef < 1 || numberRef > 100 || cin.fail()) && numberRef != -1)` is a little broken. `cin.fail()` can be true (the read failed) and `numberRef != -1` could still allow the loop to be exit even though the -1 in `numberRef` is bogus. If `cin` is errored, you always want to repeat. Nothing good will come of looking at `numberRef`. – user4581301 Dec 31 '18 at 07:42
  • @user4581301 You're absolutely right, an input of `-1a` will exit the loop as if it's just `-1`, for it to be truly fool-proof it would need a lot more work, however this is a cut and dry solution intended to show the OP how to integrate while loops for input checking, are there better ways? Absolutely. Would I do it different? Yes. However based on the OP code and approach I don't take him/her as an expert on the language, a complex and in depth answer wouldn't explain his/her current errors. – GKE Dec 31 '18 at 09:46
  • The likes of -*1a* is not the concern. I worry about `numberRef` being -1 after a completely failed read.*-1a* is valid. The program read *-1* and left *a* in the stream. *kartoffel* on the other hand fails utterly and if `numberRef` wound up -1 for whatever reason, then `while((-1 < 1 || -1 > 100 || cin.fail()) && -1 != -1)` is `while((true || shorted || shorted) && false)` The loop existed on a failed read. check `cin`'s state first, then and only then can you trust the value in `numberRef`. – user4581301 Dec 31 '18 at 23:27