简体   繁体   中英

C++ Switch statement loop back to menu

I'm struggling to get my switch statement to go back to the start after an option is selected, to allow the user to input another option. I removed all the code from the switch cases just to cut it down a bit.

Thanks

int main() {
    char tab = '\t';

    int input;
    bool menu = true;


    FlightSystem f;
    vector <Aircraft> allAircraftList_{};

    std::string flightNumber, airline, aircraftType, gridReference;
    int groundSpeed, altitude, heading;

    const int MaxAlt = 60000, MinAlt = 0, MaxSpeed = 800, MinHeading = 0, MaxHeading = 360;

    regex fNumber{ "([a-z]{2}([a-z])?[0-9]([0-9])?([0-9])?([0-9])?([0-9])?)" };

    do {
        std::cout << "#########################################################" << endl;
        std::cout << "#Flight Control Simulation#" << endl;
        std::cout << "#########################################################" << endl;
        std::cout << "" << endl;

        std::cout << tab << "1. Add Aircraft" << endl;
        std::cout << tab << "2. List All Aircraft" << endl;
        std::cout << tab << "3. List All Cruising Aircraft" << endl;
        std::cout << tab << "4. Number of Aircrafts in Sector" << endl;
        std::cout << tab << "5. Remove Aircraft" << endl;
        std::cout << tab << "6. Change Heading" << endl;
        std::cout << tab << "7. Get Heading" << endl;
        std::cout << tab << "8. Change Altitude" << endl;
        std::cout << tab << "9. Get Altitude" << endl;

        std::cout << "" << endl;
        std::cout << "---------------------------------------------------------" << endl;

        std::cout << "Please enter an option, between 1-9:" << endl;

        std::cin >> input;

        switch (input) {
        case:
             break;
        default:
            cout << "Your selection must be between 1 and 9" << endl;

        }
        return 0;
    } while (input != 9);
}

Here is my advice. Use a while loop. do-while loop in this case is unnecessary.

here's how it can be used (i made a simple program that enters a number, you can modify it accordingly, just replace my code with your switch statements)

 #include<iostream>
 using namespace std;
 int main()
{
int x;
bool repeat = true;
while(repeat)
{
    cout<<"enter a number"<<endl;
    cin>>x;
    cout<<"do you want to enter another number (y/n)"<<endl;
    char ch;
    cin>>ch;
    if(ch=='y'||ch =='Y')
        continue;
    else
        repeat = false;
}
cout<<"you entered "<<x;
}

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