Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4000366
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:46:22+00:00 2026-05-20T07:46:22+00:00

/* Program to calculate trip and plan flights */ #define TRIP 6 #define NUMLEG

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

(I ask this question a while back but my code was too messy so I re-wrote it in functions so it was easier to read)

The problem I’m having is that my leg_num is getting over written every time I step out of the loop, so when I try to print out in the printf the last leg_num I put in is the number that is being use at:

for (n = 0; n < leg_num ; n++) the 2nd one in the printing loop

EDITED

So when I put in 2 trips trip# 1 has 3 legs trip# 2 has 2 legs when it runs through the printing loops it will only print 2 legs for each trip.

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 

Everything else works fine because I put printf statements along the way to check if that was the issue but it wasn’t. I was thinking of saving the leg_num into a array and using that but not sure how to do it, plus the fact that this is part of a homework and our professor is restricting almost everything but the basic loops simple arrays.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T07:46:22+00:00Added an answer on May 20, 2026 at 7:46 am
      printf("Trip#:%d Num_leg:%d ", index+1, travel_leg[index]);
      for (n = 0;  n < leg_num ; n++)
    

    Change to

      printf("Trip#:%d Num_leg:%d ", index+1, travel_leg[index]);
      for (n = 0;  n < travel_leg[index] ; n++)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My Program overrides public void paint(Graphics g, int x, int y); in order to
I have a program that will calculate the minimal area taken by fitting rectangles
As a Christmas gift I have written a small program in Java to calculate
This program will calculate the average grade for 4 exams using a for loop
I'm writing a program to calculate a value that is a measure of the
I am writing a program to calculate the shortest path between two nodes. In
I have a small C program to calculate hashes (for hash tables). The code
I am making a Java program to calculate Simpson's rule for integrals. Here is
How can I write a c++ program to calculate large factorials. Example, if I
I'm writing a small program to calculate a physics problem but I'm having problems

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.