0

I wrote a C program that is suppose to be a game called 'knifestonepaper'. In it knife defeats Paper, Stone defeats knife and Paper defeats Stone. I wanted to make it user-friendly by prompting the user to enter y(es) or n(o) at certain points.

However, when I use scanf("%c",choice) to store user's choices the program acts 'funny' or automatically loops but works perfectly when I use getchar(). I would like to know is it so. The game code is below.

The points are:

scanf("%c",&mainexit);
/*getchar();
mainexit=getchar();*/

and

scanf("%c",&exit);
/*getchar();
exit=getchar();*/  

The game:

#include <stdio.h>
#include <stdlib.h>

int main()
{
char mainexit;
do
{
int choice;
char exit;
printf("Welcome To StoneKnifePaper!!!!! \n");
a:
printf("Pick One Option.(Enter integers)\n");
b:
printf(" 1.Stone. \n 2.Knife. \n 3.Paper. \n 4.Exit. \n GoodLuck! :) \n");
scanf("%d",&choice);
if(choice <1 || choice>4)
{
    printf("Wrong Choice!!! Try Again. \n");
    goto b;
}
else if ( choice == 4)
{
    c:
    printf("Are You Sure You Want To Quit? \n [y/n] \n");
    scanf("%c",&exit);
    /*getchar();
    exit=getchar();*/      
    if (exit == 'Y' || exit == 'y')
    {
    return 0;
    }
    else if (exit == 'N' || exit == 'n')
    {
        goto a;
    }
    else if (exit != 'N' || exit!= 'n' || exit != 'Y' || exit != 'y')
    {
        printf("Wrong Entry. (Please Enter 'y' or 'n') \n");
        goto c;
    }
}
else if (choice >= 1 && choice <=3)
{
    int randomnumber = rand()%3 +1;
    if(choice==1 && randomnumber == 2)
    {
        printf("Congratulations!! Player has Won!! Stone Blunts Knife.");
    }
    else  if(choice==2 && randomnumber == 3)
    {
        printf("Congratulations!! Player has Won!! Knife Cuts Paper.");
    }
    else  if(choice==3 && randomnumber == 1)
    {
        printf("Congratulations!! Player has Won!! Paper wraps Stone.");
    }
    else  if(choice==2 && randomnumber == 1)
    {
        printf("Computer has won!! Stone Blunts Knife.");
    }
    else  if(choice==3 && randomnumber == 2)
    {
        printf("Computer has won!! Knife Cuts Paper.");
    }
    else  if(choice==1 && randomnumber == 3)
    {
        printf("Computer has won!! Paper Wraps Stone.");
    }
    else  if(choice == randomnumber)
    {
        printf("It's a draw!");
    }
}
else
{
    printf("Wrong Choice!!! Try Again. \n");
    goto b;
}
printf("\n Play Game Again? Try Your luck. \n [y/n] ");
scanf("%c",&mainexit);
/*getchar();
mainexit=getchar();*/
}while(mainexit== 'Y'|| 'y');

return 0;
}
  • put a getchar(); after every scanf() call. scanf() leaves a \n in the input buffer (stdin) which is then read by the next scanf()/getchar()/gets/"whatever reads from stdin" – robin.koch Dec 04 '14 at 10:10
  • These links might help you: http://stackoverflow.com/questions/2507082/getc-vs-getchar-vs-scanf-for-reading-a-character-from-stdin or http://stackoverflow.com/questions/3640604/c-getchar-vs-scanf or http://stackoverflow.com/questions/2482634/is-getchar-equivalent-to-scanfc-a – ctwheels Dec 04 '14 at 10:14
  • You're comparing one `scanf("%c", ...)` call with two `getchar` calls? – mafso Dec 04 '14 at 10:44
  • @mafso Not exactly. In my code i have commented snippets of code with getchar(). That means I am using scanf(). Run it and it will run differently to when you comment the scanf() part and use the getchar(), so i want to know why is it so. – Hammerton Mwawuda Dec 04 '14 at 11:43
  • There are two `getchar` calls commented out per each `scanf("%c", ...)` call. If you call `scanf` like this twice, you get almost the same behaviour. – mafso Dec 04 '14 at 19:27

1 Answers1

0

scanf gets the input but skip the leading white space characters like spaces,newline tab space etc.. So the new line after the first input is stored in the buffer so the next scanf get the input of that.

So Avoid this you can use the space before the scanf control string. Like this scanf(" %c",&exit);

Karthikeyan.R.S
  • 3,991
  • 1
  • 19
  • 31
  • Thanks. Same explanation like in one of the comments above. I tried the space before %c and it worked. So what does this space do? – Hammerton Mwawuda Dec 04 '14 at 12:02
  • Space is considered as a white space character. So Giving that before the control string is matching the first white space character and ignores that then get the next character is a corresponding input. – Karthikeyan.R.S Dec 04 '14 at 12:15
  • The second paragraph and the last comment seem to contradict the first paragraph (which is wrong). Typo? – mafso Dec 04 '14 at 19:28