因此,我正在制定飞机座位计划,我希望用户输入数字以选择一行,然后输入字母以选择连续六种选择之一(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;
}
答案 0 :(得分: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;
尝试在其中插入一些提示,看看要传递给每个变量的内容。如果您花时间用这种方式调试代码,则比我们提供答案要好得多。这些东西一开始总是很费时间,但是如果您有更具体的问题,请随时回来,我们很乐意为您提供帮助!