Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include<stdio.h>
#include<conio.h>
int main()
{	int a,b;
	char ch;
	printf("\nEnter a number");
	scanf("%d",&a);
	printf("\nEnter another number");
	scanf("%d",&b);
	printf("\nEnter the symbol");
	ch=getchar();
	switch(ch)
	{
	case '+':
	printf("%d",a+b);
	break;
	case '-':
	printf("%d",a-b);
	break;
	}
	getch();
	return 0;
}

The program terminates when I enter the symbol.
Posted
Updated 2-Jun-15 4:19am
v3
Comments
[no name] 2-Jun-15 10:25am    
There is nothing really wrong with your switch. Your problem comes from using scanf, http://c-faq.com/stdio/stdinflush2.html

First of all use correct indentation to make code more readable.
Your problem is that the carriage return and possibly other keys from previous scanf remained in the input buffer and are read by the next scanf. In that case you will pass to the switch a \n that will perform no operation.
To clean the buffer you have many options, the first is to use the scanf '*' qualifier to remove anything from input:
C++
scanf("%d%*c",&b);
printf("\nEnter the symbol");
ch=getchar();
switch(ch)

Or you can clean the buffer by yourself:
C++
scanf("%d",&b);
    while ((ch = getchar()) != EOF && ch != '\n') ;  //Remove everything up to \n
printf("\nEnter the symbol");
ch=getchar();
switch(ch)

On Windows systems, absolutely not standard C, you can simply flush the input:
C++
fflush(stdin);
 
Share this answer
 
"The program terminates when I enter the symbol."

Well...yes, it will.

All you have told it to do is read a character, print (if it matches) and read another character.

But...to enter the character so getch can read it, you need to enter the character followed by an ENTER. Which counts as a character, so the second call to getch fetches the ENTER you already supplied, and so you program ends.

Instead of using getch, use another scanf to read a whole line (or better, read a whole line and parse it into number-symbol-number yourself) and use the symbol from that.
 
Share this answer
 
There is nothing fundamentally wrong, except bad code formatting, so the boundaries of cases are not well readable.
However, potentially it can be wrong; and something can be fixed right away.

Sure, the program terminates when you press the symbol. This is just because you programmed it this way. I don't even want to address this "problem", because this is not a problem. Cycle you code, or do anything else you want. Essentially, add some code which should do what you want.

First of all, hard-coded '-' and '+'. What if you want to change these "keys" or change the set of them? Then hard-coding of those immediate constants would make code unsupportable. The rule of thumb is: if you use some immediate constant in two or more place, it should be not there, because the change would require going in two places, which is a nasty kind of work and a sure source of bugs. You should explicitly declare such constants.

Another problem is only potential one. What if your switch statement becomes long, like one of those long handlers of Windows messages? This would be a problem of both readability and maintenance. In such cases, this is a sign the refactoring of code would be required. You could have some container of key-value pairs, where the value could be an action, that is, function reference, with separate functions describing each action. I can show you one approach, but this is just to show an idea, as the code is unrelated to C; this is my article Dynamic Method Dispatcher (Attention: not C!).

Something similar could be implemented in C as well. Some said: it makes no sense, because the optimization step of the compiler will itself create a table of key-action pairs with fast search of the action by a key. That may be true, but I don't talk about optimization here (for something happening on the key press, extra millisecond just don't count; no one will notice the extra delay). I am talking about readability and maintainability.

—SA
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900