15,938,122 members
See more:
I'm pretty new to c++, I started with it like two days ago. I coded a fully functional dos calculator. I will admit now that this may not be the best way to make a calculator, pls be nice. Here's the code. My question is on the final comment at the bottom. (Visual Studio 2017)

C++
```#include "stdafx.h"
#include <iostream>

{
int answer = x + y;
}

//Calc's subtracting function.
int subtractNumbers(int x, int y)
{
int answer = x - y;
}

//Calc's multiplication function.
int multiplyNumbers(int x, int y)
{
int answer = x * y;
}

//Calc's division function.
int divideNumbers(int x, int y)
{
int answer = x / y;
}

int main()
{
//Requests first number
std::cout << "Please enter the first number:" << std::endl;
int number1;
std::cin >> number1;

//Second number
std::cout << "Please enter the second number: " << std::endl;
int number2;
std::cin >> number2;

//Requests an arithmatec operation, and defines it as "operation" so we can call the calculator's functions to it.
std::cout << "Now, please enter an operator." << std::endl;
std::cout << " 1 is (+) " << std::endl;
std::cout << " 2 is (-) " << std::endl;
std::cout << " 3 is (*) " << std::endl;
std::cout << " 4 is (/) " << std::endl;
int operation;
std::cin >> operation;

//Calls the calculator's functions to the inputted "operation" variable. This calculates the respective equation.
if (operation == 1)
std::cout << "The sum of the two numbers is: " << addNumbers(number1, number2) << std::endl;
if (operation == 2)
std::cout << "The difference of the two numbers is: " << subtractNumbers(number1, number2) << std::endl;
if (operation == 3)
std::cout << "The product of the two numbers is: " << multiplyNumbers(number1, number2) << std::endl;
if (operation == 4)
std::cout << "The quotient of the two numbers is: " << divideNumbers(number1, number2) << std::endl;

//Error message if input is impossible.
if (operation > 4)
std::cout << "Operator error: Please enter a number between 1 and 4." << std::endl;
if (operation < 1)
std::cout << "Operator error : Please enter a number between 1 and 4." << std::endl;
return 0;
}	// (statement that will show error message if anything but 1, 2, 3, 4 is inputted.)

//The last comment there is what I'm trying to accomplish.
//ALSO: How to make it continuous, so when you calculate one thing, the console doesn't close```

What I have tried:

C++
```if (operation != 1, 2, 3, 4)
std::cout << "Eroror";

and

else (operation =! 1, 2, 3, 4)
std::cout << "error..." << std::endl;```
Posted
Updated 3-Aug-17 15:36pm
v5

## Solution 1

The first issue may be resolved using if else : If Statements in C++ - Cprogramming.com[^]

The second issue requires a loop: C++ Loop Types[^]

eg you could use an infinite loop with an exit condition and then break on a certain number: break statement - cppreference.com[^]

C++
```#include <iostream>
using namespace std;

int main () {

for( ; ; ) {
// Input number code

if(operation == 1)
.....;
else if(operation==...)
......;
else if(operation==99)
break;
else
cout << "Operator error: Please enter a number between 1 and 4." << endl;
}

return 0;
}```

Note the use of the std namespace: Name visibility - C++ Tutorials[^]

v3