Reputation: 39
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....
Upvotes: 0
Views: 14482
Reputation: 5555
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
Upvotes: 1
Reputation: 753515
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:
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.
Upvotes: 0
Reputation: 3706
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
}
}
Upvotes: 0
Reputation: 252
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.
Upvotes: 0