Thread: Add loop to calculator

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

    Add loop to calculator

    So I've been coding for nearly 5 days and managed to make this calculator with some effort. Might not be the best syntax arrangement but it works. Problem is every time it does a calculation, it closes down and I have to open it up again manually.

    I'd like to put a loop in the code so that it repeats this line...

    Code:
            printf("1 for adding up, 2 for subtraction, 3 for multiplication and 4 for division.\n");
            scanf("%i", &selector);
    ...when it's done a calculation so that I can redo it without having to press F10 on Dev-C++ again.

    And I have literally NO CLUE how to include the while (or for?) argument in it in order to achieve that. I'm literally clueless. I don't know how to make it work so I need to ask you guys how to do it.

    Thanks a lot.

    Code:
    #include <stdio.h>
    
    main()
    {    
    
    
        int selector, alg1, alg2;
    
    
    
    
            printf("1 for adding up, 2 for subtraction, 3 for multiplication and 4 for division.\n");
            scanf("%i", &selector);
        
            printf("Enter two operands:\n");
            scanf("%i %i",&alg1, &alg2);
            
            switch(selector)
            {
            
                case 1:
                    printf("%i + %i = %i", alg1, alg2, alg1 + alg2);
                        break;
        
                case 2:
                    printf("%i - %i = %i", alg1, alg2, alg1 - alg2);
                        break;
        
                case 3:
                    printf("%i * %i = %i", alg1, alg2, alg1 * alg2);
                        break;
        
                case 4:
                    printf("%i / %i = %i", alg1, alg2, alg1 / alg2);
                        break;
    
    
                default:
                    printf("Invalid");    
            }
        }

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Strangely, the code that belongs inside the while loop is already properly indented. Put a while(1) on line 10 and an opening brace on the next line. Add a closing brace at the very end. There's your loop.

    After you scanf the selector, test if it's 0 and if it is, break (out of the loop). Add a "0 to quit" option to the menu.

  3. #3
    Registered User
    Join Date
    Feb 2017
    Posts
    3
    Quote Originally Posted by algorism View Post
    Strangely, the code that belongs inside the while loop is already properly indented. Put a while(1) on line 10 and an opening brace on the next line. Add a closing brace at the very end. There's your loop.

    After you scanf the selector, test if it's 0 and if it is, break (out of the loop). Add a "0 to quit" option to the menu.
    Could you be even more specific? I'm really in the very beginning. Even with these instructions I can't figure it out what to do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-07-2017, 11:55 AM
  2. Loop a calculator program.
    By lulzintosh in forum C Programming
    Replies: 7
    Last Post: 06-05-2012, 09:41 AM
  3. Calculator loop problem
    By Laika1986 in forum C Programming
    Replies: 5
    Last Post: 10-22-2011, 12:46 PM
  4. X^Y Calculator Using a For Loop
    By b0rn2fl2 in forum C Programming
    Replies: 23
    Last Post: 10-18-2011, 11:42 AM
  5. while loop (sinple calculator)
    By aweida in forum C Programming
    Replies: 3
    Last Post: 09-27-2010, 12:40 PM

Tags for this Thread