I'm using a while loop per below:
do {
scanf("%c", &turnChoice);
if (turnChoice == 'r')
{
invalidSelection = false;
}
else if (turnChoice == 'h')
{
invalidSelection = false;
}
else
{
printf("Invalid input.\n");
}
} while (invalidSelection == true);
However, before stopping for the user input, it runs the full loop once (so it displays "Invalid input" and then asks the user for a letter). What am I doing wrong?
My guess is that you have some input before the code you show, some input that you end with a newline, right?
The problem here is that the newline is still in the input-buffer when you call scanf to read a character, so that newline is what you read and get.
There is a very simple "trick" to tell scanf to read and discard leading white-space (like newlines), and that is to prepend a single space to the format string, so try e.g.
scanf(" %c", &turnChoice);
// ^
// |
// Note space here
After take char input, we need to take '\n' char which is giver by pressing the enter button
char turnChoice;
bool invalidSelection = true;
do {
scanf("%c", &turnChoice);// if you want to give leading space then use scanf(" %c",&turnChoice);
getchar();
if (turnChoice == 'r')
{
invalidSelection = false;
}
else if (turnChoice == 'h')
{
invalidSelection = false;
}
else
{
printf("Invalid input.\n");
}
} while (invalidSelection == true);
This might help, when you scanf put "%*c" after your first character to ignore the enter key that you hit after entering the input. Code is as follows:
do {
scanf("%c%*c", &turnChoice);
if (turnChoice == 'r')
{
invalidSelection = false;
}
else if (turnChoice == 'h')
{
invalidSelection = false;
}
else
{
printf("Invalid input.\n");
}
} while (invalidSelection == true);
Related
I have a do-while loop, and I want if I press ENTER key, the progress will continue, but q will finish the program. It is not working as the program will end straight away and does not wait for the user to enter the key.
Code below is my main code.
void displayGrid() {
bool progress = true;
printf("%s", "input round for round mode, moves for move mode");
scanf("%s", input);
toLowerCase(input);
if (strcmp(input, "round") == 0) {
do {
printf("Enter key ENTER to continue,Q for quit \n");
bool qoc = quitOrContinue();
if (qoc) {
} else if (!qoc) {
progress = false;
}
} while (progress);
}
}
This is my code for checking enter and q key:
bool quitOrContinue() {
if (kbhit()) {
char click = fgetc(stdin);
while (getchar() != '\n');
if (click == 0x0A) {
return true;
} else if (click == 'q') {
return false;
}
}
}
You do not need three functions to read a char from stdin.
Here's some psuedo-code to illustrate how to read one char. (I couldn't test it, so there may be some bugs in it).
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
static bool quitOrContinue(void)
{
int click = fgetc(stdin);
if (click == 0x0A) {
return true;
} else if (click == 'q') {
return false;
}
/* Returns false in case of any other character */
return false;
}
int main(void)
{
bool condition = false;
do {
printf("Hello World\n");
printf("Enter q to quit or ENTER to continue.\n");
condition = quitOrContinue();
} while (condition);
return EXIT_SUCCESS;
}
You do not need the progress variable.
while (getchar() != '\n');
serves no purpose in your code, unless you're trying to flush stdin.
regarding:
printf("%s", "input round for round mode, moves for move mode");
You could use:
printf("input round for round mode, moves for move mode");
regarding:
scanf("%s", input);
What happens when one inputs more than size characters?
Limit length:
scanf("%6s", input);
So, I'm a C and programming newcomer and I'm trying to write a function to abstract y/n choices in a program that uses a lot of them, as of now I have it like this:
void chooser (char *choice)
{
while (1)
{
*choice='\0';
*choice=getchar();
while(getchar()!='\n');
if(*choice == 'y' || *choice == 'n')
break;
}
}
The second getchar consumes input in excess in the stdin so that it shouldn't be susceptible to the user typing gibberish.
The only problem is that if I type "ywhatever" or "nwhatever" the first getchar still captures "y" and "n" and passes it to the main.
I would like to have it so that the user has to type "y" or "n" and nothing else.
I could try ditching getchar and switching to fgets for capturing and sscanf for parsing, which is how I prefer to get strings, but, dunno, it's just a single character, I would prefer not to overcomplicate things.
Is there a way to scan stdin after user input to see if it contains more than one alphabetic character?
EDIT:
In the end, I went for this:
int chooser ()
{
char buffer[MAXIN];
while (1)
{
printf("y/n: ");
if (fgets(buffer, sizeof(buffer), stdin)!=NULL)
{
if (buffer[0] == 'y' || buffer[0] == 'n')
{
if (buffer[1]=='\n')
{
break;
}
else
{
while(getchar()!='\n');
}
}
}
}
if (buffer[0]=='y')
{
return 1;
}
if (buffer[0]=='n')
{
return 0;
}
}
It seems to be doing exactly what I need it to do, is there something else I should adjust? Does a fgets from the standard input need a if(fgets(...)!=NULL) check?
What's wrong with this solution:
#include <stdio.h>
void chooser(char *choice)
{
char buffer[200];
while (1)
{
fgets(buffer, sizeof(buffer), stdin);
if (buffer[0] == 'y' || buffer[0] == 'n')
break;
}
*choice = buffer[0];
}
int main(int argc, char **argv)
{
while (1)
{
char yn;
chooser(&yn);
if (yn == 'y' || yn == 'n')
printf("User choice: %c\n", yn);
}
}
That's not exactly "overcomplicated"
There is still room for improvement, e.g the EOF condition is not handled at all here.
Example of execution:
abc
def
y
User choice: y
n
User choice: n
yes
User choice: y
no
User choice: n
noooo
User choice: n
I'm facing a problem with my code of a simple login program. The problem I'm facing is when I use a switch case or if statement for the option of logging in as an Admin or a User, the input for username is skipped and goes directly to password, and no matter what I type it gives me my error message. Instead, I want it to receive my username first then the password. It works fine on its own if there is only code for either Admin OR User, only one but not when there are more than one. Please help. Note: I'm using the same functions for both admin and user just to check if it works. The picture shows the output.I'm a C newbie, so minimal jargon perhaps? Code as follows:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
char username[18];
char pass[16];
void arequest()
{
printf("\nPlease Enter username:");
fflush(stdin);
gets(username);
printf("\nPlease Enter Password:");
fflush(stdin);
gets(pass);
}
void averify()
{
if (strcmp(username, "admin") == 0)
{
if (strcmp(pass, "apass") == 0)
{
printf("Successful Login");
_getch();
}
else
{
printf("Invalid Password");
_getch;
}
}
else
{
printf("Invalid Username");
_getch();
}
}
int choice;
int main()
{
printf("Welcome to Railway Reservation System");
printf("\n1.Admin \n2.User");
printf("\nPlease Enter your selection:");
scanf_s("%d", &choice);
if (choice == 1)
{
arequest();
averify();
}
else if (choice == 2)
{
arequest();
averify();
}
else
{
printf("Invalid Choice");
_getch();
return main;
}
return 1;
}
output
You are flushing the input stream with fflush(). fflush(stdin) is undefined behavior in most cases, and is at best implementation-dependent. To clear the extra characters from the input stream, consider writing a little function like this:
void clear_stream(void)
{
int c;
while ((c = _getch()) != '\n' && c != EOF)
continue;
}
Remove the calls to fflush(). You do not need to clear the stream after gets(username) since gets() discards the newline. Add a call to clear_stream() after this line in main():
scanf_s("%d", &choice);
There may be extra characters, including a newline, left in the input stream after the call to scanf_s(), and these need to be removed before trying to read user input again. In some cases scanf()_s (and scanf()) will skip over initial whitespaces in reading input, but _getch() and getchar() will not. This illustrates one of the dangers of using scanf().
printf("\nPlease Enter your selection:");
scanf("%d", &choice);
clear_stream();
Also, gets() is considered so dangerous that there is never a reason to use it for anything at all. Use fgets() instead. fgets() does keep the newline, where gets() discards it, so I often write my own version of gets() using fgets() that is safe:
char * s_gets(char *st, int n)
{
char *ret;
int ch;
ret = fgets(st, n, stdin);
if (ret) {
while (*st != '\n' && *st != '\0')
++st;
if (*st)
*st = '\0';
else {
while ((ch = getchar()) != '\n' && ch != EOF)
continue; // discard extra characters
}
}
return ret;
}
The library conio.h is nonstandard, as are the functions _getch() and scanf_s(). You should use the stdio.h functions getchar() and scanf(). The value returned by scanf() is the number of successful assignments, and you should check this to be sure that the input is as expected. In your program, if the user enters a letter at the selection prompt, no assignment is made, and the value of choice remains uninitialized. The code continues without handling this problem. choice could be initialized to some reasonable value, such as int choice = -1;. Alternatively, you can check the return value from scanf() to see if an assignment was made, and proceed accordingly.
I noticed that you are returning 1 from main(). You should return 0 unless there is an error. And, I see that you return main in the event of an invalid choice. Maybe you meant to return 1 here? And it appears that you have forgotten to #include <string.h> for the strcmp() function.
Finally, I don't understand why username, pass, and choice are global variables. This is a bad practice. These should be declared in main() and passed to functions as needed. It would be a good idea to #define the global constants MAXNAME and MAXPASS instead of hard-coding the array dimensions.
I didn't intend this to be a full-scale code review when I started, but that is what it turned into. Here is a revised version of your program that implements the suggested changes:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXNAME 18
#define MAXPASS 16
void clear_stream(void)
{
int c;
while ((c = getchar()) != '\n' && c != EOF)
continue;
}
char * s_gets(char *st, int n)
{
char *ret;
int ch;
ret = fgets(st, n, stdin);
if (ret) {
while (*st != '\n' && *st != '\0')
++st;
if (*st)
*st = '\0';
else {
while ((ch = getchar()) != '\n' && ch != EOF)
continue; // discard extra characters
}
}
return ret;
}
void arequest(char username[MAXNAME], char pass[MAXPASS])
{
printf("\nPlease Enter username:");
s_gets(username, MAXNAME);
printf("\nPlease Enter Password:");
s_gets(pass, MAXPASS);
}
void averify(char username[MAXNAME], char pass[MAXPASS])
{
if (strcmp(username, "admin") == 0)
{
if (strcmp(pass, "apass") == 0)
{
printf("Successful Login");
getchar();
}
else
{
printf("Invalid Password");
getchar();
}
}
else
{
printf("Invalid Username");
getchar();
}
}
int main(void)
{
char username[MAXNAME];
char pass[MAXPASS];
int choice;
printf("Welcome to Railway Reservation System");
printf("\n1.Admin \n2.User");
printf("\nPlease Enter your selection: ");
if (scanf("%d", &choice) == 1) {
clear_stream();
if (choice == 1)
{
arequest(username, pass);
averify(username, pass);
}
else if (choice == 2)
{
arequest(username, pass);
averify(username, pass);
}
else
{
printf("Invalid Choice: %d\n", choice);
getchar();
return 1;
}
} else {
clear_stream(); // stream has not yet been cleared
printf("Nonnumeric input");
getchar();
}
return 0;
}
EDIT
The OP mentioned in the comments that scanf() was causing problems in Visual Studio. Apparently Visual Studio tries to force the use of scanf_s(). The issue with this function is not that it is inherently bad, just that it is nonstandard. One solution might be to use the s_gets() function already added to the code to read the user selection into a character buffer, and then to use sscanf() to extract input. This has an advantage in that there is no need to call the clear_stream() function after s_gets(), because s_gets() cleans up after itself, so the clear_stream() function could now be removed altogether from the program. This can be accomplished with only a small change in main():
char choice_buffer[10];
int choice;
...
if (s_gets(choice_buffer, sizeof(choice_buffer)) &&
sscanf(choice_buffer, "%d", &choice) == 1) {
if (choice == 1)
...
} else {
printf("Nonnumeric input");
getchar();
}
s_gets() reads up to the first 9 characters (in this case) of a line of user input into choice_buffer, which is an array that will hold chars (there is more space in choice_buffer than is needed to hold a single digit choice and a '\0'). If there is an error, s_gets() returns a NULL pointer, otherwise a pointer to the first char of choice_buffer is returned. If the return value of s_gets() was non-NULL, then sscanf() assigns the first int stored in the buffer to choice. If no int is found in the string, sscanf() returns a value of 0, failing the conditional test.
So the code below is a small part of a larger program, and by itself works just fine:
int main(){
char ansr;
scanf("%c", &ansr);
while (ansr != 'y' && ansr != 'n'){
printf("\n\tInvalid answer, Please try again.");
while(getchar() != '\n');
scanf("%c", &ansr);
}
if ( ansr == 'y') {
sleep(1);
printf("\n\tYou've been warned.......\n\n");
sleep(3);
}
else if ( ansr == 'n') {
printf("\n\tGoodbye then.\n\n");
}
return 0;
}
as you can see, any word the user inputs that doesn't start with a y or n will be rejected until there is a right answer. How can I also include words that are longer than the characters 'y' and 'n', since as it is any character that starts with a y or n gives results for yes or no?
For such use cases, don't use scanf to read input. Use fgets to read the text line by line and process each line with whatever logic makes sense.
int main()
{
char answer[LINE_LENGTH]; // #define LINE_LENGTH to a sensible value.
while ( fgets(answer, LINE_LENGTH, stdin) != NULL )
{
// Trim the line of the newline character.
int len = strlen(answer);
if ( answer[len-1] == '\n' )
{
answer[len-1] = '\0';
}
if ( strcmp(answer, "y") == 0 ||
strcmp(answer, "n") == 0 )
{
break;
}
printf("\n\tInvalid answer, Please try again.");
}
if ( strcmp(answer, "y") == 0)
{
sleep(1);
printf("\n\tYou've been warned.......\n\n");
sleep(3);
}
else if ( strcmp(answer, "n") == 0 )
{
printf("\n\tGoodbye then.\n\n");
}
return 0;
}
Use fgets instead of getchar, to retrieve more than one character.
#include <stdio.h>
int main(int argc, const char * argv[]) {
//run...
printf("----------------CALC--------------\n");
printf("(1) Type in a letter to get a secret message: ");
int validSecret = 0;
char secretLetter;
while(validSecret == 0){
scanf("%c",&secretLetter);
if(secretLetter == 'b'){
printf("B\n");
}
else if (secretLetter == 'r'){
printf("R\n");
}
else if (secretLetter == 'k'){
printf("K\n");
}
else {
printf("Game over\n");
validSecret = 1;
}
}
return 0;
}
If I enter a b the if statement executes correctly and prints a B and a new line but also prints game over. It's running the if and the else... This makes no sense.
The newline you've entered (corresponding to a '\n' character) doesn't match any of the other conditions; of course your code will take the else branch because of it.
Is it possible that stdin could reach EOF? Your code ignores the return value, and so wouldn't be able to detect such scenarios... It should probably look more like:
while (scanf("%c", &secretLetter) == 1) {
if (secretLetter == 'b') {
printf("B\n");
}
else if (secretLetter == 'r'){
printf("R\n");
}
else if (secretLetter == 'k'){
printf("K\n");
}
else if (secretLetter != '\n') {
printf("Game over\n");
break;
}
}
Notice how I converted your else to an else if (secretLetter != '\n')? This should prevent "Game over" from being printed when secretLetter is \n, solving the first problem...
Also notice how I made the terminal condition of the loop match the failure of scanf? This should prevent the loop from continuing when stdin reaches EOF. You might want to move the printf("Game over\n"); to outside of the loop to account for this.
You don't need an intermediate variable in place of break here; you should avoid unnecessary clutter at all cost.
After you read input, you need to flush the input buffer, like so
void flush()
{
while (getchar() != '\n')
;
}
This is so that all characters up to, and including, the newline are consumed. The reason is that scanf stops reading as soon as it sees the '\n', so it leaves it in the inpu buffer. Also, never flush wih fflush(stdin), because that isn't portable.
This is because the '\n' input by pressing Enter is also consumed by scanf(). You can use
scanf(" %c", &secretLetter);
to discard these whitespaces (including ' ', '\t', '\n' and something else).