regarding clearing the input buffer in c [duplicate] - c

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;
}

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?

Why character value is not taking after integer value in c [duplicate]

This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 2 years ago.
So this is my c code
#include <stdio.h>
void main()
{
int a;
char b;
printf("Enter input \n");
scanf("%d", &a);
printf("Enter char input \n");
scanf("%c", &b);
printf("%d,%c", a, b);
}
And I'm getting output as
Enter input
3
Enter char input
3,
Can you explain me why its not taking character value the second time. This program works well if i take charcter input first.
you are entering space or enter button after integer input, So b is assigned that space/enter.
add getchar(); before input b

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;
}

Why my scanf for char variable is not accepting value while running in the codeblocks? [duplicate]

This question already has answers here:
scanf won't ask for input the second time [duplicate]
(5 answers)
Packing two characters to an integer
(2 answers)
Closed 6 years ago.
Here is the code
//WAP in Co convert a binary or octal to a decimal number depending on user choice.
#include <stdio.h>
#include <math.h>
int conversion(int number,int base);
int main(void){
int number,base,decimal;
char choice;
printf("Enter the number:");
scanf("%d",&number);
printf("Enter 'b' for binary or 'o' for octal:");
scanf("%c",&choice); //Problem occuring here
if(choice=='b')
base=2;
base=8;
decimal=conversion(number,base);
printf("Decimal number:%d",decimal);
return 0;
}
int conversion(int number,int base){
int reminder;
int decimal=1;
int i=0;
while(number>=0){
reminder=number%base;
decimal=(decimal*reminder)*pow(base,i);
number/=10;
i++;
}
return decimal;
}
You have two scanf() and after the one some character is left out to read. you can use getchar() before the next scaf() it should take the next input
printf("Enter 'b' for binary or 'o' for octal:");
getchar();
scanf("%c",&choice);
Inside the conversion function you have while loop with a condition causes it go in infinite loop.Dividing a positive number by 10 minimum you can get is 0. use the below code:
while(number>0)
Also, if condition you have after the second input is useless. since you assign 8 to base anyway.

Accepting only integer in C [duplicate]

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
}

Resources