首页 > 解决方案 > 无法输出可用和已占用的座位

问题描述

所以我正在制作一个飞机座位程序,我希望我的用户输入一个数字来选择一行并输入一个字母来选择连续的六个选项之一(A 到 F)。我还希望用户只要输入字母“C”就可以继续输入座位。我编写了大部分代码,但由于某种原因,座椅在第一次初始化和显示后没有输出,我无法使其输出。程序刚刚停止。在纠正我在逻辑中犯的任何错误之前,我试图输出结果。

//Any unused includes are part of the default code

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cmath>
#include <string>
#include <cassert>

using namespace std;

const int rowsz = 5;
const int colsz = 6;

int main()
{
    char seating[rowsz][colsz];
    char y = ' '; //Row
    char x = ' '; //Col
    char taken = 'X'; // This letter is for marking booked seats
    char letter = ' '; // This letter initializes the seat positions.
    char let = ' '; // Choice for a seat
    char choice = 'c'; // This let's the user quit or continue booking seats
    int rownum = 1; // Row number
    for(int row = 1; row <= rowsz; row++)
    {
        letter = 'A';
        cout << rownum;
        rownum++;
        for(int col = 0; col < colsz; col++)
        {
            seating[row][col] = letter;
            letter++;
            cout << seating[row][col];
        }
        cout << endl;
    }
    cout << "Would you like to get a seat? Press C. TO quit, press Q: "; cin >> choice;
    while(toupper(choice) == 'C')
    {
        cout << "Enter a row. Ex: 1,2,3... ";cin >> y;
        cout << "Enter a Letter for a seat: "; cin >> x;

        if(toupper(x) == 'A')
            x = 0;
        if(toupper(x) == 'B')
            x = 1;
        if(toupper(x) == 'C')
            x = 2;
        if(toupper(x) == 'D')
            x = 3;
        if(toupper(x) == 'E')
            x = 4;
        if(toupper(x) == 'F')
            x = 5;

        seating[y][x] = taken;

        for(int row = 1; row <= rowsz; row++)
        {
            for(int col = 0; col < colsz; col++)
            {
                cout << seating[row][col];
            }
            cout << endl;
        }
        cout << "Seat again? Press C to continue to seat and press Q to quit. "; cin >> choice;
    }
    if(toupper(choice) != 'C')
    {
        cout << "Thank you for using this program! " << endl;
    }

    return 0;
}

如果有人还在阅读这篇旧文章,我很久以前就设法让它工作了:

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cmath>
#include <string>
#include <cassert>

using namespace std;

//const int 5 = 5;
//rconst int 6 = 6;

int main()
{
    char seating[5][6];
    int y = 0; //Row
    int x = 0; //Col
    char letter = ' '; // This letter initializes the seat positions.
    char let = ' '; // Choice for a seat
    char choice = 'c'; // This let's the user quit or continue booking seats
    int rownum = 1; // Row number

    for(int row = 0; row < 5; row++)
    {
        letter = 'A';
        cout << rownum;
        rownum++;
        for(int col = 0; col < 6; col++)
        {
            seating[row][col] = letter;
            letter++;
            cout << seating[row][col];
        }
        cout << endl;
    }
    cout << "Would you like to get a seat? Press C. TO quit, press Q: "; cin >> choice;
    while(toupper(choice) == 'C')
    {
        cout << "Enter a row. Ex: 1,2,3... ";cin >> x;

        if(x <= 0 || x > 5){
            cout << "Try again: "; cin >> x;
        }

        cout << "Enter a Letter for a seat: "; cin >> let;


        if(toupper(let) == 'A'){
            y = 0;
        }
        else if(toupper(let) == 'B'){
            y = 1;
        }
        else if(toupper(let) == 'C'){
            y = 2;
        }
        else if(toupper(let) == 'D'){
            y = 3;
        }
        else if(toupper(let) == 'E'){
            y = 4;
        }
        else if(toupper(let) == 'F'){
            y = 5;
        }
        else{
            cout << "Wrong Input: " << endl;
            cin >> let;
        }

        while(seating[x][y] == 'X')
        {
            cout << "Try again? Row:" << endl;
            cin >> x;
            cout <<" and letter: " << endl;
            cin >> let;
        }

        seating[x - 1][y] = 'X';

        rownum = 1;
        for(int row = 0; row < 5; row++)
        {
            cout << rownum;
            rownum++;
            for(int col = 0; col < 6; col++)
            {
                cout << seating[row][col];
            }
            cout << endl;
        }
        cout << "Seat again? Press C to continue to seat and press Q to quit. "; cin >> choice;
    }
    if(toupper(choice) != 'C')
    {
        cout << "Thank you for using this program! " << endl;
    }

    return 0;
}

标签: c++

解决方案


与其交出代码,我认为介绍一些关于如何调试这样的案例的基础知识会更有益。与其一次性查看整个程序,不如尝试将其分解为多个部分,直到找出代码的哪个部分存在问题。例如,让我们看一下这个部分(提示,提示,提示):

while(toupper(choice) == 'C')
{
    cout << "Enter a row. Ex: 1,2,3... ";cin >> y;
    cout << "Enter a Letter for a seat: "; cin >> x;

    if(toupper(x) == 'A')
        x = 0;
    if(toupper(x) == 'B')
        x = 1;
    if(toupper(x) == 'C')
        x = 2;
    if(toupper(x) == 'D')
        x = 3;
    if(toupper(x) == 'E')
        x = 4;
    if(toupper(x) == 'F')
        x = 5;

    seating[y][x] = taken;
    for(int row = 1; row <= rowsz; row++)
    {
        for(int col = 0; col < colsz; col++)
        {
            cout << seating[row][col];
        }
        cout << endl;
    }
    cout << "Seat again? Press C to continue to seat and press Q to quit. "; cin >> choice;

尝试在其中插入一些 couts 并查看您传递给每个变量的内容。如果你花时间以这种方式调试你的代码,你会比我们给出答案要好得多。这些东西一开始总是需要时间,但如果您有更具体的问题,请随时回来,我们很乐意为您提供帮助!


推荐阅读