c++ - 无法输出可用和已占用的座位
问题描述
所以我正在制作一个飞机座位程序,我希望我的用户输入一个数字来选择一行并输入一个字母来选择连续的六个选项之一(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;
}
解决方案
与其交出代码,我认为介绍一些关于如何调试这样的案例的基础知识会更有益。与其一次性查看整个程序,不如尝试将其分解为多个部分,直到找出代码的哪个部分存在问题。例如,让我们看一下这个部分(提示,提示,提示):
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 并查看您传递给每个变量的内容。如果你花时间以这种方式调试你的代码,你会比我们给出答案要好得多。这些东西一开始总是需要时间,但如果您有更具体的问题,请随时回来,我们很乐意为您提供帮助!
推荐阅读
- python - Django表单字段不呈现
- java - 查找数组中的最大整数,在 Java 中具有指定的开始和结束索引,**递归**
- linux - 在makefile中更改PATH
- python-3.x - 在 Pytorch 内置的自定义 Batchnorm 中更新 running_mean 和 running_var 有问题吗?
- c#-4.0 - 这个 Entity Framework Core 异常是什么意思?
- r - 如何扩展您的绘图,以便可以看到所有标签而不会在 R 中重叠
- python - 链接:致命错误 LNK1104:无法使用 CMake 查找库打开文件“python38.lib”
- excel - Excel 中的“ActiveX 组件无法创建对象”错误
- angular - 当状态由ngrx-forms管理时,如何以角度动态地将formgroup控件添加到formarray?
- python - Selenium 似乎无法在页面上找到文本