Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading