2. Post compileable code. That includes headers. Anyone trying to compile your code has to add headers.
3. What happens if the user enters "Exit"? or "EXIT"? You are testing for an exact match with "exit". You need to convert the input to lower case before testing if the user entered exit.
#include <iostream>
#include <string>
#include <algorithm> // std::transform
#include <cctype> // std::::lower
int main()
{
std::string input { };
do
{
std::cout << "Please enter an expression (enter exit to quit): ";
std::cin >>input;
// make the input all lower case
std::transform(input.begin(), input.end(), input.begin(), std::tolower);
if (input.compare("exit") == 0)
{
std::cout << "\nThank you for using my calculator! Welcome to come back!\n";
break;
}
int operand1 { };
int operand2 { };
char charOperator { };
for (size_t i = 0; i < input.length(); i++)
{
if (input[i] == '+' || input[i] == '-' || input[i] == '*' || input[i] == '/' || input[i] == '%')
{
operand1 = stoi(input.substr(0, i));
charOperator = input[i];
operand2 = stoi(input.substr(i + 1));
}
}
switch (charOperator)
{
case'+':
{
std::cout << "The result of the expression is: " << (operand1 + operand2) << '\n';
break;
}
case'-':
{
std::cout << "The result of the expression is: " << (operand1 - operand2) << '\n';
break;
}
case'*':
{
std::cout << "The result of the expression is: " << (operand1 * operand2) << '\n';
break;
}
case'/':
{
if (operand2 == 0)
std::cout << "ERROR: Division by zero!\n";
else
std::cout << "The result of the expression is: " << (operand1 / operand2) << '\n';
break;
}
case'%':
{
if (operand2 == 0)
std::cout << "ERROR: Division by zero!\n";
else
std::cout << "The result of the expression is: " << (operand1 % operand2) << '\n';
break;
}
default:
std::cout << "ERROR: Invalid operator!\n\n";
}
std::cout << '\n';
}
while (input.compare("exit") != 0);
}
Please enter an expression (enter exit to quit): 12/5
The result of the expression is: 2
Please enter an expression (enter exit to quit): 12%5
The result of the expression is: 2
Please enter an expression (enter exit to quit): Exit
Thank you for using my calculator! Welcome to come back!