Thread: First Program (Calculator)

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    11

    First Program (Calculator)

    Hey, everyone, I'm a beginner to all types of programming, so I figured I'd place my first original (i.e. completely solved by me) up for comments and criticisms. I'm mainly looking on how to make it more streamlined, as I decided I might as well learn to make efficient code as I'm learning the language. Anyway, the following is a simple calculator program. Also, and I apologize in advance if this is a stupid question, but is there a command in c++ that is similar to the ClrHome command in Visual Basic. I haven't been able to find it in any of the books I'm using. Thanks.

    Code:
    /* ****************************************************************************
        This program takes two numbers as input, asks the user to choose a operator,
        then displays the result.
    ******************************************************************************/
    
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        //First we declare the variables we will use for calculations
        long double x, y;
        long double result;
        int z; //This is the value we will use for the operator
    
        //Next we get our values from the user and display them to verify
        cout << "Please enter a number: \n";
        cin >> x;
        cout << "Please enter a second number: \n";
        cin >> y;
        cout << "\n" << "Your numbers are " << x << " and " << y << "\n";
        cout << "\n";
    
        //Next we display the possible operators
        cout << "(1) Add \n";
        cout << "(2) Subtract \n";
        cout << "(3) Multiply \n";
        cout << "(4) Divide \n";
    
        cin >> z;
    
        cout << "\n";
        //Now we do the calculations
     switch ( z )
     {
      case 1:           
        result = x + y;
        break;
      case 2:            
        result = x - y;
        break;
      case 3:            
        result = x * y;
        break;
      case 4:          
        result = x / y;
        break;
      default:            
        cout<<"Error, bad input, quitting\n";
        break;
      }
      cout << "The result is " << result << "\n";
      return 0;
    }
    Last edited by bjl; 02-02-2008 at 12:02 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Basically, looks good. You should replace the 4 while statements with either if-else statements, or better yet, a switch statement with 4 cases. Also, the last 2 lines in each of the 4 cases are the same, so the only thing you need to do conditionally is assign a value to result.
    Last edited by robatino; 02-01-2008 at 11:34 PM.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by robatino View Post
    Edit: Actually, you NEED to replace the while statements, since as it is, the appropriate case would be executed over and over in an infinite loop.
    Thus the "return 0;" at the end of each

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by brewbuck View Post
    Thus the "return 0;" at the end of each
    I realized that before seeing your post, and removed my edit. But the whiles should still go, just because it's weird to use while instead of if when it's only going to execute at most once.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    11
    Quote Originally Posted by robatino View Post
    Basically, looks good. You should replace the 4 while statements with either if-else statements, or better yet, a switch statement with 4 cases. Also, the last 2 lines in each of the 4 cases are the same, so the only thing you need to do conditionally is assign a value to result.
    I haven't run into the switch statement yet in my studies, what is the basic syntax of that?

    Thanks for the comments guys.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I haven't run into the switch statement yet in my studies, what is the basic syntax of that?
    Read the tutorial on switch.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    11
    Alright , so does that look better.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Better, but not perfect. Since you output the result on every case, you can just put it after the switch instead. It's never a good thing to have identical code in several places in your program. It's way better to put them in a function or a common execution branch, or in other words, a common place where your code will run after the switch/if/loops or anything other like that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    11
    Ok, I think I finally got it, thanks a bunch.

  10. #10
    I eat babies
    Join Date
    Jan 2008
    Location
    la-la-land
    Posts
    31
    are you sure your first program wasnt
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
    	cout << "Hello World.";
    return 0;
    }

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    are you sure your first program wasnt
    Yes, without the comments and with indentation fixed, my first C++ program was taken from my lecture notes:
    Code:
    #include <iostream.h>
    
    void main()
    {
        cout << "Hello World!\n";
    }
    Thankfully, I have made some progress since then
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    2 remarks -
    1. use more meaningful variable names that explain what they contain. Eg, ("first_number, second_number, operator_choice" would be more understandable than "x, y, z"). For a small program like this it probably doesn't matter, but if you plan to write larger programs later on, might as well make it a habit now (you wouldn't want to be tracking down what xaayz mean in a 10000 lines program, right?)
    2. consider adding a do...while loop before the switch that prompts the user until he/she enters a valid input. Something like this:
    Code:
    int operator_choice = 0;
    do {
        cout << "(1) Add \n";
        cout << "(2) Subtract \n";
        cout << "(3) Multiply \n";
        cout << "(4) Divide \n";
        cin >> operator_choice;
    } while ((operator_choice > 4) || (operator_choice < 1));

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But even so, that loop will fail horribly to due the mechanics of cin.
    Either you need to a) read a string and validate or b) clear in the input buffer if something went wrong.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    But even so, that loop will fail horribly to due the mechanics of cin.
    Either you need to a) read a string and validate or b) clear in the input buffer if something went wrong.
    stand corrected. I was only thinking about invalid input as in a number greater than 4 or less than 1.

    To the OP: What Elysia meant is that if the input is not even a number, the loop I suggested would fail.

  15. #15
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Quote Originally Posted by cyberfish View Post
    2 remarks -
    1. use more meaningful variable names that explain what they contain. Eg, ("first_number, second_number, operator_choice" would be more understandable than "x, y, z"). For a small program like this it probably doesn't matter, but if you plan to write larger programs later on, might as well make it a habit now (you wouldn't want to be tracking down what xaayz mean in a 10000 lines program, right?)
    [/code]
    I have to disagree here. In this particular case, where one performs basic arithmetic on just two variables, x and y are perfectly understandable. I would almost go sofar as to say that they are the most logical choice, given that many math texts use x and y, or x1 and x2, for bivariate functions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help with Calculator program
    By Sshakey6791 in forum C++ Programming
    Replies: 3
    Last Post: 12-21-2008, 09:36 AM
  2. Need some help with Calculator program
    By Sshakey6791 in forum C++ Programming
    Replies: 0
    Last Post: 12-21-2008, 09:16 AM
  3. Help with calculator program.
    By banjordan in forum C++ Programming
    Replies: 1
    Last Post: 12-03-2003, 10:01 PM
  4. Newbie Needs Help on Calculator Program
    By CompWiz84 in forum C++ Programming
    Replies: 13
    Last Post: 06-26-2002, 02:21 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM