switch

switch
mail@pastecode.io avatar
unknown
c_cpp
2 years ago
1.5 kB
3
Indexable
Never
#include <iostream>

using namespace std;

double calculateThis(char operate){
    double n1, n2, result;

    cout << "Input number: "; cin >> n1; cout << "Input another number: "; cin >> n2;

    switch(operate){
        case '+' : result = n1 + n2; break;
        case '-' : result = n1 - n2; break;
        case '/' : result = n1 / n2; break;
        case '*' : result = n1 * n2; break;
        default : result = 0; break;
    }
    return result;
}

int main(){

    char operate, prompt;
    double res;

	do {
	system("CLS");
    cout << "This is the list of operators: +, -, /, *\n" << "Choose an operator: "; cin >> operate;

    if(operate != '+' && operate != '-' && operate != '/' && operate != '*'){
        cout << "\nInvalid input!";

        } else {

            res = calculateThis(operate);
				
                if(operate == '+'){
                    cout << "\nThe sum is: ";
                }
                if(operate == '-'){
                    cout << "\nThe difference is: ";
                }
                if(operate == '/'){
                    cout << "\nThe quotient is: ";
                }
                if(operate == '*'){
                    cout << "\nThe product is: ";
                }

    cout << res;

    cout << "\n\nThanks for using my program! Do you want to use it again?(Y/N)\nAnswer: "; cin >> prompt; 
    
		}
	}
    while(prompt == 'Y' || prompt == 'y');
}