# Newbie C++ Question

• 03-30-2007
Driv
Newbie C++ Question
This is the first time I've ever touched C++ (More then basic Hello World anyways). For my first program I decided to make a very basic calculator that can add, divide, multiply, and subtract. I got it to run fine, but I'm looking for a way to restart the program from the "Input first number" line. I'm working with a C++ book six years old, and web tutorials, and both said the goto command should be avoided. But so far that's all I've found. Thanks in advance. (Also if you could point out any glaring beginner mistakes that I may have made, I would appreciate it. Every little bit of advice helps.)

Code:

```#include <iostream> using namespace std; //Functions for operation int addition (int a, int b) {   int r;   r=a+b;   return (r); } int subtract (int a, int b) {   int r;   r=a-b;   return (r); } int multiply (int a, int b) {   int r;   r=a*b;   return (r); } int divide (int a, int b) {   int r;   r=a/b;   return (r); } int main(int argc, char *argv[]) {     int a, b, sum, op; //Declares user input number, one, two, sum, and the operator.         restart: //Used to restart program cout << "Enter first number: "; cin >> a; cout << "Enter your operator:(1 adds, 2 subtracts, 3 multiplies, 4 divides)\n"; cin >> op; cout << "Enter second number:"; cin >> b; if (op == 1) {   sum = addition(a,b);    cout << "The Sum Is: " << sum << "\n";   goto restart;   } else if (op == 2) {   sum = subtract(a,b);   cout << "The Sum Is: " << sum << "\n";   goto restart;   } else if (op == 3) {   sum = multiply(a,b);   cout << "The Sum Is: " << sum << "\n";   goto restart;   } else if (op == 4) {   sum = divide(a,b);   cout << "The Sum Is: " << sum << "\n";   goto restart;   }   else {   cout << "Invalid Operator"; }       system("PAUSE");     return EXIT_SUCCESS; }```
• 03-30-2007
DarkDot
put the while thing in a big loop that brings it back to that point
• 03-30-2007
Sentral
Use a do...while loop with a Boolean variable. Check the tutorials on this site.
• 03-30-2007
Driv
Ah thanks, wouldn't have thought of a loop for that purpose. It works fine using a do...while boolean loop.
• 03-30-2007
Raigne
just a small recommendation. why dont you use a char as your operator instead of a number, so that you have + = addition - = subtraction * = multiplication / = division
be a little more like a calculator.
• 03-30-2007
Driv
I actually tried that, and I got a compiler error. I assumed you couldn't have something like

Code:

`x == +`
• 03-30-2007
you would want to declare a char variable,
and use that for your operator.
then do something like this

Code:

``` char operator; if(operator == '+') {           ... } else if(operator == '-') {         ... }```
something like that
• 03-31-2007
VillageIdiot
little Tip:

NEVER use GOTO!!! NEVER EVER!!!
• 03-31-2007
Noir
big tip:

Never listen to VillageIdiot. Never ever.

;)

Seriously, banning goto without thinking is stupid. It's like never using a hammer because you accidentally hit your thumb once.
• 03-31-2007
Daved
Hammers are often the right tool for the job, gotos are rarely the right tool for the job, especially in C++. I won't say "never" use them, but I would say that if you find yourself using them then it might be a good time to learn the better alternatives (like a while loop in this case).
• 03-31-2007
Noir
See? That's a good way to look at it, that's how I look at it too. And it's pretty different in meaning than "NEVER use GOTO!!! NEVER EVER!!!", right?
• 03-31-2007
Daved
>> And it's pretty different in meaning than "NEVER use GOTO!!! NEVER EVER!!!", right?
Yup. That was part of my point.