Thread: Need help with this loop

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    8

    Need help with this loop

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        
        
        while (true)
        {
            char op;
            float num1, num2;  
            char ex; 
    
            cout << "Select + for addition" << endl; // Give the user the option for addition 
            cout << "Select - for subtraction" << endl; // Give the user the option for subtraction 
            cout << "Select * for multiplication" << endl; // Give the user the option for multiplication 
            cout << "Select / for division" << endl; // Give the user the option for division 
            cout << "Select x to exit" << endl; // Give the user the option to exit the program 
            cin >> op; // User selects from the given operations
    
            if (op == 'x')
            {
                cout << "Are you sure you would like to exit?" << endl;
                cout << "Enter y to exit" << endl;
                cout << "Enter n to stay" << endl;
                cin >> ex;
            }
            
            cout << "Enter number one "; // User enters first number which they want to calculate 
            cin >> num1;
    
            cout << "Enter number two "; // User enters second number which they want to calculate 
            cin >> num2;
    
            if (op == '+')
            {
                cout << "Sum of operation = " << num1 + num2 << endl;
    
            }
            else if (op == '-')
            {
                cout << "Sum of operation = " << num1 - num2 << endl;
            }
            else if (op == '*')
            {
                cout << "Sum of operation = " << num1 * num2 << endl;
            }
            else if (op == '/')
            {
                cout << "Sum of operation = " << num1 / num2 << endl;
            }
            
        }
        system("pause");
        return 0;
        
    }
    when i ask theuser if they with to exit and they select "x" i am then asking another question but im not sure where to go from there. if they select "y" i want them to exit the program however if they select "n" i want them to go back to the menu were they select their operation again and im not sure how to do this??

  2. #2
    Guest
    Guest
    Without having put much thought into this, here is a quick solution:
    Code:
    if (op == 'x')
    {
        cout << "Are you sure you would like to exit?" << endl;
        cout << "Enter y to exit" << endl;
        cout << "Enter n to stay" << endl;
        cin >> ex;
        if (ex == 'y') {
            break; // leaves the parent while-loop
        } else {
            continue; // skips remaining loop into next iteration
        }
    }

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    8
    thank you i didn't realise i could have an if inside another if. would you know how i could include a thing were if the user tries to divide by zero a error would come up saying "cannot divide by zero" and would send the user back to the main menu?

  4. #4
    Guest
    Guest
    You could try that yourself as an exercise! Place an extra check inside the else-if section for division. If the user tries to divide by 0, you could then print a message and go to the next iteration of the loop as in my earlier example.

  5. #5
    Registered User
    Join Date
    Nov 2019
    Posts
    8
    Code:
    else if (op == '/') 
            {
                if (num2 == '0') 
                {
                    cout << "Cannot divide by zero" << endl; 
                }
                else 
                {
                    cout << "Sum of operation = " << num1 / num2 << endl;  
                }
            }
            else // If op choice is not valid
            {
                cout << "Not a valid choice" << endl; // Tell user thier choice is not valid
                continue; // Continues with the while loop
            }
    this is what i managed to do however when i try to divide by 0 the message doesnt show up and im not sure why

  6. #6
    Guest
    Guest
    Code:
    if (num2 == 0) { // not '0'
    However, comparing a float to 0 isn't quite as simple as that either, so I would recommend you declare num2 as an integer (num1 can stay float). It's complicated

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 09-22-2016, 09:08 AM
  2. Replies: 4
    Last Post: 11-05-2015, 03:29 PM
  3. Replies: 1
    Last Post: 03-28-2015, 08:59 PM
  4. Help - Collect data from Switch loop inside While loop
    By James King in forum C Programming
    Replies: 15
    Last Post: 12-02-2012, 10:17 AM
  5. Replies: 23
    Last Post: 04-05-2011, 03:40 PM

Tags for this Thread