【问题标题】:Menu driven project菜单驱动项目
【发布时间】:2013-07-03 20:09:23
【问题描述】:

讲师希望我们编写一个程序,该程序仅在用户想要重新开始选择并添加选项以继续进行另一个选择时才能重新显示菜单。

我遇到的问题是当用户选择一个从 1 到 4 的数字并完成选择时,程序会询问用户是否要继续进行另一个选择,当用户说不时,程序仍然要求选择一个不结束程序的数字。

这是我目前编写的代码:

#include<iostream>

using namespace std;
int sp;
int speed = 0;
int M, K, c, x;
const int MINspeed = 10;
const int MAXspeed = 40;
int GetSpeed();
int GetMinSpeed();
int GetMaxSpeed();
int CheckContinue();
int selection;
int GetSpeed()
{
    char c;
    while(true)
    {
        cout << "\nDo you want the speed in mph or km/h? \n" 
            << "\nEnter M or K followed by Enter: " << endl;
        cin >> c;

        if( (c != 'M')&& (c != 'K'))
        {
            cout << "Incorrect Selection. Try Again! \n\n";
            break;
        }

        if ( c == 'M')
        {
            cout << "\nSpeed in mph: " << speed << endl;
            return speed;
        }
        else if(c == 'K')
        {
            double toKmPerHour = 1.61;
            double speedInKmPerHour = speed * toKmPerHour;
            cout << "\nSpeed in km/h:" << speedInKmPerHour << endl;
            break;
        }
        CheckContinue();
    }
    return 0;
}
int GetMinSpeed()
{
    cout << "MIN speed = " << MINspeed << endl;
    CheckContinue();
    return 0;
}
int GetMaxSpeed()
{
    cout << "MAX speed = " << MAXspeed << endl;
    CheckContinue();
    return 0;
}
/*int SetSpeed(int sp)
{
cout << "The Set Speed is " << sp << endl;
return 0;
}
*/
void SetSpeed()
{
    cout << "Input your speed: ";
    cin >> speed;
    CheckContinue();
}
int CheckContinue(void)
{
    char x;
    while(true)
    {
        cout << "\nDo you want to continue with another selection? \n" 
            << "\nEnter Y or N followed by Enter: " << endl;
        cin >> x;
        if ( x == 'Y')
        {
            int selection;

            cout << "Selection Menu" << endl;
            cout << "--------------" << endl;
            cout << "\n1. Set Speed" << endl;
            cout << "2. Get Speed" << endl;
            cout << "3. Get MAX Speed" << endl;
            cout << "4. Get MIN Speed" << endl;
            cout << "5. Exit" << endl;
            cout << "\nYour selection :" <<endl;
            cin >> selection;
            switch(selection)
            {
            case 1:
                SetSpeed();
                break;

            case 2:
                GetSpeed();
                break;

            case 3:
                GetMaxSpeed();
                break;

            case 4:
                GetMinSpeed();
                break;

            case 5:
                cout << "Good Bye" << endl;
                break;
            }
        }
        else if(x == 'N')
        {
            break;
        }
    }
    return 0;
}

/* 
In this menu function, it will ask the user to input the selection, ranging from 1 to 5. 
If the user puts a number that is not between 1 to 5 or letters, then the program will 
ask the user to input a valid selection.
*/
void menu()
{
    int selection;

    cout << "Selection Menu" << endl;
    cout << "--------------" << endl;
    cout << "\n1. Set Speed" << endl;
    cout << "2. Get Speed" << endl;
    cout << "3. Get MAX Speed" << endl;
    cout << "4. Get MIN Speed" << endl;
    cout << "5. Exit" << endl;
    int bye = 0;
    while(1)
    {
        cout << "\nYour selection :" <<endl;
        cin >> selection;
        bye = 0;
        if((selection <= 5)&&(selection >= 1))
        {
            switch(selection)
            {
            case 1:
                SetSpeed();
                break;

            case 2:
                GetSpeed();
                break;

            case 3:
                GetMaxSpeed();
                break;

            case 4:
                GetMinSpeed();
                break;

            case 5:
                cout << "Good Bye" << endl;
                bye = -1;
                break;
            }
        }
        else
        {
            cout << "\nPlease input valid selection: " << endl;
            cin >> selection;
            switch(selection)
            {
            case 1:
                SetSpeed();
                break;

            case 2:
                GetSpeed();
                break;

            case 3:
                GetMaxSpeed();
                break;

            case 4:
                GetMinSpeed();
                break;

            case 5:
                cout << "Good Bye" << endl;
                bye = -1;
                break;
            }
        }
        if(bye == -1)
        {
            break;
        }

    }
}

