【问题标题】:For loop / over written variableFor循环/覆盖变量
【发布时间】:2011-02-27 21:05:57
【问题描述】:
/*
  Program to calculate trip and plan flights
*/
#define TRIP 6
#define NUMLEG 10 
#include <stdio.h>
#include <string.h>

int input_trip(void);
int input_leg(int travel_leg[NUMLEG], int index);
char input_travel_type(char leg_type[TRIP][NUMLEG], int n, int index, int leg_num);
int main(void)
{ 
  int  row, col, trip_num, index, travel_leg[NUMLEG], leg_num, n; 
  char leg_type[TRIP][NUMLEG];
  trip_num = input_trip();
  for (index =0; index < trip_num; index++)
    { 
      leg_num = input_leg(travel_leg,index);

      printf("At Trip Number:%d\n", index);
      printf("Number of legs %d\n", leg_num );

      printf("A Airplane\n");
      printf("R Train and rail travel\n");
      printf("B Bus\n");
      printf("C Car\n");
      printf("F Ferry\n");
      printf("S Cruise ship\n");
      printf("M Motorcycle\n");
      printf("Y Bicycle\n");
      printf("T Boat other than a ferry or cruise ship\n");
      printf("D Dirigible\n");
      printf("O Other\n");
      printf("NOTE!!:Please using capital letters (case sensitive).\n");

      for (n = 0; n < leg_num; n ++)
    {
      printf("At leg Number%d\n", n);
      input_travel_type(leg_type, n, index, leg_num);
    }
    }

  for (index = 0; index < trip_num; index++)
    {
      printf("Trip#:%d Num_leg:%d ", index+1, travel_leg[index]);
      for (n = 0;  n < leg_num ; n++)
    printf("Leg_type(#%d):%c ",n+1, leg_type[index][n]);
      printf("\n");

    }

  return 0;
}

int input_trip(void)
{
  int trip_num;

  printf("Please enter the number of trips:");
  scanf("%d", &trip_num);
  // if( (trip_num <= TRIP) && (trip_num >= 3))
  if( (trip_num <= TRIP) && (trip_num >=1) ) 
    {
      return trip_num;
    }


  else 
    {
      while ((trip_num < 1) || ( 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) && (trip_num >= 1))
        {
          return trip_num;
        }
    }

    } 
}
int input_leg(int travel_leg[NUMLEG], int index)
{
  int leg_num, i;
  char travel_type, checkA, A, R, B, C, F, S, M, Y, T, D, O;

  printf("Please enter the number of legs in your trip:");
  scanf("%d", &leg_num);
  if ( (leg_num <= NUMLEG) && (leg_num > 0) )
    {    
      travel_leg[index]=leg_num;
      return leg_num;
    }
  else 
    {
      while ( (leg_num < 0) || (leg_num > NUMLEG))
    {
      printf("Invalid number of legs(min 1 and max 10 legs).\n");
      printf("Please enter the number of legs in your trip:");
      scanf("%d", &leg_num);
      if ( (leg_num <= NUMLEG) && (leg_num > 0) )
        {
          travel_leg[index]=leg_num;
          return leg_num;
        }
    }
    }
}

char input_travel_type(char leg_type[TRIP][NUMLEG], int n, int index, int leg_num)
{
  char travel_type, checkA;
  printf("Please enter the leg type for leg#%d:", n+1);
  scanf("%c", &travel_type);
  checkA = ( (travel_type == 'A') || (travel_type == 'R') || (travel_type == 'B') || 
         (travel_type == 'C') || (travel_type == 'F') || (travel_type == 'S') ||
         (travel_type == 'M') || (travel_type == 'Y') || (travel_type == 'T') ||
         (travel_type == 'D') || (travel_type == '0') );

  if (checkA == 1)
    {
      leg_type[index][n]=travel_type;
    }
  else 
    {
      while (checkA != 1)
    {
      printf("Please enter the leg type for leg#%d:", n+1);
      scanf("%c", &travel_type);
      checkA = ( (travel_type == 'A') || (travel_type == 'R') || (travel_type == 'B') || 
             (travel_type == 'C') || (travel_type == 'F') || (travel_type == 'S') ||
             (travel_type == 'M') || (travel_type == 'Y') || (travel_type == 'T') ||
             (travel_type == 'D') || (travel_type == '0') );

      if (checkA == 1)
        leg_type[index][n]=travel_type;
    }
    }
}

(我不久前问过这个问题,但我的代码太乱了,所以我在函数中重写了它,这样更容易阅读)

我遇到的问题是每次我跳出循环时我的 leg_num 都会被覆盖,所以当我尝试在 printf 中打印出最后一个 leg_num 时,我输入的最后一个 leg_num 是正在使用的数字:

for (n = 0; n

已编辑

所以当我输入 2 个行程时,行程 #1 有 3 条腿行程#2 有 2 条腿,当它通过打印循环时,每次行程只会打印 2 条腿。

Trip#:1 Num_leg:3 Leg_type(#1):C Leg_type(#2):B 
Trip#:2 Num_leg:2 Leg_type(#1):A Leg_type(#2):R 

Trip#:1 Num_leg:1 Leg_type(#1):S Leg_type(#2): 
Trip#:2 Num_leg:2 Leg_type(#1):F Leg_type(#2):S 

其他一切都很好,因为我在路上放了 printf 语句来检查这是否是问题,但事实并非如此。我正在考虑将 leg_num 保存到一个数组中并使用它,但不知道该怎么做,再加上这是家庭作业的一部分,我们的教授几乎限制了除了基本循环简单数组之外的所有内容。

【问题讨论】:

  • 哪个为 (n = 0; n
  • 对不起,我不明白你的意思。 “当我尝试打印 [...] 最后一条 leg_num”。 leg_num 似乎在 input_leg 函数中被读取,然后保持不变?
  • 哦,我编辑并在其下方放了一个示例
  • 你的代码很难理解。首先,如果你声明一个函数会返回一些东西,那么它应该返回一些东西。在几乎所有的函数中,你都在 if 语句中返回值。 .so 可能存在您的函数根本不返回任何内容的情况
  • 你还有这 char [...] A, R, B, C, F, S, M, Y, T, D, O; 一堆你从不使用的无意义变量。

标签: c arrays for-loop


【解决方案1】:
  printf("Trip#:%d Num_leg:%d ", index+1, travel_leg[index]);
  for (n = 0;  n < leg_num ; n++)

改成

  printf("Trip#:%d Num_leg:%d ", index+1, travel_leg[index]);
  for (n = 0;  n < travel_leg[index] ; n++)

【讨论】:

  • 啊,所以我只需要使用数组 ._. 而不是使用另一个变量
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-03
  • 2016-07-17
  • 2019-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多