C programming - Repeat scanf until only numbers are input - c

What I wan't to do is check if my input is a number and then continue, else print out wrong format and ask for a new input. What I get is an endless loop printing "Wrong format"
This is my function for the input number:
void input_number(int *number)
{
printf("Number: ");
if ( scanf("%d", number) == 1 )
return 0;
else
{
printf("-> Wrong format, try again! <-\n");
input_number(number); // start over
}
}
When I run the program I want it to look something like this:
Number: hello
-> Wrong format, try again! <-
Number: 4
and go on....

Try this : ( beware there are a lot of much better ways to do this )
void input_number(int *number)
{
int flag=1;
printf("Number: ");
while(flag==1){
if ( scanf("%d", &number) == 1 ){ // also you were missing & specifier
flag = 0;
//return 0;
}else{
printf("-> Wrong format, try again! <-\n");
getchar(); // to catch the enter from the input -- make sure you include stdlib.h
}
}
return 0;
}
Output:
Number: f
-> Wrong format, try again! <-
Number: f
-> Wrong format, try again! <-
Number: d
-> Wrong format, try again! <-
Number: d
-> Wrong format, try again! <-
Number: s
-> Wrong format, try again! <-
Number: s
-> Wrong format, try again! <-
Number: s
-> Wrong format, try again! <-
Number: s
-> Wrong format, try again! <-
Number: 6

I think the problem is that if the scanf() fails because of either EOF or because there's a non-numeric character in the input stream, that problem still exists when you next call it, so it fails again, and this continues until the program runs out of resources or you run out patience.
At the very least, you need to distinguish between:
OK
EOF (nothing more to read)
Error (more to read, but the next character at least is not part of a number)
If you are expecting a single number per line, then it is probably best to use fgets() and sscanf(). If you are happy with multiple numbers per line, then you have to work a bit harder. You should probably also return a value from the function to indicate whether it was successful or not.
int input_number(int *number)
{
char line[4096];
while (printf("Number: ") > 0 && fgets(line, sizeof(line), stdin) != 0)
{
if (sscanf(line, "%d", number) == 1)
return 0;
printf("-> Wrong format, try again! <-\n");
}
return EOF;
}
int input_number(int *number)
{
while (printf("Number: ") > 0 && scanf("%d", number) != 1)
{
int c;
if (feof(stdin) || ferror(stdin))
return EOF;
printf("-> Wrong format, try again! <-\n");
while ((c = getchar()) != EOF && c != '\n')
;
if (c == EOF)
return EOF;
}
return 0;
}
Note the correct use of feof(); an I/O operation has failed and the code needs to distinguish between EOF and I/O errors and format errors.
In the second function, if the printf() fails, you are erroneously told that a number was read. If that's a problem, add extra tests, but that code is already harder to write than the code using fgets(), so be cautious about deciding to use it.
Note that the original code had return 0; in a function returning void. The code should have been rejected by the compiler.

Try using a loop that checks the user input and tries to parse it to an int. If it doesn't succeed, it can keep asking the user for input until they enter an actual int.

At this point you probably know that there are few flaws in your code and recursion isn't the best idea. This one is an attempt to make your code work, pretty or not.
void input_number(int *number)
{
printf("Number: ");
if ( scanf("%d", number) == 1 )
return 0;
else
{
scanf("%*s"); /* <--this will read and discard whatever caused scanf to fail */
printf("-> Wrong format, try again! <-\n");
input_number(number); // start over
}
}

Related

Why this code is not re-prompting correctly?

