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 3997942
In Process

The Archive Base Latest Questions

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

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

  • 0
/*                                                                           
  Program to calculate trip and plan flights                                 
*/
#define TRIP 6
#define NAMEMAX 40
#define DEST 1

#include <stdio.h>
#include <string.h>


int main(void)
{
  int i, trip_num, row, col;
  char travel_name[TRIP][DEST],
    dest_in[1];


  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 name of destination(Note: use _ to for spaces in \
name):\n");
      scanf("%s", &dest_in);
      if (strlen(dest_in) < NAMEMAX)
        strcpy(travel_name[i],dest_in);
   /*   else (strlen(dest_in) > NAMEMAX) */



  for (row = 0; row < trip_num; row++)
    {
      for (col=0; col < DEST; col++)
        printf("Trip#:%d travel_name:%s \n", row+1, travel_name[row][col]);
    }

  return 0;
}

I’m trying to get the user to put in a name as string and store it if the name is 40 character or less but its giving me a segmentation fault

  • 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:27:50+00:00Added an answer on May 20, 2026 at 7:27 am

    There are lots of problems with your code.

    1. scanf("%s", &dest_in) will read a string into dest_in which could potentially overflow your buffer because there is no size specifier. Consider changing it to scanf("%99s", dest_in) so you read 99 characters maximum, + 1 for the null terminator which is 100 (your array size). Also, there is no need to use to & operator here.

    2. travel_name[i][0]=dest_in[100]; You are accessing a character that is outside the bounds of dest_in. The only index you should access is 0.

    3. printf("Trip#:%d travel_name:%s \n", row+1, travel_name[row][col]); Your code says that you want to print a string. printf goes looking for a pointer to a character array but travel_name[row][col] is a single character.

    4. while (trip_num > TRIP). You tell the user to enter a number between 3 and 6 but you don’t check if the input is less than 3.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

/* Program to calculate trip and plan flights */ #define TRIP 6 #define NUMLEG
My Program overrides public void paint(Graphics g, int x, int y); in order to
i have following program to calculate size of file #include <iostream> #include <fstream> #include
This is my code : #include <iostream> using namespace std; int main (int argc,
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

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.