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

  • SEARCH
  • Home
  • 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 8721369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:12:39+00:00 2026-06-13T07:12:39+00:00

my code is the following: it asks the user to input name of planet,

  • 0

my code is the following: it asks the user to input name of planet, distance, and a description. Then it will print out whatever the user entered.

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

/* planet type */
typedef struct{
  char name[128];
  double dist;
  char description[1024];
} planet_t;

/* my planet_node */
typedef struct planet_node {
  char name[128];
  double dist;
  char description[1024];
  struct planet_node *next;
} planet_node;

/* the print function, not sure if its correct */
void print_planet_list(planet_node *ptr){
  while(ptr != NULL){
    printf("%s %lf %s\n", ptr->name, ptr->dist, ptr->description);
    ptr=ptr->next;
  }
  printf("\n");
}

int main(){
  char buf[64];
  planet_node *head = NULL;
  int quit = 0;

  do {
    printf("Enter planet name (q quits): ");
    scanf("%s",buf);
    if((strcmp(buf,"q")==0)){
      quit = 1;
    }
    else{
      planet_node *new = malloc(sizeof(planet_node));       /* New node */
      strcpy((*new).name, buf);         /* Copy name */
      printf("Enter distance and description: ");
      scanf(" %lf ", new->dist); /* Read a new distance into pluto's */
      gets(new->description);      /* Read a new description */
      new->next = head;        /* Link new node to head */
      head=new;        /* Set the head to the new node */
    }
  } while(!quit);

  printf("Final list of planets:\n");
  print_planet_list(head);


  while(head != NULL){
    planet_node *remove = head;
    head = (*head).next;
    free(remove);
  }
}

the places where i made comments are the where i am not sure if it is correct, the code complies but gives me a segmentation error.
any help? thanks.

  • 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-06-13T07:12:40+00:00Added an answer on June 13, 2026 at 7:12 am

    scanf(" %lf ", new->dist);

    This is where your error is.
    It should be:

    scanf(" %lf ", &(new->dist));

    Also an extra ‘ ‘ causes it to accept extra characters. This can be avoided with

    scanf("%lf", &(new->dist));

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

Sidebar

Related Questions

In the following code i want to achieve this: every time a user asks
The following code gives digits whose name is shorter than their value. I can't
The following code: <input style=height: 80px; width: 200px; value=test button> Works fine on Windows,
I have the following *code below, every time I refresh the page it asks
I have the following code: <?php if (!$user) { ?> <div class=fb-login-button data-perms=email,user_birthday,publish_stream>Login with
I have a form that asks the user to enter their Google Analytics code
I have the following code and it does not delete the rows, it asks
I have a program which asks the user to input an integer in the
I'm working on linux + eclipse. I have updated my code following the steps
Following code produces a nested array as a result for keys containing three items:

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.