C ++循环调用一次函数

时间:2018-07-11 07:18:24

标签: c++ function loops

我对这段代码有问题,我插入了一个循环,因此始终调用函数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";
    }
}

3 个答案:

答案 0 :(得分:5)

listOfSeats会在每次调用时将座位初始化为可用。

编辑:另外,当我第一次发布此消息时,我没有注意到,您需要将函数结果类型string替换为void,或者添加一个{{ 1}}语句。当前,函数返回时,您将具有“未定义行为”。提升编译器的警告级别,例如对于g ++,return或对于Visual C ++,-Wall最有可能对此产生诊断。

答案 1 :(得分:1)

您使用返回类型string声明了函数'string listOfSeats()',但没有在函数范围内返回。更改函数以在代码的第5行和第19行不返回任何内容(也称为void)。

简而言之,改变它:

string listOfSeats() //has return type string

对此:

void ListOfSeats() //has return type void

第5行和第19行。

答案 2 :(得分:0)

您需要刷新cin。它进入了无限循环。

用这个代替您的其他循环-

    else {
        cout <<"Invalid option entered. \nPlease enter a valid option:";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
        continue;
    }

在标题下方包含-

#include <limits>