int main()
{
    menu();

    return 0;


}//end of main function

【问题讨论】:

  • checkContinue 函数中执行此操作的代码吗?
  • 首先,组织你的代码,一团糟,你使用同一个菜单,你一遍又一遍地写,使用一个函数,问题是,你调用 checkContinue在另一个循环内......,所以当 checkContinue 完成时,它返回到菜单中的循环......所以你认为是同一个菜单,但不是

标签: c++ menu switch-statement selection case


【解决方案1】:

这可能符合您的目的。如果不适合您,请根据您的要求调用 ask()。

#include <iostream>
#include <stdlib.h>

using namespace std;
char * title;
int a , b;
void menu();
void print(const char *c , int res )
{
    cout<<"\n\n\n\n\nThe "<<c<<" of "<<a<<" and "<<b<<" is : " <<res<<endl;
}
void add()
{
    print("Addition" , (a+b));
}

void sub()
{
    print("subtraction" , (a-b));
}

void mul()
{
    print("Multiplication" , (a*b));
}

void div()
{
    print("Division" , (a/b));
}
void ask()
{
    bool call_menu;
    char ch;
    cout<<"\n\n\n\n\n\n DO you Want to Continue? Y - N:  ";
    cin>>ch;
    if(ch=='Y' || ch=='y')
    {
        call_menu= true;
    } 
    else 
    {
        if(ch=='N' || ch == 'n')
        {
            call_menu= false;
        }
        else 
        {
            cin.clear();
            ask();
        }
    }
    if(call_menu)
    {
        system("clear"); // change this to system("cls") if on windows
        menu();
    }
    else
    {
        system("clear"); // change this to system("cls") if on windows
        cout<<"\n\n\n\n\n\n\n\t\t\tHave a Nice Day ! \n\n\n"<<endl;
    }
}
void input(int *first , int *second)
{
    system("clear"); // change this to system("cls") if on windows
    cout<<"\n\n\n\t\t\t\t Calculator \n\n\n\n"<<endl;
    cout<<"Enter the First Number : ";
    cin>>(*first);
    cout<<"\nEnter the Second Number :";
    cin>>(*second);
}
void menu()
{
    int ch;
    cout<<"\n\n\n\t\t\t\t Calculator \n\n\n\n"<<endl;
    cout<<"\n\n\t\t\t1 . Addition"<<endl;
    cout<<"\n\n\t\t\t2 . Subtract"<<endl;
    cout<<"\n\n\t\t\t3 . Multiply"<<endl;
    cout<<"\n\n\t\t\t4 . Division"<<endl;
    cout<<"\n\n\t\t\t5 . Exit" <<endl;
    cout<<"\n\n\n\n Enter Your Choice : ";
    cin>>ch;
    if(ch >=1 && ch <5){
    input(&a , &b);
    }
    switch(ch)
    {
        case 1:
        add();
        ask();
        break;
        case 2: 
        sub();
        ask();
        break;
        case 3:
        mul();
        ask();
        break;
        case 4:
        div();
        ask();
        break;
        case 5:
        exit(0);
        break;
        default:
        system("clear"); // change this to system("cls") if on windows
        cin.clear();
        cin.ignore();
        menu();
        break;
    }

}
int main(int argc, char **argv)
{
    menu();
    return 0;
}

根据您的要求修改它。

