c -为什么我的循环在我正确输入程序后一直重复



我的代码有问题。我已经输入了正确的输入,但是程序一直在运行。我想我说错了,但我不知道是哪一部分。我该如何初始化它?

我的节目是关于座位预订的。我的目标是根据用户的输入来标记"X"。如果那个座位已经有人了,我需要提示一个问题,询问他们是否想再次输入座位,如果不想,我将引导他们到主菜单,他们的预订将被取消。

我所做的输出:

Enter your seat number: (EX: 1A)1a
Invalid option!
Enter your seat number: (EX: 1A)1A
Enter your seat number: (EX: 1A)

这是我的程序的一部分

#define ROWS 11
#define COLS 8
#define PASSENGERSIZE sizeof(passenger)
typedef struct{
char city[20], name[50], seatcol;
int age, seatrow, id;
}passenger;
char seat[ROWS][COLS];
int selection;
int seatavailable=60;
int i,j,x,k;
char answer, Y ;
int status=0;

void chooseseat(){
passenger p;
do{
printf("ntttEnter your seat number: (EX: 1A)");
scanf("%d%c",&p.seatrow, &p.seatcol);
if(p.seatrow == 1 && p.seatcol == 'A'){
seat[0][0]= 'X';}
else if(p.seatrow == 1 && p.seatcol == 'B'){
seat[0][2]= 'X';}
else if(p.seatrow == 1 && p.seatcol == 'C'){
seat[0][2]= 'X';}
else if(p.seatrow == 1 && p.seatcol == 'D'){
seat[0][3]= 'X';}
else if(p.seatrow == 1 && p.seatcol == 'E'){
seat[0][4]= 'X';}
else if(p.seatrow == 1 && p.seatcol == 'F'){
seat[0][5]= 'X';}
//2
.........
//10
else if(p.seatrow == 10 && p.seatcol == 'A'){
seat[9][0]= 'X';}
else if(p.seatrow == 10 && p.seatcol == 'B'){
seat[9][2]= 'X';}
else if(p.seatrow == 10 && p.seatcol == 'C'){
seat[9][2]= 'X';}
else if(p.seatrow == 10 && p.seatcol == 'D'){
seat[9][3]= 'X';}
else if(p.seatrow == 10 && p.seatcol == 'E'){
seat[9][4]= 'X';}
else if(p.seatrow == 10 && p.seatcol == 'F'){
seat[9][5]= 'X';}
else{
printf("Invalid option!");
p.id--;
getch();
} 
p.id++;
status = (p.seatrow && p.seatcol != seat[ROWS][COLS]);
}while(status);
if(!status){
printf("ntt Already allocate seat. Choose another seat? (Y/N)");
scanf("%c", answer);
if(answer == 'Y'){
chooseseat();
}
else{
printf("Your data will be not saved and will be returned to main menu:");
main();
}
}
}

试试这个:

#include <ctype.h>
#include <stdio.h>
#define ROWS 11
#define COLS 8
typedef struct
{
char city[20], name[50], seatcol;
int age, seatrow, id;
} passenger;
int seat[ROWS][COLS]; //0=available, >1: passenger number
void chooseSeat(int PassengerNumber)
{
passenger p;
for(;;)
{
unsigned SeatRowIndex;  //0=1st, 1=2nd, ...
unsigned SeatColIndex;  //0=A, 1=B, ...
int ParsedValues;
printf("ntttEnter seat number for passenger %d: (EX: 1A)", PassengerNumber);
ParsedValues=scanf("%d%c", &p.seatrow, &p.seatcol);
SeatColIndex = toupper(p.seatcol)-'A'; //no matter if seat 1a or 1A, just get it 0-based
SeatRowIndex = p.seatrow - 1; //get it 0-based
if (ParsedValues==2 && SeatRowIndex<ROWS && SeatColIndex<COLS)
{
/***legal seat given.***/
if (seat[SeatRowIndex][SeatColIndex])
{
printf("ntt Seat %d%c already allocated by passenger %d. Choose another seat? (Y/N)", p.seatrow, p.seatcol, seat[SeatRowIndex][SeatColIndex]);
for(;;) //until valid answer given
{
switch(toupper(getchar()))
{
case 'Y':
break;
case 'N':
printf("Passenger %d will have no seat.", PassengerNumber);
return;
default: //invalid answer
continue;
}
break;
}
continue;
}
seat[SeatRowIndex][SeatColIndex] = PassengerNumber; //now occupied
return; //done.
}
printf("Seat %d%c not exist!", p.seatrow, p.seatcol);
}
}
int main(int /*argc*/, char ** /*argv*/)
{
int i;
for (i=1; i<5; i++)
chooseSeat(i);
int col;
printf("nn SEAT MAPn   ");
for(col=0; col<COLS; col++) //print ruler
putchar(col+'A');
for (i=0; i<ROWS; i++)
{
printf ("n%2d ", i+1);
for(col=0; col<COLS; col++)
putchar(seat[i][col] ? 'X' : '.');
}
puts("nDone.");
return 0;
}

相关内容

  • 没有找到相关文章

最新更新