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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:39:32+00:00 2026-05-20T23:39:32+00:00

I’m trying to scan in a single line input and storing it in a

  • 0

I’m trying to scan in a single line input and storing it in a struct. I’m not sure if I am storing it wrong or I am printing it wrong. I’m not sure on how to use scanf with for loops since I never done it before not to mention C likes to overwrite stuff. So I wasn’t sure on how to approach this.

This is something I put together but when I print I get junk numbers. I was intending to maybe use pointers but my professor won’t let us use it. Which is why I’m having trouble.

EDITED

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

#define MAX 3
#define MAXTRIP 6


struct stop
{
  float cost;
  float time; 
};

struct trip
{
  char Dest_letter;
  int stop_num;
  struct stop leg[MAX];
};

int main(void)
{
  int trip_num, index, i;
  struct trip travel[MAXTRIP];

  printf("Enter number of trips: ");
  scanf("%d", &trip_num);
  printf("Please enter destination letter/number of stops and cost/length of each stop:\n");
  for (index = 0; index < trip_num; index++)
    {
      scanf("%c %d", &travel[index].Dest_letter, &travel[index].stop_num);
      for ( i=0; i < travel[index].stop_num; i++)
    scanf("%f %f", &travel[index].leg[i].cost, &travel[index].leg[i].time);
    }
  for (index =0; index < trip_num; index++)
    {
      printf("Trip:%d \nDestination Letter:%c", index+1, travel[index].Dest_letter);
      for (i=0; i < travel[index].stop_num; i++)
    printf("Cost:%.2f \nLength:%.2f", travel[index].leg[i].cost, travel[index].leg[i].time);
    }
}
  • 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-20T23:39:33+00:00Added an answer on May 20, 2026 at 11:39 pm

    You have your printing loop inside your reading loop. It is trying to print out all of the trip information after reading the first one in.

    Edit: The trouble here is that the way scanf handles single characters is kinda unintuitive next to the way it handles strings and numbers. It reads the very next character from standard in, which is probably a newline character from when you finished typing the previous input. Then it proceeds to try and read an integer, but instead it finds the letter you had intended to be consumed by the %c. This causes scanf to fail and not initialize stop_num.

    One way around this may be to read in a string instead. scanf will start reading the string at the first non-whitespace character and stop reading it at the first whitespace character. Then just take the first character from the buffer you read the string into.

    #include <stdio.h>
    
    #define MAX 3
    #define MAXTRIP 6
    
    struct stop {
        float cost;
        float time;
    };
    
    struct trip {
        char Dest_letter;
        int stop_num;
        struct stop leg[MAX];
    };
    
    int main(void)
    {
        int trip_num, index, i;
        struct trip travel[MAXTRIP];
        char buffer[10];
    
        printf("Enter number of trips: ");
        scanf("%d", &trip_num);
        for (index = 0; index < trip_num; index++) {
            printf("Please enter destination letter/number of stops:\n");
            scanf("%s %d", buffer, &travel[index].stop_num);
            travel[index].Dest_letter = buffer[0];
            for (i = 0; i < travel[index].stop_num; i++){
                printf("Please enter cost/length of stop %d:\n", i);
                scanf("%f %f", &travel[index].leg[i].cost, &travel[index].leg[i].time);
            }
        }
        printf("%d trips\n", trip_num);
        for (index = 0; index < trip_num; index++) {
            printf("Trip:%d \nDestination Letter:%c\n", index + 1, travel[index].Dest_letter);
            for (i = 0; i < travel[index].stop_num; i++)
                printf("Cost:%.2f \nLength:%.2f\n", travel[index].leg[i].cost, travel[index].leg[i].time);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I need to clean up various Word 'smart' characters in user input, including but
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.