Console exits on enter [duplicate] - c

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

Related

Small issue with my coding in C using stdio.h [duplicate]

This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
scanf Getting Skipped [duplicate]
(1 answer)
Closed last year.
I am currently working on a code and I keep having issues with my code. I will add it below, but for some reason, whenever I ask for the input, it skips over the second scanner and goes straight to the third. Does anyone know what the issue may be?
#include <stdio.h>
int main()
{
char a,b,c,d;
printf("Please input the price of your shopping items:");
printf("\n");
printf("Item 1:");
scanf("%c", &a);
printf("Item 2:");
scanf("%c", &b);
printf("\n");
printf("Item 3:");
scanf(" %c", &c);
return 0;
}

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.

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

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

date or time change.reload from disk error in c [duplicate]

This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 3 years ago.
I use emulated Turbo C++ IDE 3.0 for development in C. I get the above mentioned error on when I run the following program:
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,k;
char x;
do
{
printf("enter the number i: ");
scanf("%d",&i );
printf("enter the number j: ");
scanf("%d",&j);
k=i+j;
printf("Addition of given numbers :%d ",k);
printf("\n Do you to continue : ");
scanf("%c",&x);
} while(x=='y'||x=='Y');
getch();
}
It was successfully compiled without errors or warnings. At run time, the problem arises after the statement printf("\n Do you to continue : ");.
When I enter a value for this, the error shows up. I tried in dev C++ but the problem prevails. I am using Windows 7 Ultimate.
Suggestion
change
scanf("%c",&x);
to
scanf(" %c",&x);

Resources