For 循环 / printf / 3d 数组
/*
Program to calculate trip and plan flights
*/
#define TRIP 6
#define DEST 1
#define NUMLEG 10
#include <stdio.h>
int error_leg(int leg_num);
char error_leg_type(char leg_type);
int main(void)
{
int i, trip_num, row, col, leg_num, checkE, z, layer,
travel_leg[TRIP][NUMLEG];
char leg_type, travel_leg_type[TRIP][NUMLEG][DEST], checkF;
printf("Please enter the number of trips:");
scanf("%d", &trip_num);
while (trip_num > TRIP)
{
printf("Invalid number of trip. (Min of 3 trips and Max 6 trips).\n"); /*input number of trips*/
printf("Please enter the number of trips:");
scanf("%d", &trip_num);
if (trip_num < TRIP)
printf("Valid trip number. Please proceed to enter destination code.\n");
}
for (i=0; i < trip_num ; i++)
{
printf("Please enter the number of legs in your trip:");
scanf("%d", &leg_num);
checkE = error_leg(leg_num);
if (checkE == 2)
travel_leg[i][0]=leg_num;
else
while (checkE == 1)
{
printf("Please enter the number of legs in your trip:");
scanf("%d", &leg_num);
checkE = error_leg(leg_num);
if (checkE = 2)
{
travel_leg[i][0] = leg_num;
}
}
for (z = 0; z < leg_num ; z++)
{
printf("A Airplane \nR Train and rail travel\nB Bus \nC Car\nF Ferry \nS Cruise ship \nM Motor\
cycle \nY Bicycle\nT Boat other than a ferry or cruise ship\nD Dirigible\nO Other\n");
printf("Please enter leg type according to list above:");
getchar();
scanf("%c", &leg_type);
checkF = error_leg_type(leg_type);
if (checkF == 2)
{
travel_leg_type[i][z][0] = leg_type;
printf("%c\n", travel_leg_type[i][z][0]);
}
else
{
while ( checkF == 1)
{
printf("Please enter leg type according to list:");
getchar();
scanf("%c", &leg_type);
checkF = error_leg_type(leg_type);
if (checkF == 2)
{
travel_leg_type[i][z][0] = leg_type;
printf("%c\n", travel_leg_type[i][z][0]);
}
}
}
}
}
/*print for trip plans*/
for (layer = 0; layer < trip_num; layer++)
{
for (row = 0; row < DEST; row++)
{
for( col = 0; col < leg_num; col++);
printf("Leg_type:%c \n", travel_leg_type[layer][row][col]);
}
}
return 0;
}
int error_leg(int leg_num)
{
int checkE;
if (leg_num <= NUMLEG)
{
checkE=2;
return checkE;
}
else
{
printf("%d Invalid number of legs(min 1 and max of 10 legs).\n", leg_num);
checkE=1;
return checkE;
}
}
char error_leg_type(char leg_type)
{
char A, R, B, C, F, S, M, Y, T, D, O;
int checkF;
checkF;
if ( (leg_type == 'A') || (leg_type == 'R') || (leg_type == 'B') ||(leg_type == 'C') ||(leg_type == 'F') ||(leg_type == 'S') || (leg_type == 'M') ||(leg_type == 'Y') || (leg_type == 'T')|| (leg_type == 'D') || (leg_type == 'O') )
{
checkF = 2;
return checkF;
}
else
{
printf("%c Invalid leg type. Please Select from above.\n", leg_type);
checkF = 1;
return checkF;
}
}
由于某种原因,我认为我的leg_num 失去了它的价值,并且没有进入打印循环。我确信我将值分配给了数组,因为我在分配它后打印它以确保。奇怪的是,如果我输入 0,它会打印数组中的第一个值,但不会打印超出 1 的任何值。不确定我是否犯了错误,或者只是我没有看到的东西。 (请原谅凌乱)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试删除
for( col = 0; col 上的
;
去过那里,做到了:-)
Try removing the
;
on yourfor( col = 0; col < leg_num; col++);
Been there, done that :-)