循环调用该函数只需一次C++

Seb*_*era 2 c++ loops function

我有这个代码的问题,我插入一个循环,所以listOfSeats始终调用该函数,程序永远不会停止运行.问题是它只运行一次,我不能保证我在阵列座位中更改的值被更改.我不知道为什么会这样,请帮忙.当我添加一个调试器来查看它崩溃的原因时,我得到了这个ScreenShot 1ScreenShot2.请帮助我真的不明白这意味着什么.

#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)

Che*_*Alf 5

listOfSeats 每次呼叫时将座位初始化为可用.

编辑:另外,我没有注意到我第一次发布这个,你需要用函数结果类型string替换void,否则添加一个return语句.目前,当函数返回时,您有未定义的行为.提高编译器的警告级别,例如-Wallg ++或/W4Visual C++,很可能会产生诊断级别.

  • 要求完美的问题是不合理的.有了完美的理解,需要制定完美的问题,就没有必要问.我会说这个问题很好,很好:实际代码发布,截图,尝试(虽然不完美)解释错误. (3认同)
  • @MaxLanghof:一个合理的反应是**问**OP,不投票结束这个问题.正如评论中对此讨论的合理反应一样,是表达一个人的观点或见解,而不是低估答案(正如有人现在所做的那样).我多次放弃Stack Overflow.我认为现在已经接近最终放弃了.试图用网站的设施和缺乏设施来支持大批破坏者的人是没有希望的. (3认同)