0

This is a random guessing game. The game Secret Numbers is from min to max that are input by the user. The guesser asks guess the secret number and at the end it is supposed to be asked if they would like to play again. There also has to be multiple options for print outs if the guesser is too high or low. I am new to programming and this has got me stumped. Any help would be appreciated.

Now i can't get the user to cin again after type alphabet in the do while loop. So, any advice on the best route to do that would be appreciated.

cout<<"\nEnter ur amount to play game: ";
cin>> amount;
do{
    cout<<"\n\n"<< name<< ", What is ur betting amount? ";
    cin>> bet_amount;

    if(bet_amount > amount)
    cout << "Your betting amount is more than your current balance!\n";

    else if(!(cin>> bet_amount)){
            cin.clear();
            cin.ignore();
        }

}while(bet_amount > amount || !(cin>> bet_amount));

while(1){
// Read in guess
cout<< "\n\nEnter a guess to bet: ";
cin >> guess;
...
AvIx
  • 11
  • 6
  • Why are you using cin>> bet_amount) in while(bet_amount > amount && !(cin>> bet_amount)); – Sorcrer Jul 31 '14 at 05:01
  • @Sorcrer Hi, to make sure the user input numerical, not alphabet – AvIx Jul 31 '14 at 05:28
  • can u specify how bet_amount > amount && !(cin>> bet_amount) will check for entered value is numerical?? – Sorcrer Jul 31 '14 at 05:38
  • @Sorcrer Here is the sample output: http://i59.tinypic.com/jb35o9.png – AvIx Jul 31 '14 at 05:44
  • The fail bit on a stream is sticky. You'll want to use the `ignore` member function to throw away the input that wasn't a valid number, and then the `clear` member function to reset the fail bit. – Ben Voigt Jul 31 '14 at 06:16
  • In your pgm , how many times the user can guess? I assume the flow is like.... 1.enter betting amount(above min level) 2.The secret no: range can be specified by the user. 3.user given 1chance to specify the secret no: 4.ask user want to continue or not – Sorcrer Jul 31 '14 at 06:20
  • @Sorcrer I have updated my code, but it seem still can't work it – AvIx Jul 31 '14 at 06:48

1 Answers1

0

Try the following changes-

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

int main()
{
    int amount,bet_amount;
    string name;

    cout << "Enter your name and betting amount!\n";
    cin >> name >> amount;
    cout << "Your current balance is " << name << " " << amount << endl;
    cout << "\n *** To stop play press CTRL + c *** \n\n";

    while(1){
            cout << "Enter the amount to bet! : ";
            cin >> bet_amount;

            if(bet_amount == 0){
                    cin.clear(); // takes care of resetting the fail bits
                    cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n'); //removes any wrong input left in the stream
                    cout << "Please enter the numerical value(Amount)!\n";
                    continue;
            }
            if(bet_amount > amount)
                    cout << "Your betting amount is more than current balance!\n";
            else if(bet_amount == amount)
                    cout << "Your betting amount is equal to current balance!\n";
            else
                    cout << "Your betting amount is less than current balance!\n";
    }
    return 0;
}

With out cin.clear and cin.ignore, cin in while(1) can't scan the input from the user next time.

Sample output-

root@ubuntu:~/c/basics# ./a.out 
Enter your name and betting amount!
ABC
50
Your current balance is ABC 50

 To stop play press CTRL + c  

Enter the amount to bet! : 25
Your betting amount is less than current balance!
Enter the amount to bet! : w
Please enter the numerical value(Amount)!
Enter the amount to bet! : q
Please enter the numerical value(Amount)!
Enter the amount to bet! : 50
Your betting amount is equal to current balance!
Enter the amount to bet! : 75
Your betting amount is more than current balance!
Enter the amount to bet! : ^C // when you press ctrl+c program terminates!
Sathish
  • 3,740
  • 1
  • 17
  • 28
  • Hi, it's not guess the amount, it's to make sure it will ask user to cin again if key in character like 'j'.. – AvIx Jul 31 '14 at 04:51
  • this part program is make sure the user input numerical, not alphabet. if the user key in alphabet, it will ask user to key in again, till the input is numerical – AvIx Jul 31 '14 at 05:34
  • @AvIx Try the new program! – Sathish Jul 31 '14 at 06:53