Accepting only integer in C [duplicate] - c

This question already has answers here:
scanf format specifier to only read a number
(2 answers)
Closed 7 years ago.
How do you propose I write my code to accept only integers in C and if a letter is entered, prompt to enter only integers? And Vice versa.
printf("Enter integer");
scanf("%d", &a);

while(scanf("%d", &a)==0) //if scanf failed to scan an integer
{
printf("Invalid input. Try again\n");
int c;
while((c=getchar())!='\n' && c!=EOF); //clear the stdin
}

Related

why isn't the user input printing in c? [duplicate]

This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
How to do scanf for single char in C [duplicate]
(11 answers)
abnormal behavior of scanf [duplicate]
(3 answers)
Closed 4 months ago.
int userinput;
char convertfrom;
float value;
printf("What do you want to convert? Enter number from 1 to 5: ");
scanf("%d", &userinput);
if (userinput == 1) {
printf("enter letter K or P: ");
scanf("%c\n", &convertfrom);
printf("letter = %c", convertfrom);
}
I input "P" for the second scanf that asks for a letter, but the code keeps failing to print "letter = P". instead it only prints "letter = ".
Why can't I print the user input?

Can we take different data type inputs in different consecutive lines in C [duplicate]

This question already has answers here:
Program doesn't wait for user input with scanf("%c",&yn);
(5 answers)
scanf() leaves the newline character in the buffer
(7 answers)
Closed 1 year ago.
int main()
int c,s;
char h;
printf("Enter the carbon\n");
scanf("%d",&c);
printf("Enter the hard\n");
scanf("%c",&h);
printf("Enter strength\n");
scanf("%d",&s);
printf("%d%d",c,s);
printf("%c",h);
}
Output:
Enter the carbon
4
Enter the hard
Enter strength
My program is not reading character data types. Can anyone tell me the reason?
The compiler is not showing any error.

Console exits on enter [duplicate]

This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
How to read / parse input in C? The FAQ
(1 answer)
Closed 2 years ago.
So, i just want to know how to deal with this, the code doesnt have a purpose besides testing.
why does the the enter i press go to the getchar()command?
or is that the case to begin with? i assume it is.
here is the code
#include<stdio.h>
int main(){
int x,y,z;
printf("Enter two nums\n");
scanf("%d %d", &x, &y);
z=x/y;
printf("%d is answ\n", z);
puts("Press enter to exit");
getchar();
return 0;
}

scanf scans weird question marks in C [duplicate]

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

regarding clearing the input buffer in c [duplicate]

This question already has answers here:
How to clear input buffer in C?
(18 answers)
Why is scanf() causing infinite loop in this code?
(16 answers)
Closed 5 years ago.
I have this code, a program to take an integer from a user that's negative, if not trap them in a loop until they enter a negative integer. I have this part down, however, how could i clear the input buffer?
I tried to use while ((getchar()) != ā€˜\nā€™); after the first scanf_s but it fails.
here is code
#include <stdio.h>
int main()
{
int num;
printf("Please enter a negative integer");
scanf_s("%d", &num);
while (num >= 0.0)
{
printf("Please enter a negative integer!\n");
scanf_s("%d", &num);
}
printf("%d", num);
system("pause.exe");
return 0;
}

Resources