Thread: Basic Calculator code help

  1. #1
    Registered User
    Join Date
    Jan 2012
    Location
    UK
    Posts
    2

    Post Basic Calculator code help

    Hey everyone! Just joined today and am loving cprogramming.com ! Learned everything so far for C at this website (and some C++ but choosing to learn C first for various reasons )

    I am programming on a Windows machine and trying to make a DOS calculator while I learn new techniques (I haven't programmed in a long time and am coming back, again).

    Here is the source code to a recent project I a have been working on. I got a few problems though. I am having trouble with my exit function. You'll see what I mean once you run it. I don't know if that if loop is wrong or what, but it ain't working properly.
    Also, could I use anything else besides a goto loop because I heard those 'never ending' loops are killer. If anything you spot seems a little weary, help me! I'm learning!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    /////////////////////////////////////////
    // Functions for mathematical equations //
    /////////////////////////////////////////
    int addition(int x, int y);
    int subtraction(int x, int y);
    int multiplication(int x, int y);
    int division(int x, int y);
    
    int main(void)
    {
      
      int choice;
      int num1;
      int num2;
      int answer;
      int exit;
      
    /////////////////////////////////////////
    // Ints for function prototypes //////////
    /////////////////////////////////////////
      int x; 
      int y;
      
      int yes = 0;
      int no = 1;
      
      Start:
            
      system("CLS"); //clear screen cache
      system("COLOR 4F"); //red bkgd, white text
      
      printf("                       Welcome to Drew's Calculator 1.0\n"); /*Title, this will be centered as best as possible*/
      printf("\n\nSelect which type of operation you want to execute:\n");
      printf("1 - Addition\n");
      printf("2 - Subtraction\n");
      printf("3 - Multiplication\n");
      printf("4 - Division\n");
      printf("5 - Exit\n\n");
      
      scanf("%d\n", &choice , "\n");
      
      switch(choice){
      
      case 1: //addition\\
      
           printf("\n");
           scanf("%d%d", &num1, &num2);
    
           printf("\n\n%d", addition(num1, num2));
           getch();
           goto Start;
           break;
    
      case 2: //subtraction\\
       
           printf("\n");
           scanf("%d%d", &num1, &num2);
           
           printf("\n\n%d", subtraction(num1, num2));
           getch();
           goto Start;
           break;
      case 3: //multiplication\\
    
           printf("\n");
           scanf("%d%d", &num1, &num2);
           
           printf("\n\n%d", multiplication(num1, num2));
           getch();
           goto Start;
           break;
      case 4: //division\\
      
           printf("\n");
           scanf("%d%d", &num1, &num2);
           
           printf("\n\n%d", division(num1, num2));
           getch();
           goto Start;
           break;
      case 5: //exit\\
      
      printf("\n\nAre you sure you wish to exit? \n");
             printf("\n0 - yes \n1 - no");
             
             scanf("%d", &exit);
             
       if(exit == yes)
       {
               system("EXIT");
             }
       else if (exit == no)
       {
               goto Start;
            }
       else
       {
               goto Start;
            }
             break;
      default:
              
              goto Start;
              break;
              }
    
      getch();
      return 0;
    }
    
    /////////////////////////////////////////
    // Prototypes for functions //////////////
    /////////////////////////////////////////
    int addition(int x, int y)
    {
        return x + y;
    }
    
               int subtraction(int x, int y)
               {
                   return x - y;
                   } 
    
    int multiplication(int x, int y)
    {
        return x * y;
    }
    
               int division(int x, int y)
               {
                   return x / y;
                   }
    I'm gonna constantly keep changing and making this code better (using arrays in the future or something). This is just what I have learned so far.
    Last edited by habbalock; 01-16-2012 at 08:02 AM.

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    There's a lot that could be condensed, for example:

    Code:
    int exit;
    
     scanf("%d", &exit); 
    
    
      if(exit == yes)
       {
               system("EXIT");
             }
       else if (exit == no)
       {
               goto Start;
            }
       else
       {
               goto Start;
            }
             break;
    could be:

    Code:
        if ((getchar()) == '1') 
            exit(0);
        else 
            goto Start;


    No point having 2 extra ints just to hold a 0 and a 1, nor having the extra conditional that does the same thing. Using scanf and 4 byte types is incredibly wasteful if you're only looking for a single character input.

    Finally, you were evaluating if it was equal to 1, not '1'. The difference being that '1' is an ASCII printable character (I think its 0x31?), and 1 is hardly ever going to be found in a user-inputted string. This error actually persists all the way throughout your switch statement. Before you fix it, however, change all of the inputs that deal with characters to the type char, for clarity (and that it takes up 4 times less memory).
    Last edited by memcpy; 01-16-2012 at 08:24 AM.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Location
    UK
    Posts
    2
    Thanks man! But it still seems to not work correctly. The exit case, I mean. It doesn't display anything on the screen once the user enters '5' to exit the program. It must display the prinf's I have so they know what to choose. And if they hit 1 or 0, it still loops to the beginning..

    Code:
    case 5: //exit\\
      
      printf("\n\nAre you sure you wish to exit? \n");
             printf("\n1 - no \n0 - yes");
             
             scanf("%d", &exit);
             
           if ((getchar()) == '1') 
           {
            system("EXIT");
            }  
        else {
            goto Start;
            }
            
      default:
              
              goto Start;
              break;
    }
      getch();
      return 0;
    }
    EDIT: Once user hits a key and enters, it shows the printf message! Now, it still loops to start no matter what. P.S , getch() vs getchar() made a difference, why?

    Code:
    case 5: //exit\\
      
      printf("\n\nAre you sure you wish to exit? \n");
             printf("\n1 - no \n0 - yes");
             
             scanf("%d", &exit);
             
           if(getch() == '1') 
           {
            system("EXIT");
            }  
        else {
            goto Start;
            }
    Last edited by habbalock; 01-16-2012 at 08:30 AM.

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    > getch() vs getchar() made a difference, why?
    They're almost the same, but with a small difference. Getchar() echoes back your choice to the console, and is available on virtually all of the computer platforms you'll find. Getch() doesn't require the enter key, doesn't echo to the console, and only works on older Windows computers.

    The reason why it keeps looping is probably in your code to end the program (take the scanf back out, you still don't need it). The system(EXIT) directive is not standard, it's best to just use a simple exit(0).

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You're going to get a lot further (in the long run), when you stop using system() for doing all your terminal/process control, and stop using goto to synthesise a while loop.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic calculator
    By Khaled in forum C++ Programming
    Replies: 2
    Last Post: 05-22-2011, 08:17 AM
  2. Basic Sum Calculator
    By ScoutDavid in forum C Programming
    Replies: 14
    Last Post: 09-26-2010, 02:01 AM
  3. A Basic Calculator.
    By bijan311 in forum C++ Programming
    Replies: 4
    Last Post: 12-26-2009, 01:53 PM
  4. Basic Calculator
    By Surge in forum C Programming
    Replies: 18
    Last Post: 12-02-2006, 10:20 PM
  5. need help with my basic c++ calculator
    By iCouch_Potato in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2004, 06:23 PM