Stuck on a problem. Help needed please.

This is a discussion on Stuck on a problem. Help needed please. within the C Programming forums, part of the General Programming Boards category; Hello, I am teaching myself programming using Kochan's "Programming in C" book. I am stuck on an exercise and was ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    49

    Stuck on a problem. Help needed please.

    Hello, I am teaching myself programming using Kochan's "Programming in C" book.

    I am stuck on an exercise and was hoping you guys could help me out. Here is the question:

    4. Write a program that acts as a simple “printing” calculator.

    The program should allow the user to type in expressions of the form:

    number operator

    The following operators should be recognized by the program:

    + - * / S E

    The S operator tells the program to set the “accumulator” to the typed-in number.

    The E operator tells the program that execution is to end.

    The arithmetic operations
    are performed on the contents of the accumulator with the number that was keyed in acting as the second operand.

    The following is a “sample run” showing how the program should operate:

    Begin Calculations
    10 S //Set Accumulator to 10
    = 10.000000 //Contents of Accumulator
    2 / //Divide by 2
    = 5.000000 //Contents of Accumulator
    55 - //Subtract 55
    -50.000000
    100.25 S //Set Accumulator to 100.25
    = 100.250000
    4 * //Multiply by 4
    = 401.000000
    0 E //End of program
    = 401.000000
    End of Calculations.


    Make certain that the program detects division by zero and also checks for unknown operators.

    Thank you so much for your help guys!

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    643
    I would suggest looking into a Case Switch menu to get you started with your outline.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    2,706
    What is your code thus far?

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    49

    code thus far

    Quote Originally Posted by Matticus View Post
    What is your code thus far?
    Ok I think I mainly got it. The only thing I don't understand is it will accept a user input with no spaces (such as 5S or 2*) for everything except for the E operator. In that case the user has to format it like 0 E (with a space in between 0 and E). Let me know what you think:

    Code:
    #include <stdio.h>
    
    int main (void)
    
    {
        float number, a;
        char operator = 'S';
        
        printf("Begin calculations. \n");
    
     while (operator != 'E')
    {
        scanf("%f  %c", &number, &operator);
        
         switch (operator)
       {
            case 'S':
                a = (number);
                printf ("= %f\n", a);
                break;
           
            case '+':
                a = (a + number);
                printf ("= %f\n", a);
                break;
                
            case '-':
                a = (a - number);
                printf ("= %f\n", a);
                break;
                
            case '*':
                a = (a * number);
                printf ("= %f\n", a);
                break;
                
           case '/':
                if (number == 0)
                printf("You can't divide by zero, idiot!\n");
                else
                a = (a / number);
                printf ("= %f\n", a);
                break;
                
           case 'E':
                printf ("= %f\n End of Calculations.\n", a);
                break;
                
           default:
                printf ("Unknown operator.\n");
                break;
                
       }
          
    }
    }
    Last edited by sdbuilt; 09-05-2012 at 11:33 PM.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    2,706
    The only thing I don't understand is it will accept a user input with no spaces (such as 5S or 2*) for everything except for the E operator.
    Because you are asking for two inputs; a floating point value, and a character.

    But "0E..." is scientific notation for a floating point value, so the entire argument is taken as a single statement; 0E (which equates to 0^something which equals zero) and therefore no following character is read.

    [Edit] If in doubt, play with it.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        float fTest;
        char  cTest;
    
        scanf("%f  %c",&fTest,&cTest);
    
        printf("%.6e\t%c\n",fTest,cTest);
    
        return 0;
    }
    
    // input 1:
    // 4R
    // output 1:
    // 4.000000e+3   R
    //
    // input 2:
    // 0E
    // (nothing happens when I press 'enter', so I then type a 'w'...)
    // w
    // (...and get an output)
    // output 2:
    // 0.000000+e000   w
    //
    // FINAL TEST:
    // input 3:
    // 1E-3q
    // output 3:
    // 1.000000e-003  q
    Last edited by Matticus; 09-06-2012 at 12:07 AM.

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    49
    Ahhh I see.

    Quote Originally Posted by Matticus View Post
    Because you are asking for two inputs; a floating point value, and a character.

    But "0E..." is scientific notation for a floating point value, so the entire argument is taken as a single statement; 0E (which equates to 0^something which equals zero) and therefore no following character is read.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stuck on a practice problem- modulus operator
    By promitheus in forum C++ Programming
    Replies: 4
    Last Post: 08-22-2012, 09:02 PM
  2. Stuck on lab problem
    By Maveryc in forum C Programming
    Replies: 2
    Last Post: 10-07-2011, 08:00 PM
  3. stuck on namespace problem
    By officedog in forum C Programming
    Replies: 5
    Last Post: 11-20-2008, 06:29 PM
  4. Stuck on Input problem
    By Syneris in forum C++ Programming
    Replies: 4
    Last Post: 01-23-2006, 10:53 PM
  5. Stuck! (easy problem)
    By Tynnhammar in forum C++ Programming
    Replies: 4
    Last Post: 09-29-2004, 11:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21