Dealing with "non-numerical characters" when expecting an integer input - c

I need a help with the following function, it's expecting an integer input; and when I insert something like "F" (non-numerical characters ), the program gets stuck, it doesn't show any output or let me insert more inputs.
how can this be fixed?
int input_legality(int game_board[FIELD_ROWS][FIELD_COLS])
{
int input=0;
while(1)
{
if(scanf("%d", &input)==1)
{
if(input==DOWN || input==LEFT || input==RIGHT || input==UP)
{
return input;
}
else
if(input==EXIT)
{
printf("\n program exited by user \n");
return 1;
}
else
if(input==PRINT)
{
printField(game_board);
continue;
}
else
{
fprintf(stderr,"your step is undefined, please try another one\n");
continue;
}
}
}
return 0;
}

It seems that "F" is left in stdin if scanf() does not read an integer.
One answer would be to scan a string if an integer is not detected...
Try to add something like
char bla[256];
scanf("%s",bla);
At the end of the while loop (or in case scanf("%d) failed)
Here is a basic "main" code :
#include <stdio.h>
#define DOWN 0
#define UP 1
#define LEFT 2
#define RIGHT 3
#define EXIT 4
#define PRINT 5
int input_legality()
{
int input=0;
while(1)
{
if(scanf("%d", &input)==1)
{
if(input==DOWN || input==LEFT || input==RIGHT || input==UP)
{
return input;
}
else{
if(input==EXIT)
{
printf("\n program exited by user \n");
return 1;
}
else{
if(input==PRINT)
{
printf("ble %d \n",input);
continue;
}
else
{
fprintf(stderr,"your step is undefined, please try another one\n");
continue;
}
}
}
}
//while(getchar() != EOF);
//fflush(stdin);
char bla[256];
scanf("%s", bla);
}
return 0;
}
int main ( int argc , char * argv [] )
{
int i;
for(i=0;i<42;i++){
input_legality();
}
return 0;
}
This scanf is the easy way : some other may be better.
Using switch-case may clarify the code.
Bye,
Francis

Always, always, always get (interactive) input a line at a time. That's how the user conceives it, so the program logic should be similar.
The POSIX getline function is very useful. Alternatively, you can use fgets and deal with overlong lines yourself (perhaps implementing getline, since it is pretty easy).
Once you've fetched a line of input, parse it (being sure to error on trailing garbage) using any method: sscanf, strtok_r, strtou?ll?, ...

Related

How do i get a program to loop until a character is entered?

What I'm exactly trying to achieve is: You enter your name, get a response like 'your name is', and if you enter a number you get a response like 'invalid input' which loops you back to the 'Enter your name' part
#include <stdio.h>
char i[20];
int result;
int main()
{
void findi(); // im trying to loop it back here if a number is entered instead of a character
printf("Enter your name\n");
result = scanf("%s", &i);
while(getchar() != '\n'){ //dont know how to make it work without the '!'
if(result = '%s'){
printf("Your name is: %s", &i);
return 0;
}
else{
printf("Invalid input"); //doesnt work
findi();
}
}
}
//program just ends after a character is entered instead of continuing
Using &i (char(*)[20]) for %s (expects char*) invokes undefined behavior.
The condition result = '%s' (assign implementation-defined value to result without checking its value) looks weird.
Calling findi() (not disclosed here) from main() need not mean a loop.
Try this:
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char i[20];
printf("Enter your name\n");
/* an infinite loop (loop until return or break) */
for (;;) {
int number_exists = 0, j;
/* limit length to read and check the result*/
if (scanf("%19s", i) != 1) {
printf("read error\n");
return 1;
}
/* check if a number is entered */
for(j = 0; i[j] != '\0'; j++) {
if (isdigit((unsigned char)i[j])) {
number_exists = 1;
break;
}
}
/* see the result */
if (number_exists) {
/* one or more number is entered */
printf("Invalid input\n");
} else {
/* no number is entered : exit from the loop */
printf("Your name is: %s\n", i);
break;
}
}
return 0;
}

Using iteration in order to write less repetitive and less time consuming code in C

I am writing a code which compares two strings for a simple guessing game. One string is pre-set, in my code it's "Eggman", and this is then compared with a user input, if the letter in the same position is the same then the user is shown the letter on screen, if it is not the same as the pre-set string then a question mark appears. For example, since the pre-set string is "Eggman" the user will be asked for an input, if the input is for example "Eclair", then the program outputs "E?????", which is exactly what I want the program to do, however, I have used multiple if statements, which has made the code inconvenient, and cannot really think of a way to do it with iteration. I thought about using the strcmp() function, but I pretty sure that it can't be used to compare singular characters in a string. The code is below:
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
void checkAnswer();
int main()
{
checkAnswer();
return 0;
}
void checkAnswer()
{
char word[]="Eggman";
char input[4096];
printf("Guess the word:");
scanf("%s", input);
if (word[0]==input[0])
{
printf("E");
}
else
{
printf("?");
}
if (word[1]==input[1])
{
printf("g");
}
else
{
printf("?");
}
if (word[2]==input[2])
{
printf("g");
}
else
{
printf("?");
}
if (word[3]==input[3])
{
printf("m");
}
else
{
printf("?");
}
if (word[4]==input[4])
{
printf("a");
}
else
{
printf("?");
}
if (word[5]==input[5])
{
printf("n");
}
else
{
printf("?");
}
}
As you asked there is much easier way to solve your problem instead of definend multiple if else you can use loop.
I have tried to solve your problem, hope it will help:
#include<stdio.h>
int main()
{
char word[]="Eggman";
char input[4096];
char abc[] ={0};
int i=0,f=0;
printf("Guess the word:");
scanf("%s", input);
while(1){
if(strcmp(input,word)==0)
{
break;
}
else
{
if(f==1){
scanf("%s", input);
}
else
{
f=1;
}
}
for(i=0;i<strlen(word);i++)
{
if(input[i]==word[i])
{
printf("%c",input[i]);
}
else
{
printf("?");
}
}
}
}
The above program will ask your again and again till the user input matched with the declared string.

check input program gets stuck in an infinte loop

I'm trying to create a program that asks to type something and check if it is an integer. If it is an integer, then print "the integer is ...". Else, print "try again" and waits for another input. However, the program prints an infinite number of "try again" if you type in a character. Here's the source code:
#include <stdio.h>
#include <stdbool.h>
int main()
{
int inp;
bool t = 1;
printf("type an integer\n");
while (t) {
if (scanf("%i", &inp) == 1) {
printf("The integer is %i", inp);
t = 0;
} else {
printf("try again");
scanf("%i", &inp);
}
}
}
OP's code fail to consume the offending non-numeric input. It remains in stdin, for the next input function. As it is unfortunately just another scanf("%i", &inp) which fails the same way - infinite loop.
After attempting to read an int, read the rest of the line.
#include <stdio.h>
#include <stdbool.h>
int main() {
int inp;
int scan_count;
printf("Type an integer\n");
do {
scan_count = scanf("%i", &inp); // 1, 0, or EOF
// consume rest of line
int ch;
while ((ch == fgetchar()) != '\n' && ch != EOF) {
;
}
} while (scan_count == 0);
if (scan_count == 1) {
printf("The integer is %i\n", inp);
} else {
puts("End of file or error");
}
}
An even better approach would read the line of user input with fgets(). Example
When you entered a char, the variable inp in scanf("%d", &inp) would get null, since the input that doesn't match the format string. And the character you input would remain in the buffer, so that's the reason both your scanf would not stop.
A simplest way to fix this is modify your second scanf("%i", &inp); to scanf("%c", &c); (don't forget to declare a char c in your main function).
check here while(t) its in an infinite loop because you have to set a condition for t something like while(t==1) or while(t>1) or (t<1) something like that. saying while(t) means that t can be anything and it will continue to run.
There is nothing in to break the while loop.
consider getting rid of the boolean, and simply using a while (1) loop with a break. Also you should be using "%d" to indicate an integer in scanf/printf. And there is no need for the scanf call in the else, since your program would loop back and call scanf again anyway.
#include <stdio.h>
int main() {
int inp = 0;
printf("type an integer\n");
while (1) {
if (scanf("%d", &inp) == 1) {
printf("The integer is %d", inp);
break;
}
else {
printf("try again");
}
}
return 0;
}
I hope this helped.

First input skipped, straight to next input

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.

Letter guessing game weird output

So here is my code:
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#define MAX_GUESSES 4
int PlayGame(char guess);
int WinOrLose(char userguess, char solution);
int main()
{
FILE* infile;
char correctlet;
int games,
igame,
result;
infile = fopen("inputLet.txt", "r");
printf ("Welcome to the letter guessing game!\n");
printf ("Your job is to guess a random letter.\n");
printf("You can guess each letter a maximum of 4 times,\n");
printf ("and you will get a hint after every guess.\n");
printf ("LET'S DO THIS!\n\n>");
printf ("\nHow many games would you like to play (1-3)?\n>");
scanf ("%d",&games);
for(igame=0;igame<games;igame++)
{
fscanf(infile," %c",&correctlet);
printf("This is game %d\n", igame+1);
result = PlayGame (correctlet);
if (result == 0)
{
printf ("\nCongratulations, you guessed the right letter!\n");
}
else
{
printf ("\nUnfortunately, you did not guess the right letter. Better luck next time!\n");
}
}
return 0;
}
int PlayGame(char solution)
{
int guessnumber,
result;
char userguess;
guessnumber = 0;
while(guessnumber < MAX_GUESSES)
{
printf("Please enter your guess\n>");
scanf("%c", &userguess);
if (sizeof userguess == 0)
{
continue;
}
else if (sizeof userguess >=1)
{
printf ("Your guess was %c\n",userguess);
result = WinOrLose (userguess, solution);
if (result == 0)
{
return 0;
break;
}
else if (result == 1)
{
if (solution < userguess)
{
printf("The correct letter comes before %c alphabetically\n", userguess);
}
else if (solution > userguess)
{
printf("The correct letter comes after %c alphabetically\n", userguess);
}
guessnumber ++;
}
}
}
}
int WinOrLose (char userguess, char solution)
{
if(solution==userguess)
{
return 0;
}
else if (solution != userguess)
{
return 1;
}
}
The output asks for the number of games, and then it outputs please enter your guess your guess was (blank) The correct letter comes after (blank) Please enter your guess and THEN it allows for user input. So why is it going through one iteration of PlayGame without asking for user input? I have tried everything I can think of and can't fix the problem. I am compiling on VC++ 2010, if that helps.
Thanks in advance!
The simple answer is to flush your buffers.
The stdin buffer, the buffer that takes instructions from the keyboard (or a pipe) and submits it to the program occasionally gets some characters "stuck" in it. Junk characters that never quite get submitted, extra returns, etc. that will cause scanf() to think it reached the proper end, but actually hasn't.
fflush(stdin);
The function fflush "flushes" a buffer. The effect of this is to consume data from a buffer until the data received is the character '\0' (NULL). This means that it's reached the last of the data that is currently in the buffer.
Calling this before calling scanf() means that when scanf() is called, you reasonably know that the program will block on scanf() until you've submitted, and not just consume some junk from the buffer.

Resources