Once again this is a HW assignment, and I'm not asking for answers, but simply what I'm doing wrong, and how I should go about fixing it. The assignment is as follows:
**************************************************
You are to write a program that evaluates simple arithmetic expressions which are composed by positive numbers (real or integers) and the four basic arithmetic operators: +, -, * and /. The program will have to adhere to the following rules:
The expressions will be evaluated left to right (all operators will have the same precedence).
Blank spaces are ignored.
The expression is terminated by the end of the line, at which point you will display the answer.
The prompt is indicated with >.
You have to guard against illegal characters entered.
The program can be terminated only by pressing <CTRL-C>.
Ex:
> 2 + 3 * 4/6
3.333333
> 2/4+3
3.500000
> 2!/4+3
*** Illegal character !
>
Notes and Hints:
Since you want to loop indefinitely (until you enter <CTRL-C> ), you have to set up an infinite loop:
do
{
/* Body of loop */
} while( 1 );
To test for end-of-line, you have to verify if the character read is equal to '\n'.
If you've encountered an error, before redisplaying the prompt, you need to clean the input line. This can be accomplished with the following code:
while( (c = getchar()) != '\n' ) ;
You want to do this ONLY IF THERE WAS AN ILLEGAL CHARACTER
**************************************************
Here now is my code:
Code:
/* This programevaluates simple arithmetic expressions which
are composed by positive numbers (real or integers) and
the four basic arithmetic operators: +, -, * and /. */
#include <stdio.h>
int main()
{
int Answer = 0, integer1 = 0 ;
char c;
do
{
printf("Please Enter the Mathematical Expression you want to be evaluated:\n" );
printf(">");
scanf("%d", &Answer);
c = getchar();
switch (c) { /* switch nested in while */
case ' ': /* Read over white space */
scanf("%d",&integer1);
Answer = Answer;
break;
case '+': /* Operand was + */
scanf("%d",&integer1);
Answer = Answer + integer1;
break;
case '-': /* Operand was - */
scanf("%d",&integer1);
Answer = Answer - integer1;
break;
case '/': /* Operand was / */
scanf("%d",&integer1);
Answer = Answer / integer1;
break;
case '*': /* Operand was * */
scanf("%d",&integer1);
Answer = Answer * integer1;
break;
case '\n':
printf("%d", Answer);
break;
default: /* Operand was an invalid character */
while( (c = getchar()) != '\n' );
printf("*** Illegal character!\n");
break;
}
} while(1);
return 0;
}
*************************************
I've used the debugger and the problem appears to be that it isnt reading the number correctly. For example if I enter 5+5 it'll read 5 as like 7.055e-45. It then will read the correct operator (+) and then read the second number incorrectly as well. Thats problem one.
Problem 2, is that it does not print the answer. So if i do 2+4, it'll just go through the while loop and ask to enter the next expression, while enevr dispalying the asnwer to the first expression.
I am totally confused and have tried many different things. Any Help is GREATLY appreciated. Thank you.