0

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

我遇到的问题是当用户选择一个从 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
4

2 回答 2

0

您的问题中的代码存在几个问题。最大的问题是有很多冗余代码可以通过一些小的调整轻松消除。您有菜单打印和代码来对多个位置的选择进行操作。这将使管理继续过程变得更加困难。通过消除冗余代码并调整 and 中的逻辑,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
于 2013-07-03T21:35:36.377 回答
0

这可能符合您的目的。如果不适合您,请根据您的要求调用 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;
}

根据您的要求对其进行修改。

于 2013-07-03T21:19:21.370 回答