0

If the user inputs an option that is not between 1-5 and is a number the program works as intended and asks them to input a correct option. However if the user inputs anything other than a number the program malfunctions and gets stuck repeating the same message and then does not allow the user to input anything

(https://gyazo.com/d34e78e361344a5a3c9330e28b287960)

I am new to coding but I am assuming it is something to do with my default case not working how I think it should. Any help resolving this is much appreciated.

// Create a menu with 4 options (Add, Subtract, Multiply, Divide) 
// Check for divide by 0 errors
// Also add option which confirms users choice to exit the program

#include <iostream>

using namespace std;

int main()
{
    float fOne, fTwo, fSum; // Two numbers that the user will input and the sum of them after selecting an option
    int iOption; // Option that user will select on the menu
    char cEnd; // Used for user to continue / end the program 

    cEnd = ('n');

    do

    {
        cout << "Please choose an option." << "\n" << endl; // Asking user to select one of the 5 options
        cout << "1: Addition." << endl;
        cout << "2: Subtraction." << endl;
        cout << "3: Multiplication." << endl;
        cout << "4: Division." << endl;
        cout << "5: Exit Program." << '\n' << endl;

        cin >> iOption; // Getting user to input their option

        switch (iOption)

        {

        case 1: cout << "Enter two numbers: " << endl; // Prompting user to enter two numbers
            cin >> fOne;
            cin >> fTwo;
            fSum = fOne + fTwo;
            cout << "The sum of your two numbers added together is: " << fSum << "\n" << endl; // Result will print to the screen depending on which option was selected
            break;


        case 2: cout << "Enter two numbers: " << endl;
            cin >> fOne;
            cin >> fTwo;
            fSum = fOne - fTwo;
            cout << "The sum of your two numbers subtracted from each other is: " << fSum << '\n' << endl;
            break;


        case 3: cout << "Enter two numbers: " << endl;
            cin >> fOne;
            cin >> fTwo;
            fSum = fOne * fTwo;
            cout << "The sum of your two numbers multiplied together is: " << fSum << '\n' << endl;
            break;


        case 4: cout << "Enter two numbers: " << endl;
            cin >> fOne;
            cin >> fTwo;
            if (fTwo == 0) // If input two == 0 program will return an error message and return to menu
            {
                cout << "Error, cannot divide by zero. " << '\n' << endl;
            }
            else if (fTwo != 0) // If input two is != 0 program will calculate the sum
            {
                fSum = fOne / fTwo;
                cout << "The sum of your two numbers divided by each other is: " << fSum << '\n' << endl;
            }
            break;


        case 5: cout << "Are you sure that you want to exit the program?" << endl; cout << "Select an option (y/n)" << '\n' << endl; // Asking the user if they wish to exit the program and prompting them to select an option
            cin >> cEnd; // User should input either y or n
            if (cEnd == 'y' || cEnd == 'Y') // Inputting 'y' || 'Y' will end the program and tell the user that it is exiting
            {
                cout << "The program will now exit. " << '\n' << endl;
                system("pause");
                return 0;
            }
            break;

        default:
            cout << "Please select an option between 1-5." << '\n' << endl;
            break;

        }
    }

    while (cEnd == 'n' || 'N'); // Program will continue whilst end == 'n' or 'N', program will close if end == 'y' or 'Y'

}
Sam
  • 13
  • 1
  • 3
  • When `std::cin` fails you need to `.clear()` and `.ignore()` otherwise yes, it will stuck. – DimChtz Oct 31 '17 at 20:36
  • Thanks for the help and Fred that is exactly what i'm after so i'll try to fix it when I get time. – Sam Oct 31 '17 at 20:45

0 Answers0