1. ## 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 << "(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:
break;
}
cout << "The result is " << result << "\n";
return 0;
}```

2. 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.

3. 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

4. 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.

5. 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?

6. I haven't run into the switch statement yet in my studies, what is the basic syntax of that?

7. Alright , so does that look better.

8. 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.

9. Ok, I think I finally got it, thanks a bunch.

10. are you sure your first program wasnt
Code:
```#include <iostream>
using namespace std;
int main()
{
cout << "Hello World.";
return 0;
}```

11. 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

12. 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 << "(2) Subtract \n";
cout << "(3) Multiply \n";
cout << "(4) Divide \n";
cin >> operator_choice;
} while ((operator_choice > 4) || (operator_choice < 1));```

13. 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.

14. 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. 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.