PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, November 7, 2022

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

 November 07, 2022     c++, calculator, menu     No comments   

Issue

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);
}


Answered By - CGi03
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing