Need a little help with my calculator program

I've made a simple Calculator program.The program is working fine except one thing.
After running the program it asks me to enter choice.Now suppose I enter
1.and then enter two numbers and add,After addition instead of showing "Enter you choice" it shows-
"Enter your choice
Not a good choice"
So my question is how should I remove this extra "not a good choice"?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  #include <stdio.h>
int main()
{
	int a,b,c;
	char x;
	printf("What would you like to do\n");
	printf("1. Addition\n");
	printf("2. Multiplication\n");
	printf("3. Division\n");
	printf("4. Subtraction\n");
	for(;;)
	{
	printf("Enter your choice- 1,2,3 or 4\n");
	x=getchar();
	if(x=='1')
	{
		printf("Enter two numbers\n");
		scanf("%d""%d",&a,&b);
		c=a+b;
		printf("The answer is %d \n",c);
	}
	else if(x=='2')
	{
		printf("Enter two numbers\n");
		scanf("%d""%d",&a,&b);
		c=a*b;
		printf("The answer is %d \n",c);
	}
	else if(x=='3')
	{
		printf("Enter two numbers\n");
		scanf("%d""%d",&a,&b);
		c=a/b;
		printf("The answer is %d \n",c);
	}
	else if(x=='4')
	{
		printf("Enter two numbers\n");
		scanf("%d""%d",&a,&b);
		c=a-b;
		printf("The answer is %d \n",c);
	}
	else
	{
		
		printf("Not a good choice\n");

	}
	}
	return(0);
}
Last edited on
http://www.cplusplus.com/forum/general/106571/#msg576974
http://www.cplusplus.com/forum/general/106735/#msg578034

getchar reads just one character from the input stream buffer, but whenever you type a value, you are actually typing 2 characters:

The number and a newline...
At the end of your for-loop, write:

getchar();
This will read the newline character and discard it since you do nothing with it or you can implement one of the methods in the 2 links above
Thanks for helping me.My program is working now.
Topic archived. No new replies allowed.