int problem;
printf("Please select the problem that you want to solve:\n");
printf("\t 1-Problem 1\n");
printf("\t 2-Problem 2\n");
printf("\t 3-Problem 3\n");
while( scanf("%d", &problem)==0 && (problem!=1 || problem !=3 || problem !=2))
{
int c;
while((c=getchar())!='\n' && c!=EOF);
printf("Please select the problem that you want to solve:\n");
printf("\t 1-Problem 1\n");
printf("\t 2-Problem 2\n");
printf("\t 3-Problem 3\n");
}
It kinda looks messy because of the multiple printfs. I just don't want lengthy codes in one row. Anyway, I wanted only 1,2, or 3 as inputs. If the entered input is invalid, the program asks the user again until the user inputs a valid input.
Sure, the code works for characters, string inputs but whenever 0, or any other integer or input starting with a number except for 1,2 and 3, the program does nothing. Also, if 1.2 is entered, 1 is chosen. same goes for 2.2, 2.3 which will become 2. How can I fix it?
Point 1
You need to change the logic involved in
scanf("%d", &problem)==0
because, if scanf() returns 0, then reading problem is undefined behaviour.
You need to re-think the while loop logic. Also, always initialize the local variables.
Point 2
You need to change
(problem!=1 || problem !=3 || problem !=2)
to
((problem!=1) && (problem !=3) && (problem !=2))
because, logically, you're tryning to say, if the input is not 1 and not 2 and not 3, then..., so,
You can try this code
while( scanf("%d", &problem)==0 && (problem<1 || problem >3))
It will check valid user input and also check user has entered value between 1 to 3.
condition will fail if user enter value less than 1 or greater than 3.
Related
part of the program that i am working on is to get a number from the user , but the condition is that it has to be any number between 1 and 10 nothing else, so how could i force the user to only input one of these specific numbers , so that if he wrote any other number or a character , an error message to pop out and repeat the process till he choses correctly
here is the code
// getting a number from the user
int number;
printf("Please pick a number from 1 to 10 : ");
scanf("%d",&number);
printf("Oh ! You have chosen %d\n", number);
// what should i do here ?
C allows for very nice input control, but it is not free... Said differently you cannot rely on the language nor on the standard library but have to code everything by hand.
What you want:
control that the input is numeric
control that the number lies between 1 and 10
How to:
control the return value of the input function
if you got a number control its value
if you got an incorrect input (optionaly) give a message and loop asking
Possible code:
int number;
for (;;) { // C idiomatic infinite loop
printf("Please pick a number from 1 to 10 : ");
if (scanf("%d", &number) != 1) {
// non numeric input: clear up to end of line
int c; // c must be int to compare to EOF...
while ((c = fgetc(stdin)) != EOF && (c != '\n'));
}
else if (number > 0 && number <= 10) break; // correct input: exit loop
// error message
printf("last input was incorrect\n");
}
printf("Oh ! You have chosen %d\n", number);
If you want a more user friendly way, you could use different messages for non numeric input and incorrect values, but it is left as an exercise for you... (I am afraid I am too lazy ;-) )
What you likely envision is a fine-grained control over the character-by-character input of the user; for example, anything but digits should be impossible; or when they type a 2 and try to type another digit, that should be impossible, too.
That's something we know from graphic user interfaces. It requires that your program is "informed" about every key stroke at once.
For historical reasons, this capability is not part of the C standard library. The reason is that historically all kinds of input devices were used, for example punch cards or paper-based teletypes. The communication was line by line: Input was local until the user hit the aptly named "enter" key. Any stupid device can do that, a lowest common denominator which is why all languages which do not define GUI elements adhere to it.
Obviously, character-by-character input is entirely possible on modern terminals and computers; but it is system specific and has never been standardized in the language. It is also likely more complicated than meets the eye if you want to give the user the opportunity to edit their input, a phase during which it may be "illegal". In the end you'll need to catch the point when they submit the entire value and validate it, which is something you can do even with the crude facilities that C provides.
Hints for an implementation:
Let the user complete a line of input. Validate it, and if the validation fails, prompt for another attempt. Do that in a loop until the input is valid.
Use scanf because it is convenient and error free (compared to home-grown input parsing).
This is something often overlooked by beginners: Check the return value of scanf which will indicate whether the input could be parsed (read the scanf manual!).
int main()
{
int number = 0;
while (number < 1 || number > 10)
{
printf("please enter number between 1 - 10\n");
scanf("%d", &number);
if (number < 1 || number > 10)
{
printf("you entered invalid number!\n");
}
}
return 0;
}
while(1)
{
//input ....
if(number<0 || number>10)
{
// print error
continue;
} else {
while (getchar() != '\n') ;
break;
}
}
I think I made a mistake at the beginning.A character can be checked by if but scanf can't. If scanf can't get the input in the specified format, the illegal input in the input buffer will be kept all the time.
After looking at another question, I thought that when the input is wrong, we should use getchar() to clear the buffer before the next input.
I'm new to c language and wanted to know how to restrict the user from entering 0 as a valid number. I know you can use an if statement, but that seems too much for a simple thing. Because I recall in python being able to create validation for an input where the user cannot input a certain number.
printf("Input first number: ");
scanf("%d",&num1);
printf("Input the second number: ");
scanf("%d",&num2);
you cant prevent but you may check
do
{
int scanfresult;
printf("Input the second number (non zero): ");
scanfresult = scanf("%d",&num2);
}while(scanfresult != 1 || num2 == 0);
I'm writing a simple C program in which I want to validate my user's input for integers from 1-9.
The logic for my code seems fine but for some reason if I type "lll" for my input (or any other random input not between 1-9), it will show the error message inside the while loop a couple of times before actually stopping at the getchar() once again.
/**
* Validate user input and reprompt if invalid
* #return input - Users valid input
*/
int validateUserInput() {
int firstInput = getchar() - 48;
int secondInput = getchar();
while(secondInput != 10 || firstInput >= 10 || firstInput <= 0 ){
printf("Invalid Input. Please try again. \nEnter a number between (1-9): ");
firstInput = getchar() - 48;
secondInput = getchar();
}
return firstInput;
}
The logic is that:
Invalid Input - If the second input != 10 which means that the second char is something other than the enter key.
Invalid Input - If the first char subtracted 48 is higher than 9 or less than 1 then it's too high.
The logic seems fine to me but here's some output when I enter random characters:
Enter a number between 1-9: lllll
Invalid Input. Please try again.
Enter a number between 1-9: Invalid Input. Please try again.
Enter a number between 1-9: Invalid Input. Please try again.
Enter a number between 1-9: Invalid Input. Please try again.
Enter a number between 1-9: Invalid Input. Please try again.
Enter a number between 1-9: Invalid Input. Please try again.
Enter a number between 1-9:
Why does it repeat that message so many times before actually letting me enter input again? This is all that's wrong with my program.
Your logic is fine. But the invalid characters you input still remain in the input buffer. So, the subsequent calls to getchar() read them.
Whenever you hit an invalid input, you want to ignore them all. You can read them out using a loop like this:
while(secondInput != 10 || firstInput >= 10 || firstInput <= 0 ){
printf("Invalid Input. Please try again. \nEnter a number between (1-9): ");
int i;
while((i=getchar()) != '\n' && i != EOF);
...
Having said that it's generally hard to use getchar() and scanf() family functions to correctly read interactive input and do error checking. A better way would be to read lines (such as fgets()) and parse/error check them.
Because you entered 5 chars. 5x L. You are checking only one char, but by what did you wrote I guess you want to check whole input charr array. So then you cant use getChar().
I'm having a bit of a problem restricting the user input. I only want the integers 1,2 or 3 to be the input. Do.. while loop does the job for invalid integer inputs, but how do I disregard string/character inputs? I also need to ask the user repetitively if ever the input is invalid.
Update:
int problem;
printf("Please select the problem that you want to solve:\n");
printf("\t 1-Problem 1\n");
printf("\t 2-Problem 2\n");
printf("\t 3-Problem 3\n");
while( scanf("%d", &problem)==0 && (problem!=1 || problem !=3 || problem !=2))
{int c;
while((c=getchar())!='\n' && c!=EOF);
printf("Please select the problem that you want to solve:\n");
printf("\t 1-Problem 1\n");
printf("\t 2-Problem 2\n");
printf("\t 3-Problem 3\n");
}
It kinda looks messy because of the multiple printfs. I just don't want lengthy codes in one row. Anyway, I wanted only 1,2, or 3 as inputs. If the entered input is invalid, the program asks the user again until the user inputs a valid input.
The code works for invalid inputs such as words, letters, characters, etc. However, if the user inputs 1.2 , it proceeds with 1 which should not be the case. 0 isn't accepted either. What can I do to my code to restrict them?
scanf with a %d will fail and return 0 in case of invalid inputs(like character(s)). So, just check if the scanf failed by checking the return value of it.
while(scanf("%d",&num)==0 && (num<=1 || num >=3)) //Invalid input if this is true
{
int c;
while((c=getchar())!='\n' && c!=EOF); //Clear the stdin
printf("Invalid input. Try again\n");
}
The line
while((c=getchar())!='\n' && c!=EOF);
clears the standard input stream so that scanf does not read the invalid input again and again resulting in an infinite loop.
Note that scanf can return EOF. This will cause the program to think that the user has entered valid input. You can add a check to see if the return value from scanf isn't EOF. Also, you should initialize num to a number that is not 1,2 or 3 to avoid invoking Undefined Behavior.
This question already has answers here:
Check if input is integer type in C
(16 answers)
Closed 8 years ago.
I am working on code for one of my classes and I have hit a wall. I need the user to input a number that will be used as the number of times a for loop will be repeated. The first loop where I ask for this number is a while loop. I need to make sure the value entered is a number and not a letter or special character.
I do not know how to make sure that it is not a letter or special character.
The next problem is making sure that the for loop only runs for the specified number of times that is the number provided in the first loop.
This is what I have written so far.
#include <stdio.h>
int main()
{
int num_of_scores, n;
char enter;
float score=-1, total=0, average;
do
{
printf("\n\nEnter the number of quiz scores between 1 and 13: ");
scanf ("%d, %c", &num_of_scores, &enter);
}
while(num_of_scores<1 || num_of_scores>13/* && enter == '\n'*/);
printf("\nStill Going!");
for(n=0; n<num_of_scores; n++)
{
printf("\nEnter score %i: ", n+1);
scanf ("%f", &score);
while(score>=0 || score<=100)
{
total = total + score;
score = -1;
break;
}
}
average = total / num_of_scores;
printf("\nThe average score is %.0f.\n\n", average);
return 0;
}
So I have edited the code a little bit. There is a part in the first while loop that is in a comment which i removed because it made the program end after that loop. The printf("still going") is just a test to make sure the program gets that far. Any further pointers? I am still not sure how to check make sure a number is not entered. I though adding the && enter == '\n' would do it, but if it hangs the program it is no good. Many of the examples you have suggested are good, but i find them a little confusing. Thanks!
I'd check Check if input is integer type in C for the answer to this...
Check the return value of scanf. Per the man page:
RETURN VALUE
These functions return the number of input items successfully matched and assigned, which can be fewer than provided
for, or even zero in the event of an early matching failure.
The value EOF is returned if the end of input is reached before either the first successful conversion or a matching
failure occurs. EOF is also returned if a read error occurs, in which case the error indicator for the stream (see
ferror(3)) is set, and errno is set indicate the error.
do{
char ch = 0;
num_of_scores = 0;
printf("\nEnter the number of quiz scores between 1 and 13: ");
if(scanf("%d%c", &num_of_scores, &ch)!=2 || ch != '\n'){
int ch;
while((ch=getchar())!='\n' && ch !=EOF);
}
} while(num_of_scores<1 || num_of_scores>13);