1

Hello I am implementing a very basic Random guessing game using C++ in Eclipse IDE.

Everything is working except one condition to check whether the user input is from 0 to 9 or not and if it is not then the message should display.

What is wrong please check my code:

#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;

int main(){

    puts ("\n\n\t\t***** Welcome to the guessing game *****");
    puts ("\n\t You've eight tries to guess a randomly generated number");
    puts ("\n\t\t\tNumber is within 1 to 9");
    puts ("\n\t -------------------------------------------------------");
    puts ("\n\t -------------------------------------------------------");

    int j;
    char l;

    reverse:
    srand (time(NULL));                         // required to keep updating randomly generated number with time
    int i = rand() % 10;                        // generate random number from 0 to (10 - 1)

    for (int k = 0; k < 8; k++){                // for the eight tries provided to user

        again:
        cout << "\n\r\t guess any number : ";
        cin >> j;                               // input by the user

        if(isdigit(j) == false){                // check whether the input no. is indeed a no. or not?
            cout << "\n\r\t Please Enter Numbers between 0 and 9 only" << endl;
            goto again;                         // if no then goto again:
        }

        else if (j == i){
            cout << "\n\r\t congratulation, You win \a";
            puts ("\n\t -------------------------------------------------------");
            cout << "\n\t Press any key to start the game again or 0 to Exit";
            cin >> l;
            if(l != '0') goto reverse;      // start the game again from reverse:
            goto target;
        }

        else if (k == 7){
            cout << "\n\r\t Sorry you lose! \a";
            puts ("\n\t -------------------------------------------------------");
            cout << "\n\t Press any key to start the game again or 0 to Exit";
            cin >> l;
            if(l != '0') goto reverse;          // start the game again from reverse:
            goto target;
        }

        else {                                  // evaluate remaining chances and start over
            cout << endl << "\r\t incorrect! " << 7-k << " chances remaining" << endl;
        }
    }

    target:
    puts ("\n\t -------------------------------------------------------");
    cout << "\n\n\n\r\t\t\t * GAME OVER * \a";  // console will get close
//  while(1);                                   // console will remain open
    return 0;
}

I have already checked these: C number guessing game with isdigit() verification Random guessing game - bug http://www.cplusplus.com/reference/cctype/isdigit/

Please help

Community
  • 1
  • 1

1 Answers1

0

j is an int, isdigit works on char. It's very important to pay attention to the types of your variables.

What you want is actually very simple

if (j < 0 || j > 9){
    cout << "\n\r\t Please Enter Numbers between 0 and 9 only" << endl;

BTW don't use goto, you'll lose marks for that. Write another loop, loop until the input is valid.

john
  • 85,011
  • 4
  • 57
  • 81
  • Thank you for your reply sir, I've already tried "if (j < 0 || j > 9)" it not works – Jigar4Electronics Oct 08 '13 at 06:18
  • @Jigar4Electronics I just tried it and it worked for me. What happens when you try it? – john Oct 08 '13 at 06:23
  • Problem with "if (j < 0 || j > 9)" It detects the number less than 0 and greater then 9 and indicate them as a mistake by showing the message BUT It doesn't detects the special characters or alphabets... I want to restrict user by giving all irrelevant input.. only 0 to 9 should be accept. – Jigar4Electronics Oct 08 '13 at 06:25
  • ok the problem is solved.... as per your first answer...I've just changed the type of j to char... and it works... Thank you – Jigar4Electronics Oct 08 '13 at 06:28
  • BTW it is written that the prototype of isdigit() is " int isdigit (int c) " then why it works??? char j is not int !! How you get that isdigit() is works with char??? where is it written, please share the link – Jigar4Electronics Oct 08 '13 at 06:33
  • @Jigar4Electronics That is confusing, and it would take a long time to explain it properly. But look at this link http://www.cplusplus.com/reference/cctype/isdigit/, see at the top it says 'Check if *character* is decimal digit'. See also later it says '*Character* to be checked, casted to an int, or EOF.' That's the reason, isdigit works with characters or the special integer value `EOF`. Because it has to work with EOF any character is casted to an integer before calling isdigit. – john Oct 08 '13 at 06:37