I am totally new to C.
I want to re-prompt when the input value is not a number and when it's a number it should be less than 1. when I give any sort of string it works correctly. But when I give any number it goes to the next line without printing "Number: ".Then in the next line, it prints "Number: "again if the input value is less than 1.
int x;
printf("Number: ");
while (scanf("%d", &x) != 1 || x < 1 )
{
printf("Number: ");
scanf("%*s");
}
and the result it gives me is this
result
It would be wise to use fgets to read the line, then use sscanf to parse the input. That way, you can get the line, then check if sscanf succeeds!
Simple example:
int target_number; // The number you will have at the end of this.
while (1) { // Loop for rechecking number
char line[16]; // See notes on how to read the whole line.
fgets(line, sizeof(line), stdin);
// We use 1 here because sscanf returns the number of format specifiers that are matched. Since you only need one number, we use 1.
if (sscanf(line, "%d", &target_number) != 1) {
fprintf(stderr, "Invalid Input! Please enter in a valid number.");
continue;
}
}
// Do whatever you will with target_number
Notes
You can see how to read the whole line here.
This code is not safe!
It does not protect against buffer overflow attacks and the like. Please see this on the right way to do this. If this is just for learning, you don't need to worry.
/*This is how it will work the way you want.
If I understand your goal correctly, of course?
If your goal was different,
please specify and I will try to solve it.*/
#include<stdio.h>
int main(void)
{
int x;
printf("Number: ");
while (scanf("%d.%*d", &x)!=1 || x<0)
{
if(x<0)
{
printf("Number: ");
continue;
}
else
printf("Number: ");
scanf("%*s");
}
printf("Hello world!");
return 0;
}

C Program - How to deny any non-numerical input

I've just started learning the language of C, and would love your help in cleaning up / simplifying my code if you know a better way to reach the following.
I want a program to ask for a number, and if that is found then proceed to print and end, however if anything else is put in (e.g. a letter key), then I want the program to loop asking for a number until one is given.
I started off by using a simple scanf input command, but this seemed to go into an infinite loop when I tried to check if a valid number (as we define them) was put in.
So instead I have ended up with this, from playing around / looking online, but I would love to know if there is any more efficient way!
//
// Name & Age Program
// Created by Ben Warren on 1/3/18.
//
#include <stdio.h>
int main (void)
{
//Setting up variables
int num;
char line[10]; /* this is for input */
//Collecting input
printf("Please enter any number? \t");
scanf("%d", &num);
//If Invalid input
while (num==0)
{
printf("\nTry again:\t");
fgets(line, 10, stdin); //turning input into line array
sscanf(line, "%d",&num); //scaning for number inside line and storing it as 'num'
if (num==0) printf("\nThat's not an number!");
}
//If Valid input
{
printf("\n%d is nice number, thank you! \n\n", num);
*}*
return 0;
}
Instead of checking if the value is different to 0, check the return value of
sscanf. It returns the number of conversions it made. In your case it should be 1. Unless the return value is 1, keep asking for a number.
#include <stdio.h>
int main(void)
{
int ret, num;
char line[1024];
do {
printf("Enter a number: ");
fflush(stdout);
if(fgets(line, sizeof line, stdin) == NULL)
{
fprintf(stderr, "Cannot read from stdin anymore\n");
return 1;
}
ret = sscanf(line, "%d", &num);
if(ret != 1)
fprintf(stderr, "That was not a number! Try again.\n");
} while(ret != 1);
printf("The number you entered is: %d\n", num);
return 0;
}
That is not a bad approach for someone new to C. One small improvement would be to actually check the return value of scanf(), since it returns the number of arguments successfully retrieved. Then you could get away from relying on num being 0 to indicate the input was valid. Unless you do want to specifically flag 0 as invalid input.
int ret = scanf("%d", &num);
ret == 1 would mean an integer was succesffully read into num, ret == 0 would mean it was not.
Consider using strtol to parse a string for a long int. This also allows you to detect trailing characters. In this example if the trailing character is not a newline, the input can be rejected. strtol can also detect overflow values. Read the documentation to see how that works.
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
//Setting up variables
long int num = 0;
char line[40] = ""; /* this is for input */
char *parsed = NULL;
printf("Please enter any number? \t");
fflush ( stdout);
while ( fgets(line, 40, stdin))
{
parsed = line;//set parsed to point to start of line
num = strtol ( line, &parsed, 10);
if ( parsed == line) {//if parsed equals start of line there was no integer
printf("Please enter a number? \t");
printf("\nTry again:\t");
fflush ( stdout);
continue;
}
if ( '\n' != *parsed) {//if the last character is not a newline reject the input
printf("Please enter only a number? \t");
printf("\nTry again:\t");
fflush ( stdout);
}
else {
break;
}
}
if ( !parsed || '\n' != *parsed) {
fprintf ( stderr, "problem fgets\n");
return 0;
}
printf("\n%ld is nice number, thank you! \n\n", num);
return 0;
}
0 (zero) is a number...
But I see what you want to do...
You can check for a valid number, using isdigit or a combination of similar functions
I think its also important to follow the advice of other answers to use the return value from scanf using code such as:
int ret = scanf("%d", &num);
and examining ret for success or failure of scanf.

