Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

C programming - IF ELSE?

These lines of code are giving me a warm time, I want to scan the input for play but it is not working properly:

printf("\nDo you wish to play another game? Y[Yes]/ N[No]: ");

scanf("%c", &play);

getch();

if(play=='Y'||play=='y')

{

printf("Please enter the amount you wish to bet. (Note bet must be greater\nthan zero[0]): JA$ ");

scanf("%f", &bet);

}

else

if(play=='N'||play=='n')

{

printf("Your Overall Toatl Winnings for the %d times you played is: %.2f", count, OTW);

printf("Thank You for Playing... \nGoodbye...");

return 0;

}

else

{

printf("Invaild Selection. Try Again: ");

scanf("%c", &play);

}

Update:

Its not an error. it is simply that i am not given a chance to input yes or no... i.e. not reading the choice for play....

3 Answers

Relevance
  • Anonymous
    9 years ago

    sometime scanf has unexpected behaviour if you don't clean out the input stream

    sometimes there is a leftover char in the input stream that you don't want.

    use rewind (stdin) before EVERY scanf call and see if that helps

    printf("\nDo you wish to play another game? Y[Yes]/ N[No]: ");

    rewind (stdin);

    scanf("%c", &play);

    /* getch(); I dont think you need this if you do a rewind call before every scanf */

    if(play=='Y'||play=='y')

  • Ya-Hoo
    Lv 4
    9 years ago

    What is the error statement?

    Did you make sure to declare character play as a pointer above? Try adding a space in the condition.

    if (play == 'Y' || play == 'y')

  • 9 years ago

    this is so confusing, just use the iostream library, i could do this in half the lines much less complicated

Still have questions? Get your answers by asking now.