I've made a calculator program which calculates the desired calculation(including accumulation(+), subtraction(-), multiplication(x) and division(/). But I have problems because when you enter a character or combine a number with a character(Ex. 778ds) it can bring malfunction and errors to the program and unfortunately we must close it... I am only a beginner so please give an easy explanation, what should i implement/change to the code.
#include<iostream>
usingnamespace std;
int main()
{
double n1;
double n2;
char redo;
char operation;
do
{
system("CLS"); // If a person enters y it returns the program to this line.
system("TITLE Calculator"); // Title of Program
system("Color B4"); // Writing Color
cout << "Please enter your desired calculation: " << endl;
cin >> n1 >> operation >> n2;
switch(operation)
{
case'+':
cout << "Your result is: " << n1 + n2 << endl;
break;
case'-':
cout << "Your result is: " << n1 - n2 << endl;
break;
case'*':
cout << "Your result is: " << n1 * n2 << endl;
break;
case'x':
cout << "Your result is: " << n1 * n2 << endl;
break;
case'X':
cout << "Your result is: " << n1 * n2 << endl;
break;
case'/':
if(n2 == 0){
cout << "That is an invalid operation!" << endl;
}else{
cout << "Your result is: " << n1 / n2 << endl;
}
break;
default:
cout << "That is an invalid operation!" << endl;
break;
}
cout << "Do you want to calculate something else?(Y/N)" << endl;
cin >> redo;
}while(redo == 'y' || redo == 'Y'); // || means or. If a person enters y or Y the program starts again and returns to the system CLS line
system("PAUSE");
return 0;
}