Detect a character in my calculator program?

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<iostream>

using namespace 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;      
}


Topic archived. No new replies allowed.