Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now 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.

Problem writing a code in C?

So i am a beginner in C i started learning C online just 1 week ago, I was writing this program it's not completely done yet.

In the code everything runs fine, while loop if statement, but "else" statement is not working properly it's printing the output twice.

Here's my code

#include<stdio.h>

#include<conio.h>

main()

{

char players;

printf("\nWelcome to Darts");

printf("\nYou want to play single player(s) or double player(d): ");

scanf("%c",&players);

while(players != 's' || players != 'd'){

if(players == 's'){

printf("You choosed single player");

break;

}

if(players == 'd'){

printf("You choosed double player");

break;

}

else{

printf("Type 's' for single player and 'd' for double player: ");

scanf("%c", &players);

}

}

getch();

}

** If user inputs 's' or 'd' it runs fine, but when user inputs something else than 's' or 'd' it prints

"Type 's' for single player and 'd' for double player: " Twice. This line is in else statement

6 Answers

Relevance
  • ?
    Lv 7
    7 years ago
    Favorite Answer

    you have a problem here:

    while(players != 's' || players != 'd'){

    players is always not equal to one of 's' or 'd'

    say players is 's' != 's' is false but != 'd' is true

    your statement is always true.

    and change

    scanf("%c", &players)

    to

    scanf(" %c", &players) // so it will ignore white space

  • 7 years ago

    Change each scanf format to " %c" with a space between the opening quote and the %c. That will cause scanf to ignore "whitespace" characters (spaces, tabs, or newlines) before storing the character.

    When the user types an x and then Enter, there are TWO characters placed in the input stream: the 'x' and the '\n' newline. Your program is reading both of those characters as separate inputs and responding to each of them. Skipping whitespace is an easy way to fix that.

    There will still be a problem if the user types "xyzzy" and then Enter. You'll see five responses to that one input. You might want to ignore that problem for now. The fixed program should work well enough. Later, you'll learn more about dealing with user input.

  • 7 years ago

    This is a common problem that people run into when using scanf to read a character. Scanf won't finish until you hit Enter, but since you are only reading a single character you get the character but the Enter remains in the buffer. When it hits the second scanf if gets the Enter that is still in the buffer so goes right through it.

    To fix this you are better off using the getchar function instead of scanf.

  • 7 years ago

    Use

    Scanf("%c\n", &Players);

    Will then collect the new line that's in the buffer

  • justme
    Lv 7
    7 years ago

    Your logic is all wrong. change it to this:

    while(players != 's' && players != 'S' && players != 'd' && players != 'D'){

    printf("Type 's' for single player and 'd' for double player: ");

    scanf("%c", &players);

    }

    if(players == 's' || players == 'S'){

    printf("You choosed single player");

    break;

    }

    else{ //if(players == 'd'){

    printf("You choosed double player");

    break;

    }

  • Anonymous
    7 years ago

    google.com

Still have questions? Get your answers by asking now.