This question already has answers here:
How to check if input is numeric in C++
(8 answers)
Closed 7 years ago.
This is what I have now:
int main() {
int number;
printf("Type your number: ");
scanf("%i",&number);
char code[4];
printf("Type your code: ");
scanf("%s",&code);
When I type anything but numbers in the first one the script goes all crazy, it just shows
Type your number: NOTaNUMBER
Type your code: THErestOFtheSCRIPT
-- Back to command line
What I want it to do is
Type your number: NOTaNUMBER
You didn't enter a number
-- Back to command line
How can I do this?
This is different from the said duplicate. I'm talking about C here, not C++.
C doesn't know cin, the answer to the said duplicate. However, an answer was found below
For input string use
scanf("%s",code);
or better
scanf("%3s",code);
To check correct input for number use value returned by scanf:
char ch;
if( 1 == scanf("%i",&number) )
{
// use correct number
}
else
{
// clean input bufer
while( (ch = getchar()) != '\n' && ch != EOF);
}
Related
This question already has answers here:
Check if input is integer type in C
(16 answers)
Closed 3 years ago.
I need to check that if the user input a value, the program runs only if the value is a positive integer between [1-8] (Inclusive), but if the input value is a letter or word or not input at all (Enter), the program ask again for an input
#include <stdio.h>
int main (){
int height;
do {
printf ("Height: ");
scanf ("%i", &height);
}
while (height < 1 || height > 8);
{ .
.
.
.
I was thinking to add another condition "|| (or)" in the while validation, something like while "height
be different(!=) from an integer" then do...
But I can't figure out how to check that statement in other StackOverflow questions. I hope you can enlighten me
use fgets()
char buf[1000];
if (!fgets(buf, sizeof buf, stdin)) {
exit(EXIT_FAILURE);
}
if (buf[1] != '\n') exit(EXIT_FAILURE);
if (*buf < '1') exit(EXIT_FAILURE);
if (*buf > '8') exit(EXIT_FAILURE);
// use *buf, maybe (*buf - '0')
an input of "<ENTER>", "0<ENTER>", "12<ENTER>", "a<ENTER>", "<SPACE>3<ENTER>", ... will exit with failure
I've found this answered question:
Check if input is integer type in C
This question only will solve the part about being able to know is the input of the scanf is a number or not, which seems on of the main points. You will still have to check if the input is positive or if it's in the range you want.
This question already has answers here:
How to scanf only integer?
(9 answers)
Closed 4 years ago.
I want to know how to keep taking inputs from keyboard with this condition: If the given input is a positive number, it keeps going with the code If the given input is a negative number or a letter, it must print "insert a positive number" and then ask again for another input until it has the correct one. About negative and positive inputs the code i wrote works great, but it bugs out when I put a letter. The check I tried is the following
chk=isalpha(n);
while(!chk || n<0)
{
printf("Inserire un intero positivo \n");
scanf("%d", &n);
chk=isalpha(n);
}
printf("%d\n%d\n", t1, t2);
In this case if I put a negative number it works correctly, but if I type a letter the printf loops. I also tried while(isalpha(n) || n<0) And a bunch of other pieces of code I'll skip for you. Please help me figure this out
You can check return value of scanf in case of char it returns 0 along with that you need to clear the buffer to stop scanf consuming the same character.
Example:
int ret = 0;
do
{
char c;
while ((c = getchar()) != '\n' && c != EOF) { } /* to clear the bad characters*/
printf("Inserire un intero positivo \n");
ret = scanf("%d", &n);
}while(!ret || n<0);
This question already has answers here:
What is the effect of trailing white space in a scanf() format string?
(4 answers)
Simple C scanf does not work? [duplicate]
(5 answers)
Closed 4 years ago.
I wrote this code for a school project and I have the following problem.
When I choose 1 at the first time, my program runs fine but as I choose w or r the second time, something goes wrong. None of the 2 ifs is running. I printed usr_ans2 to see the result of scanf and usr_ans2 variable is a weird question mark in a box and not a w or r character as I typed. Also I tried scanf(" %c", usr_ans2) . The question marks do not appear but the if commands are still not running.
int main(){
int usr_ans1;
char usr_ans2;
while(1){
printf("\nSelect action: (1-3)\n");
scanf("%d", &usr_ans1);
if(usr_ans1 == 1){
printf("Select to write or read from a file the text: (w/r) ");
usr_ans2 = scanf("%c", &usr_ans2);
if(usr_ans2 == 'w')
printf("You selected to write");
else if(usr_ans2 == 'r')
printf("You selected to read");
}
else if(usr_ans1 == 2){
printf("Example1");
}
else if(usr_ans1 == 3){
printf("Example2");
}
return 0;
}
scanf() in usr_ans2 = scanf("%c", &usr_ans2); will return 1 (the numbers of successfully converted specifiers) or EOF (some negative value like -1 when end-of-file or error occurs). if(usr_ans2 == 'w') will never be true.
Try
// usr_ans2 = scanf("%c", &usr_ans2);
scanf(" %c", &usr_ans2); // add the space too to skip leading white-space
This question already has answers here:
i want to getchar twice but i cant
(2 answers)
Closed 7 years ago.
I have been tasked to create a simple lower to upper case program. However, while testing if my input prints correctly, I have noticed that every time I enter a character, it always loops again even when its not supposed to.
#include <stdio.h>
int main() {
char input;
//enter a character
//set the given input char to variable
printf("Enter a character in lower case: \n");
input = getchar();
//Sentinel value is Q
while (input != 'Q'){
printf("you entered %c.\n",input);
printf("\n");
printf("Enter a character in lower case: \n");
input = getchar(); //gets input from inside the loop
}
printf("Goodbye \n");
return 0;
}
test output (I input the character 'g' and pressed enter once):
Enter a character in lower case:
g
you entered g.
Enter a character in lower case:
you entered
.
I'm not seeing the problem.
The problem here is caused by the newline character entred into the stdin by pressing the ENTER key. Before proceeding for the next input, you need to clear the input buffer , like adding
while (getchar() != '\n');
before asking for the next input.
That said, getchar() returns an int. A char may not be enough to hold the return value always, Change
char input;
to
int input = 0;
This question already has answers here:
i want to getchar twice but i cant
(2 answers)
Closed 7 years ago.
I have been tasked to create a simple lower to upper case program. However, while testing if my input prints correctly, I have noticed that every time I enter a character, it always loops again even when its not supposed to.
#include <stdio.h>
int main() {
char input;
//enter a character
//set the given input char to variable
printf("Enter a character in lower case: \n");
input = getchar();
//Sentinel value is Q
while (input != 'Q'){
printf("you entered %c.\n",input);
printf("\n");
printf("Enter a character in lower case: \n");
input = getchar(); //gets input from inside the loop
}
printf("Goodbye \n");
return 0;
}
test output (I input the character 'g' and pressed enter once):
Enter a character in lower case:
g
you entered g.
Enter a character in lower case:
you entered
.
I'm not seeing the problem.
The problem here is caused by the newline character entred into the stdin by pressing the ENTER key. Before proceeding for the next input, you need to clear the input buffer , like adding
while (getchar() != '\n');
before asking for the next input.
That said, getchar() returns an int. A char may not be enough to hold the return value always, Change
char input;
to
int input = 0;