Seb*_*era 2 c++ loops function
我有这个代码的问题,我插入一个循环,所以listOfSeats始终调用该函数,程序永远不会停止运行.问题是它只运行一次,我不能保证我在阵列座位中更改的值被更改.我不知道为什么会这样,请帮忙.当我添加一个调试器来查看它崩溃的原因时,我得到了这个ScreenShot 1和ScreenShot2.请帮助我真的不明白这意味着什么.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
string listOfSeats();
const int arraySize = 20;
int main() {
while(true) {
cout << "These are the type of seats we offer during the flight and their current status:\n";
cout << "(LW) Left Window\n(AL) Aisle left\n(AR) Aisle Right\n(RW) Right Window\n\n";
listOfSeats();
}
}
string listOfSeats() {
string typeOfSeat;
int numOfSeat;
static string seat[ arraySize ];
for ( int i = 0; i < 20; ++i )
seat[ i ] = "Available";
cout << setw( 5 )<< "Seat" << setw( 10 ) << "Status" <<endl;
for ( int lw = 0; lw < 5; ++lw )
cout << setw( 4 ) << "LW"<<lw+1 << setw( 12 ) << seat[ lw ] << endl;
for ( int al = 5; al < 10; ++al )
cout << setw( 4 ) << "AL"<<al-4 << setw( 12 ) << seat[ al] << endl;
for ( int rl = 10; rl < 15; ++rl )
cout << setw( 4 ) << "RL"<<rl-9 << setw( 12 ) << seat[ rl] << endl;
for ( int rw = 15; rw < 20; ++rw )
cout << setw( 4 ) << "AL"<<rw-14 << setw( 12 ) << seat[ rw] << endl;
cout << "\nPlease enter the type of seat you want to reserve (just the characters on brackets):";
for(;;) {
cin >> typeOfSeat;
if (typeOfSeat=="LW") {
cout <<"You have chosen a Left window seat.";
break;
}
else if(typeOfSeat=="AL") {
cout <<"You have chosen an Aisle left seat.";
break;
}
else if(typeOfSeat=="AR") {
cout <<"You have chosen an Aisle right seat.";
break;
}
else if(typeOfSeat=="RW") {
cout <<"You have chosen a Right window seat.";
break;
}
else {
cout <<"Invalid option entered. \nPlease enter a valid option:";
continue;
}
}
cout << "\nFrom the list presented above. Enter the number of the type of seat you want to reserve\n(just the number following the characters):";
for(;;) {
cin >> numOfSeat;
if (numOfSeat==1) {
cout <<"You have reserved the number "<< numOfSeat<<" "<<typeOfSeat<<" seat.";
break;
}
else if(numOfSeat==2) {
cout <<"You have reserved the number "<< numOfSeat <<" "<<typeOfSeat<<" seat.";
break;
}
else if(numOfSeat==3) {
cout <<"You have reserved the number "<< numOfSeat <<" "<<typeOfSeat<<" seat.";
break;
}
else if(numOfSeat==4) {
cout <<"You have reserved the number "<< numOfSeat <<" "<<typeOfSeat<<" seat.";
break;
}
else if(numOfSeat==5) {
cout <<"You have reserved the number "<< numOfSeat <<" "<<typeOfSeat<<" seat.";
break;
}
else {
cout <<"Invalid option entered. \nPlease enter a valid option:";
continue;
}
}
if (typeOfSeat=="LW") {
seat[ numOfSeat-1 ]="Reserved";
}
}
Run Code Online (Sandbox Code Playgroud)
listOfSeats 每次呼叫时将座位初始化为可用.
编辑:另外,我没有注意到我第一次发布这个,你需要用函数结果类型string替换void,否则添加一个return语句.目前,当函数返回时,您有未定义的行为.提高编译器的警告级别,例如-Wallg ++或/W4Visual C++,很可能会产生诊断级别.