-2

I am making Bubble game in c where randomly some numbers are generating on the console window and user need to enter that numbers appearing on the screen or console window and I need to compare that user input and random generated number and make that number invisible from the screen.

I have used rand() function in c and for user input I have used getch() function and i have tried every type conversion but not getting the solution yet.

void main() {
char temp1 = 0;
char temp = 0;
temp1 = rand()%10;
   temp = getch();
   if(temp == temp1)
   printf("ok");
  }

I want to compare randomly generated value and user input at run time and input given by the user by seeing on console's number is equal i want to exit.

1 Answers1

0

A digit character has a character code value '0' to '9' (in the ASCII character set 0x30 to 0x39 or 48 to 57 decimal), while rand() % 10 has integer value 0 to 9.

By adding the character code '0' (48) to the random number you will generate a random character code '0' to '9'.

int main()
{
    char temp1 = rand() % 10 + '0' ;
    char temp = getch() ;

    if( temp == temp1 )
    {
        printf("ok");
    }

    return 0 ;
}

Alternatively if you subtract '0' from the input character you will transform digit characters to their corresponding 0 to 9 integer values (and other characters to some other value).

Clifford
  • 88,407
  • 13
  • 85
  • 165