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;
}