How would I only let the user input one character in c

If I only want the user to enter one char, how would I go about doing that in the c language. My attempt at this is below but it failed horribly. From what i read online I heard you could use the function gets or fgets to accomplish this, but I could not figure out how.
do
{
geussNumber += 1;
printf("Enter guess number %d\n", geussNumber);
scanf(" %c", &geussLetter);
scanf ("%c", &inputViolation);
if (isalpha(geussLetter) == 0)
{
printf("You did not enter a letter\n");
}
else if (inputViolation == true)
{
printf("You eneterd more than one letter\n");
}
else
{
inputLoopEnd = 1;
}
}
while( inputLoopEnd == false );
You could use the getc family of functions.
Have a look at http://quiz.geeksforgeeks.org/difference-getchar-getch-getc-getche/ for example.
It seems you want to read input one line at a time (i.e. the user types a one-letter guess and then <enter>), and you want to verify that the guess indeed contains only a single letter. Couching the problem in those terms perhaps makes it clearer how fgets() could be applied, as that function's purpose is to read one line at a time. Having read a whole line -- or at least as much as the buffer can accommodate -- you can validate it and extract the guess.
scanf() is hard to use properly, so I do recommend the fgets() approach. If you insist on using scanf(), however, then you might do it like this:
// consumes leading whitespace, inputs one character, then consumes any
// immediately-following run of spaces and tabs:
int result = scanf(" %c%*[ \t]", &guessLetter);
if (result == EOF) {
// handle end-of-file ...
} else {
assert(result == 1); // optional; requires assert.h
int nextChar = getchar();
if ((nextChar == '\n') || (nextChar == EOF)) {
// handle multiple-character guess ...
} else if (!isalpha(guessLetter)) {
// handle non-alphabetic guess ...
} else {
// it's valid ...
}
}
Do not use things like fgets() or fputs() etc... They are falling out of use.
As you can see from the description here... this function is designed to handle objects of type str, and you are more focused on using chars at the moment so why not just handle only chars to make life easier.
You can't do this the way you think you can...
scanf(" %c", &geussLetter);
scanf ("%c", &inputViolation);
This can't work because even if the user enters in only one char the way they are supposed to, it's still going to trigger your inputViolation scheme.
Edit: 12:14pm 7/20/2016
I really like the elegance of MOHAMAD's solution on the community wiki.
So I edited to fit your situation and it works well here too. Same idea...
#include <stdio.h>
#include <stdlib.h>
int clean_stdin()
{
while (getchar() != '\n');
return 1;
}
int main(void)
{
int first_time_around = 0;
char theguess = 0;
char c;
do
{
if (first_time_around == 0)
first_time_around++;
else
printf("Wrong input \n");
printf("Enter guess number: \n");
} while (((scanf("%c%c", &theguess, &c) != 2 || c != '\n')
&& clean_stdin()) || !isalpha(theguess));
return 0;
}

when scanf returns 0 in c and just doesn't work

