#include "Menu.h"
#include <iostream>
#include <stdio.h>
#include <string>
void Menu::displayMenu() {
cout << " 1. New User login \n 2. View available vehicles \n 3. Return Vehicle \n 4. Exit Program \n";
char choice;
cin >> choice;
main:
switch (choice) {
case '1': //new user option
login();
break;
case '2': //This will be the option for viewing available cars. each choice in this switch will lead to car info and choice to rent.
carmenu:
cout << "Now viewing available vehicles...Select vehicle for further information...\n";
cout << " 1. Toyota Carolla \n 2. Toyota Rav4 \n 3. Toyota Prius \n 4. Ford Mustang \n 5. Ford Transit \n 6. Main Menu \n";
char rent;
char carchoice;
cin >> carchoice;
switch (carchoice) {
case '1':
cout << "Toyota Carolla..\n Passenger class \n 30 MPG \n 4 Doors \n";
cout << "Enter Y to rent vehicle or enter N to return to car selection...\n";
cin >> rent;
if (rent == 'Y') {
//construct
}
else if (rent == 'N') { goto carmenu; };
case '2':
cout << "Toyota Rav4..\n Passenger class \n 24 MPG \n 5 Doors \n";
cout << "Enter Y to rent vehicle or enter N to return to car selection...\n";
cin >> rent;
if (rent == 'Y') {
//call passenger car constructor called Rav4 with proper data entries.
}
else if (rent == 'N') { goto carmenu; };
break;
case '3':
cout << "Toyota Prius.. \n Passenger class \n 54 MPG \n 4 Doors \n";
cout << "Enter Y to rent vehicle or enter N to return to car selection...\n";
cin >> rent;
if (rent == 'Y') {
//call passenger car constructor called Prius with proper data entries.
}
else if (rent == 'N') { goto carmenu; };
break;
case '4':
cout << "Ford Mustang.. \n Passenger class \n 18 MPG \n 2 Doors \n";
cout << "Enter Y to rent vehicle or enter N to return to car selection...\n";
cin >> rent;
if (rent == 'Y') {
//call passenger car constructor called Mustang with proper data entries.
break;
}
else if (rent == 'N') { goto carmenu; };
break;
case '5':
cout << "Ford Transit ..\n Cargo class \n 14 MPG \n";
cout << "Enter Y to rent vehicle or enter N to return to car selection...\n";
cin >> rent;
if (rent == 'Y') {
Cargo van(14);
van.set_purpose();
}
else if (rent == 'N') { goto carmenu; };
case '6':
goto main;
}//second switch
}//first switch
}//function end
我希望这不太难以阅读......我正在做一个项目,我必须为公司设计一个汽车租赁计划。主菜单的第二种情况将用户带到一个菜单,在该菜单中他可以选择要租用的汽车类型。菜单还没有完成,但我已经完成了汽车选择菜单选项5的课程。租用汽车时(为if语句输入Y)正确调用货物的构造函数,但程序要么在输入目的后立即结束,要么返回到汽车租赁页面。我不知道在哪里放假或去发言。任何人都可以帮忙??
答案 0 :(得分:0)
通过将break
放在每个案例的末尾,它将突破当前案例下面的案例并执行其余代码。如果您为租金选择输入“Y”,您的程序将再次返回到汽车菜单,因为变量choice
永远不会被重新分配,因此它将保持为“2”。如果输入“N”,您将返回选择汽车,因为goto carmenu
。对于所有其他输入,您没有其他情况,因此程序将在中断后退出。
我建议将代码分解为函数,而不是使用goto
。如果您遇到这样的错误,可以更容易理解和理解。