对于loop / printf / 3d数组

时间:2011-02-26 00:18:19

标签: c multidimensional-array printf

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

1 个答案:

答案 0 :(得分:5)

尝试删除;

上的for( col = 0; col < leg_num; col++);

去过那里,做到了: - )