For 循环 / printf / 3d 数组

发布于 2024-10-19 07:22:18 字数 3468 浏览 2 评论 0 原文

/*
  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 的任何值。不确定我是否犯了错误,或者只是我没有看到的东西。 (请原谅凌乱)

/*
  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;
    }
}

For some reason along the way down I think my leg_num loses its value and it didn't make it to the print loop. I'm sure I assign the value to the array because I print it after I assign it to make sure. Strange thing is that it will print the first value in the array if I put 0 but it wont print any beyond 1. Not sure if I made a error or its just something I'm not seeing. (excuse the messyness)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

善良天后 2024-10-26 07:22:18

尝试删除 for( col = 0; col 上的 ;

去过那里,做到了:-)

Try removing the ; on your for( col = 0; col < leg_num; col++);

Been there, done that :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文