Thread: I still have some problems

  1. #1
    Mike Jones
    Guest

    I still have some problems

    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.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I don't know whether changing the value of a variable by using the variable twice in the expressiong is allowed in C++ (i.e. Answer = Answer - something_else). This is a bit of a long shot, but try changing all those to Answer -= something_else. Other than that, I can't see anything else wrong with it.

  3. #3
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Talking

    Well Well Well,
    In the first instance, let me assure u that C/C++ does not provide you with an inbuilt provision of displaying the values of any variables once their values are computed. You EXPLICITLY should use the printf (or alike) statement to display the result. After having gone through ur code, the first thing I noticed was that u do not have a

    printf("%d",Answer);

    statement in ur code... put it after u close the braces for the Switch statement. Then half of ur problem (for displaying the result) would come to an end.

    One more thing that I noticed was that ur program was only handling a single operation between two operands. and it does not provide u with a routine to perform computations on other operands. CHeck out...

    Best Wishes,
    Sriharsha.
    Help everyone you can

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 09:19 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM