C ++ switch语句无法正常工作

时间:2018-03-25 01:25:45

标签: c++ loops switch-statement

因此,对于我正在编写的程序,我有一个菜单,用户可以输入一个整数选项来计算一些不同的东西。选择1-4是计算,5应调用函数将数据从文件打印到控制台,6退出所有内容。无论出于什么原因,在我使用的switch语句中添加了一个新的case(5)后,我的代码有点破碎了。当我输入5时,程序仍然要求我输入一个重量(我根本不理解),之后它将进入无限循环,这意味着它必须在我的打印中进入while循环()函数。我不明白到底是怎么回事,所以我真的很感激第二眼。

int main()
{
srand(time(0));

bool repeat = true;
bool loopFlag = true;
float calories = 0;
string intensity = "";



cout << "Welcome to Carmello's Fitness Center" << endl;

do // main loop
{
    fstream transactionLog;
    transactionLog.open("userfile.txt", std::ios::in |std::ios::out |std::ios::app);

    int idNum;
    menu(idNum); // simply displays the menu

    loopFlag = true;

    int choice = userChoice(choice, loopFlag); // gets the users menu choice
    loopFlag = true; // this resets the loopflag to ensure it can be used again after the last validation

    float weight = userWeight(weight, loopFlag); // gets users weight and converts into kilograms for further calculations
    float weightPoundsCopy = weight;
    weight = weight / 2.2;

    int time = 0;
    float calories = 0;

    switch (choice)
    {
    case 1:
        {
            int lower = 30, upper = 60;
            int activity = BIKING;
            string description = "riding the stationary bike: ";
            met(weight, loopFlag, activity, lower, upper, description, time, calories, intensity);
            break;
        }
    case 2:
        {
            int lower = 30, upper = 60;
            int activity = RUNNING;
            string description = "running on the treadmill: ";
            met(weight, loopFlag, activity, lower, upper, description, time, calories, intensity);
            break;
        }
    case 3:
        {
            int lower = 15, upper = 30;
            int activity = LIFTING;
            string description = "lifting weights: ";
            met(weight, loopFlag, activity, lower, upper, description, time, calories, intensity);
            break;
        }
    case 4:
        {
            int lower = 60, upper = 90;
            int activity = YOGA;
            string description = "doing Hatha Yoga: ";
            met(weight, loopFlag, activity, lower, upper, description, time, calories, intensity);
            break;
        }
    case 5:
        {
            cout << "5 CHOSEN";
            print (transactionLog);
            break;
        }
    }
    cout << setfill('0') << setw(5) << idNum << endl;
    cout << choice << endl;
    cout << time << endl;
    cout << weightPoundsCopy << endl;
    cout << calories << endl;
    cout << intensity << endl;

    outputFunction(transactionLog, idNum, choice, time, weightPoundsCopy, calories, intensity);
    transactionLog.close();
}
while (repeat);
}   

这似乎是最相关的功能

int userChoice (int choice, bool loopFlag)
{
    do // loop to validate user's activity (choice) input
    {
        cin >> choice;
        if (cin.fail() || choice > 6 || choice < 1)
        {
            cout << "\nInvalid choice. Please choose from option 1 through 6." << endl;
            cin.clear();
            cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
        }
        else if (choice == 6)
        {
            exit(EXIT_SUCCESS); // exits program if user chooses to
        }
        else
        {
            loopFlag = false;
        }
    }
    while (loopFlag); // loop will continue until input meets correct conditions and thus sets loopflag to false

    return choice;
}

float userWeight(float weightPounds, bool loopFlag)
{
    do // this do-while loop validates weight input
        {
        cout << "\nPlease enter your weight in pounds: " << endl;
        cin >> weightPounds;

        if (cin.fail() || weightPounds <= 0 || weightPounds >= 1000)
        {
            cout << "\nInvalid weight entry!" << endl;
            cin.clear();
            cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
        }
        else
        {
            loopFlag = false;
        }
    }
    while (loopFlag);

    return weightPounds;
}

void print(fstream &transactionLog)
{
    // reset everything and go to the beginning of the file
transactionLog.clear();
transactionLog.seekg(0, std::ios::beg);
// start the streaming >>
if (enterPassword)
{
    while(!transactionLog.eof())
    {
        cout << setw(20) << left << "UserID" << "Activity" << "Mins" << "Weight"
             << "Calories" << "Intensity" << "Time Stamp" << endl;
    }
}
else
{
    cout << "SECURITY BREACH" << endl;
    return;
}
}

1 个答案:

答案 0 :(得分:0)

我认为你的代码就像编写的那样工作。您已经被要求用户通过调用&#34; userChoice&#34;功能,并且在此之后,您已经通过调用&#34; userWeight&#34;来询问用户体重。功能。只有在那之后,您才在切换案例中解析了用户输入。

如果您希望您的程序没有询问用户体重,您应该写下这样的内容:

if (userInput < 5)
{
    weight = userWeight(weight, loopFlag);
    float weightPoundsCopy = weight;
    weight = weight / 2.2;
}