You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

停车场程序中switch内continue未跳转至循环起始的问题求助

停车场管理程序continue语句问题排查

问题描述

编程新手编写停车场管理程序时,遇到如下问题:当用户在确认停车时输入'y'以外的字符,程序提示停车取消后并未回到菜单选择循环的开头,反而直接退出了程序,这与预期的continue语句作用不符。

问题代码

#include <iostream>
#include <string.h>
#include <fstream>
#include <vector>

int rick_amnt = 100, car_amnt = 200, busses_amnt = 300;
int car_parked = 0;

using namespace std;

int main()
{
    int type, menu;
    char confirm;

    do {
        cout << "-------MENU--------\n";
        cout << "01 : Parking tickets" << endl;
        cout << "02. Parked cars" << endl;

        cout << "Choose an option : ";
        cin >> menu;

        system("cls");

        if (menu == 1) {

            cout << "01: Rickshaw\n02: Car\n03: Bus\n";
            cout << "what is your car type : ";
            cin >> type;

            switch (type) {
            case 1: {
                cout << "Rickshaw parking ticket = " << rick_amnt << endl;
                cout << "do you wish to park your Rickshaw?(y/n) ";
                cin >> confirm;
                if (confirm == 'y') {
                    cout << "Successfull payment!" << endl;
                    car_parked++;
                    break;
                }
                else {
                    cout << "Car parking declined" << endl;
                    continue;
                }
            }

            case 2: {
                cout << "Car parking ticket = " << car_amnt << endl;
                cout << "do you wish to park your Car? (y/n) ";
                cin >> confirm;
                if (confirm == 'y') {
                    cout << "Successfull payment!" << endl;
                    car_parked++;
                    break;
                }
                else {
                    cout << "Car parking declined" << endl;
                    continue;
                }
            }

            case 3: {
                cout << "Buses parking ticket = " << busses_amnt << endl;
                cout << "do you wish to park your Bus?(y/n) ";
                cin >> confirm;
                if (confirm == 'y') {
                    cout << "Successful payment!" << endl;
                    car_parked++;
                    break;
                }
                else {
                    cout << "Car parking declined" << endl;
                    continue;
                }
            }

            default:
                cout << "invalid identity" << endl;
            }
        }

        else if (menu == 2) {
            cout << "Parked cars = " << car_parked << endl;
        }
        else
            cout << "Invalid entry" << endl;

        cout << "Ty again?(y/n) ";
        cin >> confirm;
        system("cls");
    } while (confirm == 'y');

    cout << "Successfully exited the parking lot.\nHave a nice day!" << endl;

    return 0;
}

问题根源

你代码中的continue确实会跳转到do-while循环的条件判断处,但此时confirm变量的值是用户输入的非'y'(比如'n'),导致循环条件confirm == 'y'不成立,程序直接退出循环。这就是预期回到菜单却直接退出的核心原因。

解决方案

方式1:重置confirm后执行continue

在每个case的else分支里,输出取消提示后,先将confirm设置为'y',再执行continue。这样循环条件会满足,程序回到菜单开头:

else {
    cout << "Car parking declined" << endl;
    confirm = 'y'; // 强制让循环条件成立
    continue;
}

方式2:用标志变量控制流程

新增一个布尔标志变量,控制是否跳过后续的"是否重试"询问,避免修改confirm的语义:

int main()
{
    int type, menu;
    char confirm;
    bool skip_retry = false; // 新增标志变量

    do {
        skip_retry = false; // 每次循环重置标志
        cout << "-------MENU--------\n";
        cout << "01 : Parking tickets" << endl;
        cout << "02. Parked cars" << endl;

        cout << "Choose an option : ";
        cin >> menu;

        system("cls");

        if (menu == 1) {

            cout << "01: Rickshaw\n02: Car\n03: Bus\n";
            cout << "what is your car type : ";
            cin >> type;

            switch (type) {
            case 1: {
                cout << "Rickshaw parking ticket = " << rick_amnt << endl;
                cout << "do you wish to park your Rickshaw?(y/n) ";
                cin >> confirm;
                if (confirm == 'y') {
                    cout << "Successful payment!" << endl;
                    car_parked++;
                    break;
                }
                else {
                    cout << "Car parking declined" << endl;
                    skip_retry = true;
                    break; // 跳出switch
                }
            }

            case 2: {
                cout << "Car parking ticket = " << car_amnt << endl;
                cout << "do you wish to park your Car? (y/n) ";
                cin >> confirm;
                if (confirm == 'y') {
                    cout << "Successful payment!" << endl;
                    car_parked++;
                    break;
                }
                else {
                    cout << "Car parking declined" << endl;
                    skip_retry = true;
                    break; // 跳出switch
                }
            }

            case 3: {
                cout << "Bus parking ticket = " << busses_amnt << endl;
                cout << "do you wish to park your Bus?(y/n) ";
                cin >> confirm;
                if (confirm == 'y') {
                    cout << "Successful payment!" << endl;
                    car_parked++;
                    break;
                }
                else {
                    cout << "Car parking declined" << endl;
                    skip_retry = true;
                    break; // 跳出switch
                }
            }

            default:
                cout << "invalid identity" << endl;
            }
        }

        else if (menu == 2) {
            cout << "Parked cars = " << car_parked << endl;
        }
        else
            cout << "Invalid entry" << endl;

        if (!skip_retry) { // 仅当未取消停车时询问重试
            cout << "Try again?(y/n) ";
            cin >> confirm;
            system("cls");
        } else {
            system("cls");
        }
    } while (confirm == 'y');

    cout << "Successfully exited the parking lot.\nHave a nice day!" << endl;

    return 0;
}

额外优化建议

  • 修正拼写错误:Successfull改为Successful,Ty again改为Try again;
  • 变量名优化:busses_amnt改为bus_amnt(单数更符合语义);
  • 冗余头文件清理:<string.h>替换为<cstring>(C++标准头文件),<fstream>和<vector>未使用可删除;
  • 避免case穿透:确保每个case分支末尾都有break(当前代码已做到,需保持)。

内容的提问来源于stack exchange,提问作者Jack Drayton

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 09:39:24