Reputation: 37
How to print only one time the statement if a few wrong input is key in: Eg: if key in kkk, only will print one statement instead of the example below. sample output:
Do you wish to try again (Type Y to continue Q to quit:kkk
Error: Invalid choice
Do you wish to try again (Type Y to continue Q to quit:Error: Invalid choice
Do you wish to try again (Type Y to continue Q to quit:Error: Invalid choice
Do you wish to try again (Type Y to continue Q to quit:
valid=0;
while (valid==0)
{
printf("\nDo you wish to try again (Type Y to continue Q to quit:");
// print statement request for input
scanf(" %c", &choice); // get user input
choice = toupper(choice);
if((choice == 'Y') || (choice == 'Q'))
valid= 1;
else
printf("Error: Invalid choice\n"); // statement
}
Upvotes: 3
Views: 3120
Reputation: 16213
Your code is checking 1 char per time due to scanf
format specifier "%c". That means your scanf
exits, the first time, after the enter key and then, on next calls, returns immediately for all chars entered by user.
To avoid this behavior you can take the whole input in a time.
You can do something like:
#include <stdio.h>
#include <ctype.h>
int main (void)
{
int valid=0;
char choice[32];
size_t i=0;
int data;
while (valid==0)
{
printf("\nDo you wish to try again (Type Y to continue Q to quit):"); // print statement request for input
fgets(choice, sizeof(choice), stdin);
i=0;
while ((choice[i] != '\0') && (valid == 0))
{
data = toupper((unsigned char)(choice[i]));
if((data == 'Y') || (data == 'Q'))
{
valid= 1;
}
i++;
}
if (valid == 0)
printf("Error: Invalid choice\n"); // statement
}
}
As you can see I used fgets to get input from user into a buffer, to store the whole input (multi chars) in one shot.
After that you can loop throung input chars and check if one of those chars is a correct selection
Upvotes: 1
Reputation: 5457
How to print only one time the statement if a few wrong input
One way of doing it is to scan the input into string and compare the first element against 'Y' and 'Q' as other answers have shown you how to do.
However, If you don't want to use strings then, second way without using strings is to consume the additional characters after scanning the input using a loop this way:
int valid=0;
char choice;
while (valid==0)
{
printf("\nDo you wish to try again (Type Y to continue Q to quit:");
scanf("%c", &choice);
// remove space before format specifier as you are consuming white spaces in the below loop
//consuming additional characters mechanism:
int consume_char; //to store consumed character (getchar() returns int)
//the below loop consumes all additional characters
while( ( (consume_char = getchar()) != '\n' ) && (consume_char != EOF));
choice = (char) toupper(choice);
if((choice == 'Y') || (choice == 'Q')) valid= 1;
else printf("Error: Invalid choice\n"); // statement
}
return 0;
Upvotes: 1
Reputation: 2221
Your scanf
is currently looking for a single character (%c
) so when you input "kkk" and press enter it receives input four times: 'k', 'k', 'k', and '\n'
Change it to use %s
instead and then only check the first character of the string you get.
Or as said in the comments, use fgets
to get a string and deal with it as mentioned above.
Upvotes: 4