Простой вопрос консольного калькулятора.
Я довольно новичок в c++, я начал с него примерно два дня назад. Я закодировал полностью функциональный калькулятор dos. Теперь я признаю, что это может быть не самый лучший способ сделать калькулятор, пожалуйста, будьте добры. Вот код. Мой вопрос касается последнего комментария внизу. (Visual Studio 2017)
#include "stdafx.h" #include <iostream> //Calculator's adding function. int addNumbers(int x, int y) { int answer = x + y; return answer; } //Calc's subtracting function. int subtractNumbers(int x, int y) { int answer = x - y; return answer; } //Calc's multiplication function. int multiplyNumbers(int x, int y) { int answer = x * y; return answer; } //Calc's division function. int divideNumbers(int x, int y) { int answer = x / y; return answer; } 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
Что я уже пробовал:
if (operation != 1, 2, 3, 4) std::cout << "Eroror"; and else (operation =! 1, 2, 3, 4) std::cout << "error..." << std::endl;