+ 4
Please Modify My Code
How to Make Calculator Taking input in 1 line using C++ https://sololearn.com/compiler-playground/cpLVlpycztIs/?ref=app
8 ответов
+ 3
Try this:
#include <iostream>
#include <sstream>
using namespace std;
void Calculator(float num1, char op, float num2)
{
float result;
switch (op)
{
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
default:
cout << "Please Enter Correct Operation";
return;
}
cout << result;
}
int main()
{
string input;
getline(cin, input);
istringstream iss(input);
float num1, num2;
char op;
if (iss >> num1 >> op >> num2) {
Calculator(num1, op, num2);
} else {
cerr << "Invalid input format." << endl;
}
}