Liz
Liz

Reputation: 21

C++ switch statement for arithmetic operations

When I run my program, it doesn't allow me to pick an operation. It just goes straight to "Invalid option" and asks again. I want to be able to choose '+', '-', '*', '/', '^', and '!' as my options. What did I do wrong?

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>

using namespace std;

int main()
{

char operation;
int num1, num2, result, remain;

//Display the menu to the user and get their first choice

    cout << "What operation would you like to perform:" << endl 
    << " + addition\n - subtraction\n * multiplication\n / division\n ^ number to power\n ! factorial" 
    << "\n q quit" << endl << endl << "Operation? ";
    cin >> operation;

//Switch - the user does not want to quit

switch (operation)
{

case 1: result=num1+num2;
    cout << "Enter the first number to add: " << endl;
    cin >> num1;
    cout << "Enter the second number to add: " << endl;
    cin >> num2;
    cout << endl << num1 << " + " << num2 << " = " << result;
    break;

case 2: result=num1-num2;
    cout << "Enter the first number to subtract: " << endl;
    cin >> num1;
    cout << "Enter the second number to subtract: " << endl;
    cin >> num2;
    cout << endl << num1 << " - " << num2 << " = " << result;
    break;

case 3: result=num1*num2;
    cout << "Enter the first number to multiply: " << endl;
    cin >> num1;
    cout << "Enter the second number to multiply: " << endl;
    cin >> num2;
    cout << endl << num1 << " * " << num2 << " = " << result;
    break;

case 4: result=num1/num2;
    cout << "Enter the dividend: " << endl;
    cin >> num1;
    cout << "Enter the divisor: " << endl;
    cin >> num2;
    cout << endl << num1 << " / " << num2 << " = " << result;
    cout << endl << num1 << " % " << num2 << " = " << result;
    break;

case 5: result=num1^num2;
    cout << "Enter the base number " << endl;
    cin >> num1;
    cout << "Enter the power: " << endl;
    cin >> num2;
    cout << endl << num1 << " ^ " << num2 << " = " << result;
    break;

case 6: result=num1!=num2;
    cout << "Enter a number: " << endl;
    cin >> num1;
    cout << endl << num1 << " ! " << " = " << result;
    break;

default: 
    cout << "That is an invalid operation!" << endl;    
    break;

} // switch statement closed

cout << endl << "What operation would you like to perform:" << endl << " + addition\n - subtraction\n * multiplication\n / division\n ^ number to power\n ! factorial" << "\n q quit" << endl << endl << "Operation? ";
cin >> operation; 

return 0;
} //main statement closed

Upvotes: 0

Views: 3278

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726849

You are using a char against a switch of ints that do not match character codes that you wish to select. If you would like to decide among characters, use character literals instead of integer numbers:

case '+':
    // Read inputs first
    cout << "Enter the first number to add: " << endl;
    cin >> num1;
    cout << "Enter the second number to add: " << endl;
    cin >> num2;
    // Compute the result next
    result=num1+num2;
    cout << endl << num1 << " + " << num2 << " = " << result;
    break;

Note that the assignment

result=num1+num2;

should come after the code that reads the inputs, i.e. num1 and num2.

at the end when I ask if it wants to do another operation, it just ends even if I pick an operation.

This is because an end-of-line character that you entered after num2 is sitting in the buffer. You need to ignore it. Add this line:

cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

before

cin >> operation;

Upvotes: 3

Related Questions