Like Tree2Likes
  • 1 Post By ZuK
  • 1 Post By ZuK

Simple Multi-Session Calculator: Switch Case and While Loop Problem in C

This is a discussion on Simple Multi-Session Calculator: Switch Case and While Loop Problem in C within the C Programming forums, part of the General Programming Boards category; Hello Everyone, I have a problem with my simple operations calculator code (using C, in Code::Blocks) . I am required ...

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    14

    Question Simple Multi-Session Calculator: Switch Case and While Loop Problem in C

    Hello Everyone,

    I have a problem with my simple operations calculator code (using C, in Code::Blocks). I am required to use a while loop statement so the user can execute multiple step operations without re-opening the program. When I launch the program, I get through the first session fine, but when I'm on the second session, when entering the two operands and press enter (to calculate), it just gives me the return and say press any key to continue (exit). You could run it, if it helps anything. I am so close, I've been working on this problem for 1.5 days, I'm sure it is something simple, please help! Thanks!

    Here is the code:

    Code:
    # include <stdio.h>
    int main()
    {
    int num1, num2;
    char op;
    int finished = 0;
    
    
    
    
    
    
        printf("Simple Operations Calculator\n");
        printf(" \n");
        
        printf("Enter your desired operation (+, -, *, /): ");
        scanf("%c", &op);
        printf("Enter your two desired numbers: ");
        scanf("%d%d", &num1, &num2);
    
    
        switch(op)
        {
            case '+':
                printf("num1 + num2 = %.2d\n", num1+num2);
                break;
    
    
            case '-':
                printf("num1 - num2 = %.2d\n", num1-num2);
                break;
    
    
            case '*':
                printf("num1 * num2 = %.2d\n", num1*num2);
                break;
    
    
            case '/':
                printf("num1 / num2 = %.2d\n", num1/num2);
                break;
    
    
            default:
                printf("Error! That is not an operation option.");
                break;
    
    
        }
          while(!finished){
            printf("Enter your desired operation (+, -, *, /): ");
            scanf("%c", &op);
            getchar();
            printf("Enter your two desired numbers: ");
            scanf("%d%d", &num1, &num2);
    
    
            getchar();
            return 0;
    
    
            }
    
    
    }

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    your loop should include your calculation code... how else will it be repeated?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    14
    So put the switch case within the while loop as well?

  4. #4
    Registered User
    Join Date
    Jun 2013
    Posts
    14
    Quote Originally Posted by vart View Post
    your loop should include your calculation code... how else will it be repeated?
    I put the switch case in the while loop, but when I enter the two desired operands, it gives me the default, "Error! That is not an operation option!"

    Code:
    # include <stdio.h>
    int main()
    {
    int num1, num2;
    char op;
    int finished = 0;
    
    
    
    
    
    
        printf("Simple Operations Calculator\n");
        printf(" \n");
    
    
        printf("Enter your desired operation (+, -, *, /): ");
        scanf("%c", &op);
        printf("Enter your two desired numbers: ");
        scanf("%d%d", &num1, &num2);
    
    
        switch(op)
        {
            case '+':
                printf("num1 + num2 = %.2d\n", num1+num2);
                break;
    
    
            case '-':
                printf("num1 - num2 = %.2d\n", num1-num2);
                break;
    
    
            case '*':
                printf("num1 * num2 = %.2d\n", num1*num2);
                break;
    
    
            case '/':
                printf("num1 / num2 = %.2d\n", num1/num2);
                break;
    
    
            default:
                printf("Error! That is not an operation option.");
                break;
    
    
        }
          while(!finished){
            printf("Enter your desired operation (+, -, *, /): ");
            scanf("%c", &op);
            getchar();
            printf("Enter your two desired numbers: ");
            scanf("%d%d", &num1, &num2);
    
    
            switch(op)
        {
            case '+':
                printf("num1 + num2 = %.2d\n", num1+num2);
                break;
    
    
            case '-':
                printf("num1 - num2 = %.2d\n", num1-num2);
                break;
    
    
            case '*':
                printf("num1 * num2 = %.2d\n", num1*num2);
                break;
    
    
            case '/':
                printf("num1 / num2 = %.2d\n", num1/num2);
                break;
    
    
            default:
                printf("Error! That is not an operation option.");
                break;
    
    
        }
    
    
            getchar();
            return 0;
    
    
            }
    
    
    }
    Last edited by critixal; 06-30-2013 at 01:00 PM. Reason: mis-spelled a word

  5. #5
    Registered User
    Join Date
    Jun 2013
    Posts
    14
    Quote Originally Posted by critixal View Post
    I put the switch case in the while loop, but when I enter the two desired operands, it gives me the default, "Error! That is not an operation option!"

    Code:
    # include <stdio.h>
    int main()
    {
    int num1, num2;
    char op;
    int finished = 0;
    
    
    
    
    
    
        printf("Simple Operations Calculator\n");
        printf(" \n");
    
    
        printf("Enter your desired operation (+, -, *, /): ");
        scanf("%c", &op);
        printf("Enter your two desired numbers: ");
        scanf("%d%d", &num1, &num2);
    
    
        switch(op)
        {
            case '+':
                printf("num1 + num2 = %.2d\n", num1+num2);
                break;
    
    
            case '-':
                printf("num1 - num2 = %.2d\n", num1-num2);
                break;
    
    
            case '*':
                printf("num1 * num2 = %.2d\n", num1*num2);
                break;
    
    
            case '/':
                printf("num1 / num2 = %.2d\n", num1/num2);
                break;
    
    
            default:
                printf("Error! That is not an operation option.");
                break;
    
    
        }
          while(!finished){
            printf("Enter your desired operation (+, -, *, /): ");
            scanf("%c", &op);
            getchar();
            printf("Enter your two desired numbers: ");
            scanf("%d%d", &num1, &num2);
    
    
            switch(op)
        {
            case '+':
                printf("num1 + num2 = %.2d\n", num1+num2);
                break;
    
    
            case '-':
                printf("num1 - num2 = %.2d\n", num1-num2);
                break;
    
    
            case '*':
                printf("num1 * num2 = %.2d\n", num1*num2);
                break;
    
    
            case '/':
                printf("num1 / num2 = %.2d\n", num1/num2);
                break;
    
    
            default:
                printf("Error! That is not an operation option.");
                break;
    
    
        }
    
    
            getchar();
            return 0;
    
    
            }
    
    
    }



    Come on, could anyone help me?

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    The second time there will be a \n in the input buffer and the scanf("%c", &op ) will read that as op.
    simple fix: put getchar(); after scanf("%d%d", &num1, &num2);

    Kurt
    critixal likes this.

  7. #7
    Registered User
    Join Date
    Jun 2013
    Posts
    14
    Quote Originally Posted by ZuK View Post
    The second time there will be a \n in the input buffer and the scanf("%c", &op ) will read that as op.
    simple fix: put getchar(); after scanf("%d%d", &num1, &num2);

    Kurt
    Thanks Kurt! I have another question.
    The two sessions work, but after the second, it does not allow me to do another session.
    I want to be able to do as many sessions as I want.
    Do you know the solution to this?

  8. #8
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by critixal View Post
    Do you know the solution to this?
    Don't return from inside the loop.
    If you'd indent the code better the mistake would be obvious.
    Kurt
    critixal likes this.

  9. #9
    Registered User
    Join Date
    Jun 2013
    Posts
    14
    Quote Originally Posted by ZuK View Post
    Don't return from inside the loop.
    If you'd indent the code better the mistake would be obvious.
    Kurt
    Sorry about that, I just started C programming, well programming in general, on Thursday. I probably wouldn't have know that anyway. but I will try to improve my indenting skills for programming. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch case loop
    By justin8077 in forum C Programming
    Replies: 4
    Last Post: 03-16-2011, 09:32 AM
  2. C tutorial Switch..case loop
    By dunsta in forum C Programming
    Replies: 8
    Last Post: 04-18-2010, 04:55 AM
  3. The Multi-Session API
    By rchiu5hk in forum C++ Programming
    Replies: 3
    Last Post: 09-15-2009, 02:44 AM
  4. switch-case,Infinite while-loop ,break
    By babu198649 in forum C++ Programming
    Replies: 8
    Last Post: 09-20-2008, 08:12 AM
  5. Multi session disk
    By GSLR in forum Tech Board
    Replies: 11
    Last Post: 11-24-2003, 06:32 PM

Tags for this Thread


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