#include <stdio.h>
#include <string.h>
char first_class();
void business_class();
int main()
{
int row,class = 0;
char X;
char seating[13][6] ={
{X, X, '*', '*', '*', '*'},{'*','*',X,X,'*','*'},{X,X,X,'*','*',X},{'*','*','*',X,X,'*'},{X,X,X,X,X,X},{'*','*',X,X,'*','*'},{'*',X,'*','*',X,X},{'*','*','*','*','*','*'},{X,'*','*','*',X,'*'},{X,X,X,'*','*','*'},{X,X,X,X,X,X},{'*','*','*','*','*','*'},{'*',X,X,X,'*','*'} };
int i,j;
int a;
char c;
char seat;
printf("*********************************************************\n");
printf("**** Thank you for choosing Ricardio's Airline. ****\n");
printf("** Best seats available for your flight, guaranteed, **\n");
printf("**** Refund is not available ****\n");
printf("*********************************************************\n");
printf("We have the best seats in da haus yo!\n");
printf("Please select a Class\n");
printf("1 = First Class, 2 = Business Class, 3 = Economy Class\n:");
scanf("%d", &class);
for(c='A'; c<='G'; ++c)
printf("\t %c", c);
printf("\n");
for(i = 0;i < 14; i++)
{
printf("Row %d", i);
for(j = 0; j < 7; j++)
{
printf("\t seating[i][j]");
if (j % 6 == 0)
printf("\n");
}
}
if (class == 1)
first_class();
else if (class == 2)
business_class();
else if (class == 3)
printf ("economy_class()");
else
printf("Invalid Class");
if(strcmp(seat, "1C") == 0)
seating[0][2] = X;
if(strcmp(seat, "1D") == 0)
seating[0][3] = X;
if(strcmp(seat, "1E") == 0)
seating[0][4] = X;
if(strcmp(seat, "1F") == 0)
seating[0][5] = X;
if(strcmp(seat, "2A") == 0)
seating[1][0] = X;
if(strcmp(seat, "2B") == 0)
seating[1][1] = X;
if(strcmp(seat, "2E") == 0)
seating[1][4] = X;
if(strcmp(seat, "2F") == 0)
seating[1][5] = X;
for(c='A'; c<='G'; ++c)
printf("\t %c", c);
printf("\n");
for(i = 0;i < 14; i++)
{
printf("Row %d", i);
for(j = a; j < 7; j++){
printf("\t seating[i][j]");
if (j % 6 == 0)
printf("\n");
}
}
return 0;
}
char first_class()
{
int row = 0;
char column;
char seat;
printf ("*******************************************************\n");
printf ("* First Class *\n");
printf ("*******************************************************\n");
printf ("Rows 1 and 2 are reserved for First Class seating.\n");
printf ("Which seat would you like?\n");
scanf("%s", seat);
if(strcmp(seat, "1A") == 0)
printf("The seat you have chosen is already taken");
if(strcmp(seat, "1B") == 0)
printf("The seat you have chosen is already taken");
if(strcmp(seat, "1C") == 0)
printf("You have chosen seat %d %c", row,column);
if(strcmp(seat, "1D") == 0)
printf("You have chosen seat %d %c", row,column);
if(strcmp(seat, "1E") == 0)
printf("You have chosen seat %d %c", row,column);
if(strcmp(seat, "1F") == 0)
printf("You have chosen seat %d %c", row,column);
if(strcmp(seat, "2A") == 0)
printf("You have chosen seat %d %c", row,column);
if(strcmp(seat, "2B") == 0)
printf("You have chosen seat %d %c", row,column);
if(strcmp(seat, "2C") == 0)
printf("The seat you have chosen is already taken");
if(strcmp(seat, "2D") == 0)
printf("The seat you have chosen is already taken");
if(strcmp(seat, "2E") == 0)
printf("You have chosen seat %d %c", row,column);
if(strcmp(seat, "2F") == 0)
printf("You have chosen seat %d %c", row,column);
else
printf("Invalid Row or That Row Is For The Other Classes");
return seat;
}
void business_class()
{
int column;
int row = 0;
printf ("*******************************************************\n");
printf ("* Business Class *\n");
printf ("*******************************************************\n");
printf ("Rows 3,4,5,7 and 8 are reserved for First Class seating.\n");
printf ("Which row would you like?\n");
scanf("%d", row);
if(row = 3)
printf("Please select your coloumn\n");
scanf("%s",column);
if(column = 'A')
printf("Hi");
if(row = 4)
printf("Please select your coloumn\n");
else
printf("Invalid Row or That Row Is For The Other Classes");
}
答案 0 :(得分:2)
您的脚本中存在一些问题。 Firt,你必须改变第139行
char seat;
以下内容:
char *seat;
因为你像字符串一样处理变量席位,所以char就是一个字符。其他观察结果是您需要使用scanf传递变量的方向,因此,您必须替换以下行:
scanf("%s",column);
scanf("%d",row);
通过
scanf("%d",&column);
scanf("%d",&row);
请注意,在第二步中,您必须将变量的类型更改为'%d'(整数)。另一方面,你有一个函数first_class返回一个char,但是,你想要返回一个字符串的变量席位,因此,你必须通过
替换函数定义char* first_class()
另外,要比较变量,必须使用operator ==,请参阅函数business_class。你的最终代码必须是:
#include <stdio.h>
#include <string.h>
char *first_class();
void business_class();
int main()
{
int row,class = 0;
char X;
char seating[13][6] ={
{X, X, '*', '*', '*', '*'},{'*','*',X,X,'*','*'},{X,X,X,'*','*',X},{'*','*','*',X,X,'*'},{X,X,X,X,X,X},{'*','*',X,X,'*','*'},{'*',X,'*','*',X,X},{'*','*','*','*','*','*'},{X,'*','*','*',X,'*'},{X,X,X,'*','*','*'},{X,X,X,X,X,X},{'*','*','*','*','*','*'},{'*',X,X,X,'*','*'} };
int i,j;
int a;
char c;
char *seat;
seat = (char *) malloc(15);
printf("*********************************************************\n");
printf("**** Thank you for choosing Ricardio's Airline. ****\n");
printf("** Best seats available for your flight, guaranteed, **\n");
printf("**** Refund is not available ****\n");
printf("*********************************************************\n");
printf("We have the best seats in da haus yo!\n");
printf("Please select a Class\n");
printf("1 = First Class, 2 = Business Class, 3 = Economy Class\n:");
scanf("%d", &class);
for(c='A'; c<='G'; ++c)
printf("\t %c", c);
printf("\n");
for(i = 0;i < 14; i++)
{
printf("Row %d", i);
for(j = 0; j < 7; j++)
{
printf("\t seating[i][j]");
if (j % 6 == 0)
printf("\n");
}
}
if (class == 1)
first_class();
else if (class == 2)
business_class();
else if (class == 3)
printf ("economy_class()");
else
printf("Invalid Class");
if(strcmp(seat, "1C") == 0)
seating[0][2] = X;
if(strcmp(seat, "1D") == 0)
seating[0][3] = X;
if(strcmp(seat, "1E") == 0)
seating[0][4] = X;
if(strcmp(seat, "1F") == 0)
seating[0][5] = X;
if(strcmp(seat, "2A") == 0)
seating[1][0] = X;
if(strcmp(seat, "2B") == 0)
seating[1][1] = X;
if(strcmp(seat, "2E") == 0)
seating[1][4] = X;
if(strcmp(seat, "2F") == 0)
seating[1][5] = X;
for(c='A'; c<='G'; ++c)
printf("\t %c", c);
printf("\n");
for(i = 0;i < 14; i++)
{
printf("Row %d", i);
for(j = a; j < 7; j++){
printf("\t seating[i][j]");
if (j % 6 == 0)
printf("\n");
}
}
return 0;
}
char* first_class()
{
int row = 0;
char column;
char *seat;
printf ("*******************************************************\n");
printf ("* First Class *\n");
printf ("*******************************************************\n");
printf ("Rows 1 and 2 are reserved for First Class seating.\n");
printf ("Which seat would you like?\n");
scanf("%s", seat);
if(strcmp(seat, "1A") == 0)
printf("The seat you have chosen is already taken");
if(strcmp(seat, "1B") == 0)
printf("The seat you have chosen is already taken");
if(strcmp(seat, "1C") == 0)
printf("You have chosen seat %d %c", row,column);
if(strcmp(seat, "1D") == 0)
printf("You have chosen seat %d %c", row,column);
if(strcmp(seat, "1E") == 0)
printf("You have chosen seat %d %c", row,column);
if(strcmp(seat, "1F") == 0)
printf("You have chosen seat %d %c", row,column);
if(strcmp(seat, "2A") == 0)
printf("You have chosen seat %d %c", row,column);
if(strcmp(seat, "2B") == 0)
printf("You have chosen seat %d %c", row,column);
if(strcmp(seat, "2C") == 0)
printf("The seat you have chosen is already taken");
if(strcmp(seat, "2D") == 0)
printf("The seat you have chosen is already taken");
if(strcmp(seat, "2E") == 0)
printf("You have chosen seat %d %c", row,column);
if(strcmp(seat, "2F") == 0)
printf("You have chosen seat %d %c", row,column);
else
printf("Invalid Row or That Row Is For The Other Classes");
return seat;
}
void business_class()
{
int column;
int row = 0;
printf ("*******************************************************\n");
printf ("* Business Class *\n");
printf ("*******************************************************\n");
printf ("Rows 3,4,5,7 and 8 are reserved for First Class seating.\n");
printf ("Which row would you like?\n");
scanf("%d", &row);
if(row == 3)
printf("Please select your coloumn\n");
scanf("%d",&column);
if(column == 'A')
printf("Hi");
if(row == 4)
printf("Please select your coloumn\n");
else
printf("Invalid Row or That Row Is For The Other Classes");
}
答案 1 :(得分:0)
关于这种界限:
if(strcmp(seat, "1C") == 0)
seat is a single character and
“1C”`是一个字符串(包括尾随的NUL字符)是3个字节。
因此单个字符不能与字符串进行比较。
另外,这种行:
scanf("%s", seat);
表示将字符串读入单个char变量seat
。
如果用户输入超出<newline>
的任何内容,缓冲区将会溢出!
(scanf()在读取字符串时总是附加NUL字符)
缓冲区溢出是未定义的行为,可能/将导致seg故障事件。
建议将变量seat
声明为
char seat[5];
并修改对scanf()
的调用以将最大字符数输入限制为小于座位长度[1]的1。
scanf( '%4s", seat);
请通过附加更正代码的编辑更正代码中的所有此类问题。