首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >循环只调用函数一次C++

循环只调用函数一次C++
EN

Stack Overflow用户
提问于 2018-07-11 15:18:24
回答 3查看 361关注 0票数 2

我对这段代码有一个问题,我插入了一个循环,所以函数listOfSeats总是被调用,程序永远不会停止运行。问题是它只运行一次,并且我不能保证我在数组基座中更改的值会被更改。我不知道为什么会这样,请帮帮忙。当我添加一个调试器来查看它崩溃的原因时,我得到了这个ScreenShot 1ScreenShot2。请帮帮我,我不太明白这是什么意思。

代码语言:javascript
复制
#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";
    }
}
EN

回答 3

Stack Overflow用户

发布于 2018-07-11 15:42:34

每次调用listOfSeats时,它都会将席位初始化为可用。

编辑:而且,当我第一次发布这篇文章时,我没有注意到,您需要将函数结果类型string替换为void,或者添加一条return语句。当前,当函数返回时,您有未定义的行为。提高编译器的警告级别,例如用于g++的-Wall或用于Visual C++的/W4,很可能会产生关于这方面的诊断。

票数 5
EN

Stack Overflow用户

发布于 2018-07-11 15:57:24

使用返回类型string声明函数“string listOfSeats()”,但不在该函数的作用域内返回。更改函数以不返回任何内容(也称为void)代码的第5行和第19行。

简而言之,更改以下内容:

代码语言:javascript
复制
string listOfSeats() //has return type string

要这样做:

代码语言:javascript
复制
void ListOfSeats() //has return type void

在5号和19号线。

票数 1
EN

Stack Overflow用户

发布于 2018-07-11 15:56:24

你需要洗掉你的cin。它在无限循环中。

将你的else循环替换为这个-

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

包括在标题下-

代码语言:javascript
复制
#include <limits>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51279431

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档