# First Program (Calculator)

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 02-01-2008
bjl
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; }```
• 02-01-2008
robatino
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.
• 02-01-2008
brewbuck
Quote:

Originally Posted by robatino
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 ;)
• 02-01-2008
robatino
Quote:

Originally Posted by brewbuck
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.
• 02-02-2008
bjl
Quote:

Originally Posted by robatino
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?

• 02-02-2008
laserlight
Quote:

I haven't run into the switch statement yet in my studies, what is the basic syntax of that?
• 02-02-2008
bjl
Alright , so does that look better.
• 02-02-2008
Elysia
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.
• 02-02-2008
bjl
Ok, I think I finally got it, thanks a bunch.
• 02-02-2008
mthemapc
are you sure your first program wasnt
Code:

```#include <iostream> using namespace std; int main() {         cout << "Hello World."; return 0; }```
• 02-02-2008
laserlight
Quote:

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 :p
• 02-02-2008
cyberfish
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));```
• 02-03-2008
Elysia
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.
• 02-03-2008
cyberfish
Quote:

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.
• 02-03-2008
MWAAAHAAA
Quote:

Originally Posted by cyberfish
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.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last