Thread: An Exercise i am unable to complete fully, Please help.

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    13

    An Exercise i am unable to complete fully, Please help.

    Hey there, im currently trying to learn c and im unable to complete this own program,

    "write a program that acts as a simple "printing" calculator. this program should allow the user to type in expressions of the form
    number operator
    the following operators should be recognised by 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.000 //contents of accumulator
    2 / //divide by 2
    =5.000 //contents of accumulator
    55 - //subtract 55
    =-50.000
    100.25 S //set accumulator to 100.25
    =100.250
    4 * //multiply by 4
    =401.000
    0 E //end of program
    =401.00
    end of calculations

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

    thank you .

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Presumably when you say "unable to complete", means you've actually "made a start".

    Post what you've got.

    Can you read these lines and just print them back to the user?
    10 S
    2 /
    55 -
    100.25 S
    4 *
    0 E


    Can you parse said lines to extract a number and a character?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Ok, you were unable to complete it, but did you start it? What have you tried so far? Post your attempt here, in code tags.

    Just in case you expect us to solve the whole exercise for you, please read our homework policy, as well as a few forum guidelines while you're at it:
    https://cboard.cprogramming.com/c-pr...uncements.html
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Feb 2017
    Posts
    13
    An Exercise i am unable to complete fully, Please help.-screenshot-2017-02-01-00-46-30-jpg

  5. #5
    Registered User
    Join Date
    Feb 2017
    Posts
    13
    this isnt a homework just an exercise from a programming in c book by stephen G. kochan. trying to learn how to code for my university

  6. #6
    Registered User
    Join Date
    Feb 2017
    Posts
    13
    you input those lines in continuously order after each output thats where im stuck, i get the program to work but im allowed to input once and the program ends automatically after an the 1st output

  7. #7
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    A fuzzy picture is a poor substitute to posting the code itself as text. Remember to put it in code tags.

  8. #8
    Registered User
    Join Date
    Feb 2017
    Posts
    13
    Code:
     
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main ()
    {
    
        float value1, A;
        char operator;
        
    
        printf("Enter in an expression: \n");
        scanf("%f %c ", &value1, &operator);
        
        
        
        switch(operator)
    {
        
        case 'S':
                    A = value1;
                    printf("%.3f\n", A);
                    break;
        
        case '+':
                    printf("%.3f\n", value1 + A);
                    break;
        case '-':
                    printf("%.3f\n", value1 - A);
                    break;
        case '*':
                    printf("%.3f\n", value1 * A);
                    break;
        case '/': 
                    if (value1 == 0)
                    printf("Error not possible, please try again\n");
                    
                    else 
                    printf("%.3f\n", value1 / A);
                    break;
                    
        case 'E':     
                    exit(1);
            
        
        
        default:
                    printf("Unknown operator\n");
                    break;
    }                
    
            
                    return 0;
    }

  9. #9
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You need a loop around pretty much the whole thing.
    Code:
    while (1) {
        printf();
        scanf();
        switch() {
        }
    }
    You should get rid of the extra space at the end of the scanf format. It will cause the input to hang there.

    You need to "accumulate" the answer, so you need to set A to the new value every time. You could probably print the current value in one place, after the switch.

    You may as well allow both upper- and lower-case S and E.
    Code:
    case 'E': case 'e':
        exit(0);
    An exit value (or return value from main) of non-zero generally indicates program failure.

  10. #10
    Registered User
    Join Date
    Feb 2017
    Posts
    13
    thank you it worked, appreciate everyones help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 01-04-2012, 11:00 PM
  2. cant fully understand what these lines mean?
    By transgalactic2 in forum C Programming
    Replies: 2
    Last Post: 01-26-2009, 03:08 PM
  3. Search function not working fully
    By tabstop in forum C Programming
    Replies: 7
    Last Post: 12-04-2008, 02:57 AM
  4. Pipe not working fully
    By Heraclitus in forum Linux Programming
    Replies: 1
    Last Post: 03-05-2003, 10:09 AM
  5. Fully understanding classes ...
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 08-14-2002, 11:48 AM

Tags for this Thread