This question already has answers here:
How to do scanf for single char in C [duplicate]
(11 answers)
Closed 8 years ago.
I m new in c programing
I write a program. But when i run the program before i entered my choice program ends.
Here is the screenshot.
https://onedrive.live.com/download?resid=E63780628DD96583!3161&authkey=!APl3ReO7T4XIO_s&v=3&ithint=photo,jpg
#include<stdio.h>
#include<conio.h>
main()
{
char ch;
int num1, num2, a, m, s;
float d;
printf("\nEnter The First Number: ");
scanf("%d", &num1);
printf("\nEnter The Second Number: ");
scanf("%d", &num2);
a=num1+num2;
m=num1*num2;
s=num1-num2;
d=(float)(num1/num2);
printf("\nEnter Your Choice: ");
scanf("%c", &ch);
switch(ch)
{
case 'A': printf("\nThe Addition Of The Number Is= %d", a);
break;
case 'M': printf("\nThe Multipication Of The Numbers Is= %d", m);
break;
case 'S': printf("\The Subsraction Of THe Numbers Is= %d", s);
break;
case 'D': printf("\nThe Division Of The Two Numbers Is= %f", d);
break;
default : printf("\nInvalid Entry");
break;
}
getch();
return 0;
}
Where Did I Do The Mistake???
Add a space before %c in the scanf will solve the issue.This happens because scanf does not consume the \n character after you enter the values of the first two integers.As the Enter key(\n) is also a character,it gets consumed by the scanf("%c",&ch); and thus,your default case gets executed.The space before the %c will discard all blanks and spaces.
Related
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)
Scanf skips every other while loop in C
(10 answers)
Closed 4 months ago.
i created a code that converts the temperature into another form, like if you want to convert celcius to farenheit or kevlin and vice versa. But it isnt working as i thought.
code:-
#include <stdio.h>
int main()
{
float a, b;
char c, e;
printf("What you want to change\ntype c for celcius, f for farenheit or k for kelvin: ");
scanf("%c", &c);
switch (c)
{
case 'c':
printf("Now put the celcius value you want to convert: ");
scanf("%f", &a);
printf("Now in which you want to change\ntype f for farenheit or k for kelvin: ");
scanf("%c", &e);
switch (e)
{
case 'f':
printf("The farenheit value of %0.2f degree celcius: %0.2f\n", a, a*1.8+32);
break;
case 'k':
printf("The kelvin value of %0.2f degree celcius: %0.2f\n", a, a+273.15);
break;
default:
printf("You didnt put the correct operation");
break;
}
break;
case 'f':
printf("Now put the farenheit value you want to change into celcius: ");
scanf("%f", &a);
b = ((a-32)*5)/9;
printf("Now in which you want to change\ntype c for celcius or k for kelvin: ");
scanf("%c", &e);
switch (e) {
case 'c':
printf("The celcius value of %0.2f degree farenheit: %.2f\n", a, b);
break;
case 'k':
printf("The kelvin value of %0.2f degree farenheit: %0.2f\n", a, b+273.15);
break;
default:
printf("You didnt put the correct operation");
break;
}
break;
case 'k':
printf("Now put the kelvin value you want to change: ");
scanf("%f", &a);
if (a>=0) {
printf("Now in which you want to change\ntype c for celcius or f for farenheit: ");
scanf("%c", &e);
b=(a-273.15)*1.8+32;
switch (e) {
case 'c':
printf("The celcius value of %0.2f kelvin in celcius is: %0.2f\n", a, a-273.15);
break;
case 'f':
printf("The farenheit value of %0.2f kelvin in farenheit is: %0.2f\n", a, b);
break;
default:
printf("You didnt put the correct operation");
break;
}}
else {
printf("Kelvin cannot be less than zero");
}
break;
default:
printf("You didnt put the correct spell of what you want to change\n");
break;
}
return 0;
}
I was trying to make it so that it will ask the user in which you want to change but it didnt worked as i thought. It isnt asking for what you want to change and skips that scanf function.
The issue you're getting is that scanf leaves a newline character in the buffer after getting the float. Then the scanf for the character gets the newline and you're switch statement goes to the default statement. This is a major flaw of scanf and can be very confusing for new users of C. One potential solution to this is to clear the buffer using this:
while ((c = getchar()) != '\n' && c != EOF);
This uses getchar (which takes one character from the same buffer as scanf) and will loop until getchar gets a newline or gets to the end of the buffer. Then when you run scanf it will have an empty buffer an take input from the user.
I want this program to repeat every time when users wants to continue.
I want user to choose "yes" to continue the program using other options but it's not executable. I used 'if else' statement for that but I am not getting any applicable or satisfying results. Can someone help me on this matter?
#include <stdio.h>
int main() {
float a, b;
int p;
char q;
printf("Enter the first number:");
scanf("%f", &a);
printf("Enter the second number:");
scanf("%f", &b);
printf("Choose the option:\n1.Sum\n \n2.Subtraction\n \n3.Multiplication\n \n4.Divison\n \nyour choice=");
scanf("%d", &p);
switch (p)
{
case 1:
printf("The sum of above numbers is: %f", a + b);
break;
case 2:
printf("The subtraction of above numbers is:%f", a - b);
break;
case 3:
printf("The multiplication of above numbers is:%f", a * b);
break;
case 4:
printf("The division of above numbers is:%f", a / b);
break;
}
printf("\n\nDo u want to continue:(y for yes and n for no) :");
scanf("%c", &q);
if (q == 'y')
return main();
else
return 0;
}
You need to use a loop for this. Explanations in the comments:
int main() {
float a, b;
int p;
char q;
do // use a loop here
{
printf("Enter the first number:");
scanf("%f", &a);
printf("Enter the second number:");
scanf("%f", &b);
printf("Choose the option:\n1.Sum\n \n2.Subtraction\n \n3.Multiplication\n \n4.Divison\n \nyour choice=");
scanf("%d", &p);
switch (p)
{
case 1:
printf("The sum of above numbers is: %f", a + b);
break;
case 2:
printf("The subtraction of above numbers is:%f", a - b);
break;
case 3:
printf("The multiplication of above numbers is:%f", a * b);
break;
case 4:
printf("The division of above numbers is:%f", a / b);
break;
}
printf("\n\nDo you want to continue: (y for yes and n for no) :");
scanf(" %c", &q); // use " %c" instead of "%c", the white space
// will absorb any blank space (including newlines)
} while (q == 'y'); // continue loop if user has entered 'y'
return 0;
}
This question already has answers here:
program wont read at second scanf()
(2 answers)
Closed 2 years ago.
I am making a mini project i c with the help of nested switch statement in my code Error is in 15th line of the code,here %c is not working but %s is working,please help me to solve this query and tell me how to run this code with the use of %c here is my code:-
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,ch;
char choice;
clrscr();
printf("1.Calculator\n2.Convrter\n\n");
printf("Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("1.Addition(A)\n2.Subtraction(S)\n3.Multiplication(M)\n4.Division(D)\n5.Module(P)\n\n");
printf("Enter your choice : ");
**scanf("%c",&choice);**
switch(choice)
{
case 'A':printf("Provide the value of a : ");
scanf("%d",&a);
printf("Provide the value of b : ");
scanf("%d",&b);
printf("\n");c=a+b;
printf("%d",c);
break;
case 'S':printf("Provide the value of a : ");
scanf("%d",&a);
printf("Provide the value of b : ");
scanf("%d",&b);
printf("\n");c=a-b;
printf("%d",c);
break;
case 'M':printf("Provide the value of a : ");
scanf("%d",&a);
printf("Provide the value of b : ");
scanf("%d",&b);
printf("\n");c=a*b;
printf("%d",c);
break;
case 'D':printf("Provide the value of a : ");
scanf("%d",&a);
printf("Provide the value of b : ");
scanf("%d",&b);
printf("\n");c=a/b;
printf("%d",c);
break;
case 'P':printf("Provide the value of a : ");
scanf("%d",&a);
printf("Provide the value of b : ");
scanf("%d",&b);
printf("\n");c=a%b;
printf("%d",c);
break;
default:printf("Invalid Input");
}
break;
}
getch();
}
you just need to add getchar(); before
scanf()
because you will take \n as char choice
so one getchar will solve your problem
case 1:printf("1.Addition(A)\n2.Subtraction(S)\n3.Multiplication(M)\n4.Division(D)\n5.Module(P)\n\n");
printf("Enter your choice : ");
getchar();
scanf("%c", &choice);
switch (choice)
%s will work because it takes more than one char.
or you can:
1:printf("1.Addition(A)\n2.Subtraction(S)\n3.Multiplication(M)\n4.Division(D)\n5.Module(P)\n\n");
printf("Enter your choice : ");
scanf(" %c", &choice); //add space before %c
switch (choice)
as H.S. said in comment.
The previous call to scanf():
scanf("%d",&ch);
in Line 10, leaves the newline character (\n) made by the push to Enter/Return inside stdin.
This newline character is read on the second scanf() call since the %c format/conversion specifier does not ignore leading white space and also read/consumes non-printable characters.
Just insert white space before the conversion specifier %c:
scanf(" %c",&choice);
to catch that newline character or any leading white space.
This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 3 years ago.
In this C program, if I use fflush(stdin) before the second scanf, then it's working fine. And also scanning the operator before the operands, then it's working. I don't understand, why it's working like that.
#include<stdio.h>
int main() {
int a,b,c;
char d;
printf("Enter two operands:");
scanf("%d%d",&a,&b);
printf("\nEnter the operation you desire to perform on Calculator and i.e +, -, *, / :\n");
fflush(stdin);
scanf("%c",&d);
switch(d) {
case '+': printf("\n%d %c %d =%d",a,d,b,(a+b));
break;
case '-': printf("\n%d %c %d =%d",a,d,b,(a-b));
break;
case '*': printf("\n%d %c %d =%d",a,d,b,(a*b));
break;
case '/': (a>b)?(printf("\n%d %c %d =%d",a,d,b,(a/b))):(printf("\n%d %c %d =%d",a,d,b,(b/a)));
break;
default: printf("\nInvalid input");
}
return 0;
}
Output:
Just modify your code to this :
#include<stdio.h>
int main() {
int a,b,c;
char d;
printf("Enter two operands:");
scanf("%d%d",&a,&b);
printf("\nEnter the operation you desire to perform on Calculator and i.e +, -, *, / :\n");
// try give a space like " %c"
scanf(" %c",&d);
switch(d) {
case '+': printf("\n%d %c %d =%d",a,d,b,(a+b));
break;
case '-': printf("\n%d %c %d =%d",a,d,b,(a-b));
break;
case '*': printf("\n%d %c %d =%d",a,d,b,(a*b));
break;
case '/': (a>b)?(printf("\n%d %c %d =%d",a,d,b,(a/b))):(printf("\n%d %c %d =%d",a,d,b,(b/a)));
break;
default: printf("\nInvalid input");
}
return 0;
}
It is working fine :)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
This code executes itself without asking for char "choose" for further processing before the switch statement scanf("%c",&choose); switch(choose).
printf("enter option 1,2,3 or 4 \n\n");
scanf("%c",&choose); // why it not ask to input char
switch(choose)
{
case '1': case '+':
{
printf("enter 1st value\n");
scanf("%f",&a);
printf("enter 2st value\n");
scanf("%f",&b);
c=a+b;
printf("%f + %f = %f",a,b,c);
break;
}
case '2': case '-':
{
printf("enter 1st value\n");
scanf("%f",&a);
printf("enter 2nd value\n");
scanf("%f",&b);
c=a-b;
printf("%f - %f = %f",a,b,c);
break;
}
case '3': case'*':
{
printf("enter 1st value\n");
scanf("%f",&a);
printf("enter 2nd value\n");
scanf("%f",&b);
c=a*b;
printf("%f * %f = %f",a,b,c);
break;
}
}
printf("enter option 1,2,3 or 4 \n\n");
scanf("%c",&choose);
In your code if you are using any scanf before the above given code, then this behaviour is expected as the previous scanf reads the input leaving \n in the input buffer which is feeded as input to next scanf.
To overcome this issue please use fflush(stream) before scanf(...), this should solve the problem.
fflush(stdin);
or you can use getchar() function, this will alse solve your the issue.
Probably because you've already input something earlier, so that there are non-scanned characters left in the buffer which this "greedy" scanf() then scans without stopping.
You should check the return value of scanf() to learn if it succeeded or not.
And you should switch to using fgets() to read in a whole line, and then parse that. It's much safer and easier to predict, that way.
I've executed it without any problems (it gets the "choose" char):
#include <stdio.h>
int main(int argc, char *argv ) {
char choose;
float a,b,c;
printf("enter option 1,2,3 or 4 \n\n");
scanf("%c",&choose); // why it not ask to input char
switch(choose)
{
case '1': case '+':
{
printf("enter 1st value\n");
scanf("%f",&a);
printf("enter 2st value\n");
scanf("%f",&b);
c=a+b;
printf("%f + %f = %f",a,b,c);
break;
}
case '2': case '-':
{
printf("enter 1st value\n");
scanf("%f",&a);
printf("enter 2nd value\n");
scanf("%f",&b);
c=a-b;
printf("%f - %f = %f",a,b,c);
break;
}
case '3': case'*':
{
printf("enter 1st value\n");
scanf("%f",&a);
printf("enter 2nd value\n");
scanf("%f",&b);
c=a*b;
printf("%f * %f = %f",a,b,c);
break;
}
}
return 0;
}
Probably its something you didn't read before...
I'm guessing that you are doing a similar "scanf("%c",&choose);" before, so the next read you will get the carriage return.
You could try to change the following lines:
scanf("%c",&choose);
for:
scanf("%c%*c",&choose);
...so you discard the \n
If this don't solve your issue, maybe you should provide more code :)