Thread: simple printing calculator in C doesn't work

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    3

    simple printing calculator in C doesn't work

    Hello everyone,
    I am new to C and don't have a programming background. So I'm trying to learn C as my first step into programming. Here is the problem I am confronted with (my code below):

    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.

    This is what I came up with but it doesn't work. It compiles fine (using gcc -Wall -o ...) but doesn't do what I want it to.

    Thanks for taking a look at it and letting me know where I made a mistake.

    Code:
    #include <stdio.h>
    
    int main (void)
    {
        float val, tmp;
        char acc;
    
        printf("Beginning Calculations. \n");
        printf("Set initial value of accumulator.\n");
        printf("Use 's' to store value in accumulator.\n");
        printf("Example: 10 s stores the value 10 in the accumulator.\n");
    
         printf("Use 'e' to end program.\n");
    
        scanf("%f%c", &val, &acc);
    
        do {
            switch (acc)
            {
                case 's':
                    tmp = val;
                    break;
                case '+':
                    printf("%.f\n", tmp + val);
                    tmp = tmp + val;
                    printf("= %.2f\n", tmp);
                    break;
                case '-':
                    printf("%.2f\n", tmp - val );
                    tmp = tmp - val;
                    printf("= %.2f\n", tmp);
                    break;
                case '*':
                    printf("%.2f\n", tmp * val);
                    tmp = tmp * val;
                    printf("= %.2f\n", tmp);
                    break;
                case '/':
                    if (val == 0)
                        printf("Division by zero not possible.\n");
                    else
                        printf("%.2f\n", tmp / val);
                        tmp = tmp / val;
                        printf("= %.2f\n", tmp);
                    break;
                case 'e':
                    printf("Good day!\n");
                    break;
                default:
                    printf("Unknown operator.\n");
                    break;
            }
        }
        while (acc != 'e');
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Please ask a more specific question. Why isn't your program doing what you want? What did you input into your program? What did your program output with those inputs? What did you expect your program to output?

    Jim

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    3
    ah sorry. Here is what I input:
    10s
    5+
    (Here is supposed to be a total 15. But I don't get one.)
    2*
    (Here is supposed to a total 30. But I don't get one.)
    e (to end program)

    But I never get a total and can't get out of the program.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You probably want to put the scanf() inside the 'do-while' loop, before the 'switch'.

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    3
    great that helped thanks. I know have the totals but I still cant get out of the program. when I enter 'e' it loops non stop.
    e
    =inf
    inf
    =inf
    inf
    .
    .
    .

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    When you try to input a character into a number scanf() will fail, and no further processing of input from that stream will occur until reset. If you test the return value from scanf() to insure that the correct number of entries were accepted you can exit your loop when it fails. You may want to look at the documentation for scanf().

    Jim

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You have to figure out how to take the new-line character and ignore it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why the simple Socket Snippet doesn't work?
    By meili100 in forum Networking/Device Communication
    Replies: 2
    Last Post: 05-05-2008, 04:57 AM
  2. Simple C++ code doesn't work
    By alex_dude_122 in forum C++ Programming
    Replies: 6
    Last Post: 10-18-2006, 12:53 PM
  3. simple fread doesn't work
    By ronenk in forum C Programming
    Replies: 3
    Last Post: 10-15-2004, 05:08 AM
  4. Simple class doesn't work
    By Cris987 in forum C++ Programming
    Replies: 7
    Last Post: 01-08-2004, 11:18 PM
  5. Why doesn't this work? (simple)
    By Clyde in forum C Programming
    Replies: 3
    Last Post: 04-02-2002, 04:48 PM

Tags for this Thread