简体   繁体   中英

How to make a loop start from beginning in C++?

I am writing a program in C++ which takes numbers input and it adds substracts divides, and multiplies. In the code when I want the switch case statement to run infinitely until the user presses 5 to exit the program. Please help me. Whenever I am writing the while(true) it is infinitely showing the output.

#include <iostream>
using namespace std;

 class SimpleCalculator
{
protected:
private:
    int num1, num2;

public:
    int add(int a, int b)
    {
        num1 = a;
        num2 = b;
        return a + b;
    }

    int diff(int a, int b)
    {
        num1 = a;
        num2 = b;
        return a - b;
    }

    int quo(int a, int b)
    {
        num1 = a;
        num2 = b;
        return a / b;
    }

    int pro(int a, int b)
    {
        num1 = a;
        num2 = b;
        return a * b;
    }
};

int main()
{
    int n1, n2;
    cout << "Enter the value of the number 1: ";
    cin >> n1;
    cout << "Enter the value of the number 2: ";
    cin >> n2;
    SimpleCalculator o1;

    int input;
    cout << "Enter your number: ";
    cin >> input;

    while (true)
    {
        switch (input)
        {
        case 1:
            cout << o1.add(n1, n2) << endl;
            break;
        case 2:

            cout << o1.diff(n1, n2) << endl;
            break;

        case 3:

            cout << o1.pro(n1, n2) << endl;
            break;

        case 4:

             cout << o1.quo(n1, n2) << endl;
             break;

        case 5:
            break;
        default:
            cout << "Invalid entry" << endl;
            break;
        }
    }
    return 0;
}

You have two main issues:

  1. The user input requests must be part of the loop since you want them to be repeated at every iteration (ie the user needs to make a choice at every iteration).
  2. You forgot to exit the loop in the case 5: statement.
  3. [Optional] Your application is lacking a user menu (the user can't guess what he has to do)

Note: I would advise you to split your code into several functions so that it would be clearer to read and easier to organize.

Note 2: You don't need to use a class for such basic operations, the members are not really needed, you can directly return the result of the operation. My advice would be to define the functions as free functions inside a namespace or as static members of your class if you really want to.


Example:

First let's define the calculation functions:

int add(int a, int b) {return a+b;}
int sub(int a, int b) {return a-b;}
int pro(int a, int b) {return a*b;}
int quo(int a, int b) {return a/b;}

Then we define a function to ask for user inputs:

void ask_user_input(int & lhs, int & rhs)
{
    std::cout << "Enter the first operand: ";
    std::cin >> lhs;
    std::cout << "Enter the second operand: ";
    std::cin >> rhs;
}

Finally we define a function to display the user menu:

void display_menu()
{
    std::cout << "\n--- Calculator ---\n";
    std::cout << "1. Addition\n2. Substraction\n3. Multiplication\n4. Division\n5. Exit\n";
}

Bringing all things together in the main() function, it gives:

int main()
{
    bool quit(false);
    int choice, lhs, rhs;

    while(!quit)
    {
        display_menu();
        std::cin >> choice;

        switch(choice)
        {
            case 1: ask_user_input(lhs, rhs);
                    std::cout << '\n' << lhs << " + " << rhs << " = " << add(lhs, rhs) << '\n';
                    break;
            case 2: ask_user_input(lhs, rhs);
                    std::cout << '\n' << lhs << " - " << rhs << " = " << sub(lhs, rhs) << '\n';
                    break;
            case 3: ask_user_input(lhs, rhs);
                    std::cout << '\n' << lhs << " * " << rhs << " = " << pro(lhs, rhs) << '\n';
                    break;
            case 4: ask_user_input(lhs, rhs);
                    std::cout << '\n' << lhs << " / " << rhs << " = " << quo(lhs, rhs) << '\n';
                    break;
            case 5: quit = true;
                    break;

            default: std::cout << "Invalid choice, try again !\n";
                     break;
        }
    }

    return 0;
}

Live example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM