Search code examples
c++menucalculator

how do i make a console menu (c++) that i can return to after selecting something?


i made a simple menu and i know how to make choices and stuff but once i select a choice i don't know how to make it so that it returns to the selection menu.

#include <cmath>
#include <iostream>
#include <string>

int main()
{
    std::cout << "+====| LEO'S CALCULATOR |=========+\n\n";

    std::cout << "1 - Addition\n";

    std::cout << "2 - Subtraction\n";

    std::cout << "3 - Division\n";

    std::cout << "4 - circle area\n\n";

    std::cout << "+=================================+\n";

    int choice;

    std::cin >> choice;

    // CIRCLE RADIUS

    if (choice == 4)
    {

        std::cout << "what is the radius of your circle\n";
        double radius;
        double circleArea;
        const double pi = 3.14;
        std::cin >> radius;
        circleArea = pi * pow(radius, 2);
        std::cout << "the radius of your circle is: " << circleArea;
    }
}

I tried to look up what I was trying to do and i saw something regarding functions and being able to call them but i couldn't understand it. I tried to make the menu a function and in the choice i attempted to call said function but obviously it didn't work

im a beginner so if you have any other suggestions on what i should change id appreciate it and suggested resources for c++ are welcome


Solution

  • With a loop (do while for example) and an option to exit the loop.

    #include <iostream>
    
    int main()
    {
        int choice;
        do
        {
            std::cout << "+====| LEO'S CALCULATOR |=========+\n\n";
            std::cout << "1 - Your menu...\n";
            std::cout << "5 - Quit\n\n";
            std::cout << "+=================================+\n";
    
            std::cin >> choice;
    
            // Your code.
        }
        while(choice != 5);
    }