'when there is no successful assignments' i know that scanf returns 0 to indicate it, but is that the only thing that it does? this is my code:
#include<stdio.h>
int main(void) {
int val,x;
x=scanf("%d",&val);
if(x==1)
printf("success!");
else{
printf("try again\n");
scanf("%d",&val);
}
return 0;
}
if i enter a number, it works fine but if i enter a character scanf doesn't work anymore, this is what i get:
k
try again
process returned 0 (0x0) execution time :2.578 s
press any key to continue.
_
meaning that it doesn't allow me to enter a new value, why is that? is there something wrong in the code? if yes how can i fix it? should i stop using scanf?
When scanf doesn't work, the invalid data is still left in the stream. You'll have to read and discard the data from the stream first before you can enter more data.
#include<stdio.h>
int main(void) {
int val,x;
x=scanf("%d",&val);
if(x==1)
printf("success!");
else{
// Discard everything upto and including the newline.
while ( (x = getchar()) != EOF && x != '\n' );
printf("try again\n");
scanf("%d",&val);
}
return 0;
}
The scanf family of functions are broken-as-specified and should never be used for anything.
The correct way to write this program is to use getline, if available, or fgets otherwise, to read an entire line of user input. Then use strtol to convert the input to a machine integer, taking care to check for errors:
errno = 0;
result = strtol(line, &endptr, 10);
if (endptr == line || *endptr != '\n' || errno)
// invalid input

Compare user input with text file in C

I need to have a user input a word then compare the word with a text file to see if it is correct. The user has 3 attempts to enter the word before the program terminates. My issue is reading the word from the file I know it's something simple that I have wrong. I should also clarify that the error I'm getting is in the compiler I haven't gotten to the point of being able to compare the strings yet!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
FILE *fp;
fp = fopen("secret.txt","r");
char guess[10];
const char secret[10];
int i, c;
c = getc(fp);
fgets(secret, sizeof(secret), fp);
for (i=0; i < 3; i++)
{
printf("Please guess the word: \n");
scanf("%s", guess);
while (c !=EOF)
{
if (strcmp(secret,guess)==0)
{
printf("Your guess was correct");
return 0;
}
else
{
printf("Your guess was incorrect. Please try again\n");
}
}
fclose (fp);
}
return 0;
}
Here are some pointers:
c = getc(fp) consumes the first character of the file, so it never becomes part of the secret variable.
If secret.txt contains a newline, the newline is read into the secret variable.
The while (c != EOF) loop seems pointless, since c isn't modified inside the loop. Furthermore, the infinite nature of the loop prevents the outer for loop from functioning correctly.
If I were you, I'd fix the while loop and would make sure that secret is read correctly, for example by printing it out or examining it in a debugger.
What is
c = getc(fp);
needed for? My "guess" would be that you read the first character of the word into c and then secret misses the first character.
EDIT: Instead of using getc for EOF checking, which as said corrupts the read word (and this while loop is rubbish anyway), just check the return value of fgets:
if(fgets(secret, sizeof(secret), fp) == NULL)
//file is empty or other error occurred
and remove this infinite while(c != EOF) loop.
So it should rather look something like:
FILE *fp = fopen("secret.txt","r");
char guess[10];
const char secret[10];
int i;
if(fgets(secret, sizeof(secret), fp) == NULL)
{
printf("Error while reading file\n");
return -1;
}
fclose(fp);
for (i=0; i < 3; i++)
{
printf("Please guess the word: \n");
scanf("%s", guess);
if (strcmp(secret,guess) == 0)
{
printf("Your guess was correct");
return 0;
}
else
printf("Your guess was incorrect. Please try again\n");
}
return 0;
Your code is grossly off: you do not alter 'c' inside a loop, making it spin indefinitely. It's a good idea to sketch your algorithm on a piece of paper before you start coding. In your case, pseudocode should look like this:
Open file
Read the secret
Close file
Repeat three times:
--- Display the prompt
--- Read user input
--- If user input matches the secret, congratulate the user and exit.
Tell the user his guess was incorrect.
At this point, converting it to C should be more or less mechanical. Good luck!
while (c !=EOF)
{
if (strcmp(secret,guess)==0)
{
printf("Your guess was correct");
return 0;
}
else
{
printf("Your guess was incorrect. Please try again\n");
}
}
looks like an infinite loop to me

Resources