Question:
Problem writing a code in C?
Osho
2014-04-13 05:04:38 UTC
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
#include

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
Six answers:
?
2014-04-13 08:53:41 UTC
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
husoski
2014-04-13 05:28:52 UTC
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.
Daniel B
2014-04-13 05:30:55 UTC
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.
amania_r
2014-04-13 07:09:59 UTC
Use

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

Will then collect the new line that's in the buffer
justme
2014-04-13 05:32:17 UTC
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;

}
2014-04-13 08:17:51 UTC
google.com


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...