【讨论】:

    【解决方案2】:

    您问题中的代码存在几个问题。最大的问题是有很多冗余代码可以通过一些小的调整轻松消除。您有菜单打印和代码来对多个位置的选择进行操作。这将使管理继续过程变得更加困难。通过消除冗余代码并调整mainmenu 中的逻辑,您不仅可以降低复杂性,还可以使其更易于管理。

    例如,menu 可以更改为删除 while 循环并返回一个布尔值来指示用户是否想要退出。这将允许您选择一个选项,对其采取行动,然后返回让程序的其他部分处理询问用户是否要继续。

    以下示例是对原始代码的修改。它只解决了要求用户继续的逻辑并消除了冗余的菜单代码。您应该查看整个代码并根据需要进行额外的调整。

    #include <iostream>
    #include <string>
    
    using namespace std;
    int sp;
    int speed = 0;
    int M, K, c, x;
    const int MINspeed = 10;
    const int MAXspeed = 40;
    
    int GetSpeed()
    {
        char c;
    
        while(true)
        {
            cout << "\nDo you want the speed in mph or km/h? \n" 
                << "\nEnter M or K followed by Enter: " << flush;
            cin >> c;
    
            if( (c != 'M')&& (c != 'K'))
            {
                cout << "Incorrect Selection. Try Again! \n\n" << flush;
                continue;
            }
    
            if ( c == 'M')
            {
                cout << "\nSpeed in mph: " << speed << endl;
                return speed;
            }
            else if(c == 'K')
            {
                double toKmPerHour = 1.61;
                double speedInKmPerHour = speed * toKmPerHour;
                cout << "\nSpeed in km/h:" << speedInKmPerHour << endl;
                return speed;
            }
        }
    
        return 0;
    }
    
    int GetMinSpeed()
    {
        cout << "MIN speed = " << MINspeed << endl;
        return 0;
    }
    
    int GetMaxSpeed()
    {
        cout << "MAX speed = " << MAXspeed << endl;
        return 0;
    }
    
    void SetSpeed()
    {
        cout << "Input your speed: ";
        cin >> speed;
    }
    
    /* 
    In this menu function, it will ask the user to input the selection, ranging from 1 to 5. 
    If the user puts a number that is not between 1 to 5 or letters, then the program will 
    ask the user to input a valid selection.
    
    returns false if the user has selected the exit option
    returns true for all other options
    */
    bool menu()
    {
        cout << "Selection Menu" << endl;
        cout << "--------------" << endl;
        cout << "\n1. Set Speed" << endl;
        cout << "2. Get Speed" << endl;
        cout << "3. Get MAX Speed" << endl;
        cout << "4. Get MIN Speed" << endl;
        cout << "5. Exit" << endl;
    
        int selection;
        cout << "\nYour selection :" <<endl;
        cin >> selection;
    
        switch(selection)
        {
        case 1:
            SetSpeed();
            break;
    
        case 2:
            GetSpeed();
            break;
    
        case 3:
            GetMaxSpeed();
            break;
    
        case 4:
            GetMinSpeed();
            break;
    
        case 5:
            cout << "Good Bye" << endl;
            return false;
            break;
    
        default:
            cout << "\nPlease input valid selection: " << endl;
        }
    
        return true;
    }
    
    
    int main()
    {
        for(bool process = true; process;)
        {
            process = menu();
            if(process)
            {
                for(bool valid = false; !valid;)
                {
                    cout << "\nDo you want to enter another selection? (Yes/No) " << flush;
                    cin.clear();
                    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    
                    string line;
                    getline(cin, line);
    
                    if(line == "No")
                    {
                        valid = true;
                        process = false;
                    }
                    else if(line == "Yes")
                    {
                        valid = true;
                    }
                    else
                    {
                        cout << "\nInvalid input\n\n" << flush;
                    }
                }
            }
        }
    
        return 0;
    }//end of main function
    

    【讨论】:

    • 我目前正在为夏季学期学习 C plus plus,而且速度很快。所以我几乎没有赶上,但从不知道你可以在这种方法中使用。在实验期间,讲师希望我们将 main() 缩短,但我没有遵循与讲师相同的步骤,因为我很想做不同的事情。我是这方面的初学者,现在我知道下一步是什么,那就是重组我的代码。也许这就是我无法解决问题的原因。
    • 我运行你的代码,你的代码没有错误,但这不是我想要的。我希望用户输入设定的速度并按回车键。在用户输入一个设定的速度后,程序会询问用户“你想再做一个选择吗?(Y/N)”如果用户说是,那么用户将输入另一个选择。但是,如果用户说不,那么程序将退出。但是你的代码没有这样做,它告诉我选择一个选择,当我这样做时,它会自动退出而不问我“你想要做另一个选择吗?(Y/N)”
    • 很容易更改为main() 以检查 YesNo 而不是 Y。我已经更新了示例。
    猜你喜欢
    • 2013-06-19
    • 1970-01-01
    • 2014-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-23
    • 2013-04-23
    相关资源
    最近更新 更多