-2

I need to ask the user in this way "Do you want to do it again?" and the user will choose Y or N whether they want to compute another set of numbers and go through the program all over again.

#include <iostream>

using namespace std;

int main()
{
    float x, y;
    float Sum, Difference, Product, Quotient;
    char choice;

    cout << "Enter x and y: ";
    cin >> x >> y;
    cout << "MENU" << endl;
    cout << "A: Addition " << endl;
    cout << "B: Subtraction" << endl;
    cout << "C: Multiplication " << endl;
    cout << "D: Division " << endl;
    cout << "E: Exit " << endl;
    cout << "Enter your choice :";
    cin >> choice;
        switch (choice)
        {
        case 'A':
            Sum = x+y;
            cout << x << "+" << y << "=" << Sum <<endl;
            break;
        case 'B':
            Difference = x-y;
            cout << x << "-" << y << "=" << Difference <<endl;
            break;
        case 'C':
            Product = x*y;
            cout << x << "*" << y << "=" << Product <<endl;
            break;
        case 'D':
            Quotient = x/y;
            cout << x << "/" << y << "=" <<  Quotient <<endl;
            break;
        case 'E':
            break;
        default:
            cout << "Invalid input" << endl;
        }
    return 0;
}

Seju Baek
  • 7
  • 2
  • You need to add a loop. Also your code for Product and Quotient is wrong. `case 'E':` should probably return 0. – drescherjm Feb 11 '22 at 14:14
  • 4
    Quite honestly, you know you have to add a loop (you even mention the term in your title), but you haven't actually tried to do it yourself? – Some programmer dude Feb 11 '22 at 14:15
  • You have a couple of logical errors due to copy-paste actions. – 273K Feb 11 '22 at 14:16
  • At StackOverflow you will usually get better help and your question will be given a more favorable vote if you actually attempt to solve the problem you want solved. Your code shows no attempt to loop. – drescherjm Feb 11 '22 at 14:21
  • Your introductory C++ book should answer "How do I loop?" in an early chapter. Stack Overflow is not a tutorial site. – Drew Dormann Feb 11 '22 at 14:22
  • @drescherjm oh, yeah sorry I didn't notice it. – Seju Baek Feb 11 '22 at 14:22
  • Why would you expect that `'E' == 5`? – Lukas-T Feb 11 '22 at 14:59
  • 1
    A small code which may help you. https://onlinegdb.com/bvGb1ALfR Pay attention to the fact i've removed `using namespace std;` which is considered a bad practice, and i ve wrapped the loop process in a function which helps to make really clear your purpose. – IkarusDeveloper Feb 11 '22 at 15:10

1 Answers1

1

It's actually pretty simple. You can use loops as such:

    #include <iostream>

    using namespace std;

    void showChoices();
    int main()
    {
        while (true) // This is a while loop
        {
            system("cls"); // Clear the screen. If you don't want to clear the screen you can remove this line

            float x, y;
            float Sum, Difference, Product, Quotient;
            char choice;

            cout << "Enter x and y: ";
            cin >> x >> y;

            showChoices();
            cin >> choice;
            switch (choice)
            {
            case 'A':
                Sum = x + y;
                cout << x << "+" << y << "=" << Sum << endl;
                break;
            case 'B':
                Difference = x - y;
                cout << x << "-" << y << "=" << Difference << endl;
                break;
            case 'C':
                Product = x - y;
                cout << x << "*" << y << "=" << Product << endl;
                break;
            case 'D':
                Quotient = x - y;
                cout << x << "/" << y << "=" << Quotient << endl;
                break;
            case 'E':
                return 0; // Instead of break, use return here
            default:
                cout << "Invalid input" << endl;
            }
        }
    }

    void showChoices()
    {
        cout << "MENU" << endl;
        cout << "A: Addition " << endl;
        cout << "B: Subtraction" << endl;
        cout << "C: Multiplication " << endl;
        cout << "D: Division " << endl;
        cout << "E: Exit " << endl;
        cout << "Enter your choice :";
    }

Also consider removing the line:

using namespace std

...because it's not considered as a good practice.

Also consider refactoring your code by moving the code inside the loop to a separate function, which returns a boolean something like this:

    #include <iostream>

    using namespace std;

    void showChoices();
    bool askUser();
    int main()
    {
        while (askUser()) { /* Loop */ }
    }

    void showChoices()
    {
        cout << "MENU" << endl;
        cout << "A: Addition " << endl;
        cout << "B: Subtraction" << endl;
        cout << "C: Multiplication " << endl;
        cout << "D: Division " << endl;
        cout << "E: Exit " << endl;
        cout << "Enter your choice :";
    }

    bool askUser()
    {
        system("cls"); // Clear the screen. Can be removed if you don't want to do so.

        float x, y;
        float Sum, Difference, Product, Quotient;
        char choice;

        cout << "Enter x and y: ";
        cin >> x >> y;

        showChoices();
        cin >> choice;
        switch (choice)
        {
        case 'A':
            Sum = x + y;
            cout << x << "+" << y << "=" << Sum << endl;
            break;
        case 'B':
            Difference = x - y;
            cout << x << "-" << y << "=" << Difference << endl;
            break;
        case 'C':
            Product = x - y;
            cout << x << "*" << y << "=" << Product << endl;
            break;
        case 'D':
            Quotient = x - y;
            cout << x << "/" << y << "=" << Quotient << endl;
            break;
        case 'E':
            return false;
        default:
            cout << "Invalid input" << endl;
        }

        return true;
    }

I hope it was useful :).

The Coding Fox
  • 1,488
  • 1
  • 4
  • 18
  • You are calling the recursion only when the player choose to exit (and with no break), so the viceversa of the porpose. Your main has not return call, It's a little dangerous. – IkarusDeveloper Feb 11 '22 at 14:51
  • Oh, thanks for informing me that. I've edited the answer. – The Coding Fox Feb 11 '22 at 14:55
  • In C++ it's not allowed to call `main` I think. It's also a bad idea, because at one point it _will_ overflow your stack. – Lukas-T Feb 11 '22 at 14:56
  • It's allowed to call main in C++ I believe. I tried it just now and it works perfectly fine. But I agree with the stack overflowing. I've removed the 2'nd option. – The Coding Fox Feb 11 '22 at 14:59
  • You may also give a few good practice hints. The first is `using namespace std;` which is considered a really bad practice. The second is the fact he may preferably wrap the loop code in a function which returns a bool. https://onlinegdb.com/bvGb1ALfR – IkarusDeveloper Feb 11 '22 at 15:08
  • 2
    Anyway no it's not allowed to call main. https://stackoverflow.com/questions/2532912/call-main-itself-in-c/10311825 – IkarusDeveloper Feb 11 '22 at 15:11
  • @IkarusDeveloper Yep I'm totally aware of that. I never do that either. Just wanted to be straightforward :). And as for calling main, I meant that it compiles and works fine for me. Never knew it was against the C++ standards tho. Thanks for informing :D. – The Coding Fox Feb 11 '22 at 15:12
  • @SolvedGames I tried your code and modified it until it worked. Thanks for the help. – Seju Baek Feb 11 '22 at 15:52