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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:00:27+00:00 2026-06-17T11:00:27+00:00

Whe I run it fgets() is simply ignored and scanf() doesn’t input data to

  • 0

Whe I run it fgets() is simply ignored and scanf() doesn’t input data to structure.

My code:

#include"stdio.h"
#include"stdlib.h"

typedef struct {
  char nazv[50];
  int numr;
  int date[3];
  int time[2];
} train;

void input(train*rasp,int n);
void print(train*rasp,int n);
void output(train*rasp, int n, int*date, int*time);

main() {
  int n;
  int t[2];
  int d[3];
  printf("How much? ");
  scanf("%d",&n);
  train* rasp=(train*)malloc(n*sizeof(train));
  input(rasp,n);
  printf("Enter date: ");
  for (int date=0;date<3;date++) {
    scanf("%d",&d[date]);
  }
  printf("Enter time: ");
  for (int time=0;time<2;time++) {
    scanf("%d",&t[time]);
  }
  for (int i=0;i<n;i++)
    print(rasp,n);
  output(rasp,n,d,t);
  free(rasp);
  return 0;
}
void input(train*rasp,int n) {
  for (int i=0;i<n;i++) {
    printf("Train #%d\n",i+1);
    printf("Enter train's name: ");
    fgets(rasp[i].nazv,50,stdin);
    printf("Enter train's number: ");
    scanf("%d",&rasp[i].numr);
    printf("Enter date: ");
    for (int d=0;d<3;d++) {
      scanf("%d",&rasp[i].date[d]);
    }
    printf("Enter time: ");
    for (int t=0;t<2;t++) {
      scanf("%d",&rasp[i].time[t]);
    }
  }
  printf("\nOK.");
}
void print(train*rasp,int n) {
  printf("Name: ");
  printf("%s",rasp[n].nazv);
  printf("\nNumber: %d",rasp[n].numr);
  printf("\nDate: %d.%d.%d", rasp[n].date[0],rasp[n].date[1],rasp[n].date[2]);
  printf("\nTime: %d:%d\n", rasp[n].time[0],rasp[n].time);
}
void output(train*rasp,int n,int*date,int*time) {
  for (int i=0;i<n;i++) {
    if (rasp[n].date[2]<date[2])
      continue;
    if (rasp[n].date[2]>date[2]) {
      print(rasp,n);
    } else {
      if (rasp[n].date[1]<date[1])
    continue;
      if (rasp[n].date[1]>date[1]) {
    print(rasp,n);
      } else {
    if (rasp[n].date[0]<date[0])
      continue;
    if (rasp[n].date[0]>date[0]) {
      print(rasp,n);
    } else {
      if (rasp[n].time[0]<time[0])
        continue;
      if (rasp[n].time[0]>time[0]) {
        print(rasp,n);
      } else {
        if (rasp[n].time[1]<=time[1])
          continue;
        print(rasp,n);
      }
    }
      }
    }
  }
}

input() function should put trains into to database.
output() function should only print trains from database with a date later than the one user supplied before.

  • 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-17T11:00:29+00:00Added an answer on June 17, 2026 at 11:00 am
    scanf("%d",&n);
    

    scanf leaves the newline that sent the input to the programme in the input buffer.

    train* rasp=(train*)malloc(n*sizeof(train));
    input(rasp,n);
    
    void input(train*rasp,int n) {
      for (int i=0;i<n;i++) {
        printf("Train #%d\n",i+1);
        printf("Enter train's name: ");
        fgets(rasp[i].nazv,50,stdin);
    

    fgets finds a newline as the first character in the input buffer and doesn’t look further, leaving the train’s name in the input buffer.

        printf("Enter train's number: ");
        scanf("%d",&rasp[i].numr);
    

    Which scanf fails to convert into an int.

    Mixing scanf and fgets for user input is error-prone becaus of the different newline-behaviour. Unless you clear the input buffer between a scanf and a following fgets, the fgets most often will only get the newline left over form the scanf (there are a few scanf formats that consume the newline).

    You can clear the buffer

    void clear_input_buffer(void) {
        int c;
        while((c = getchar()) != EOF && c != '\n');
        if (c == EOF) {
            // input stream broken, what now?
            exit(EXIT_FAILURE);
        }
    }
    

    or use fgets for all input and use sscanf (and strtol and friends) to parse the input.

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

Sidebar

Related Questions

Whe I try to run my site locally I get this error: Description: The
this is my code. which works fine for 10 records. but whe it will
I have a shopping cart in my app and an underlying data structure that
App reads JSON data. Then it would put it to whe listview (correctly) but
Sorry for my poor math/music knowledge.. Assumed whe want to play a video file
I never understood this: in web development whe[ n|re ] does it make sense
Edit: Solved, the error dissapeared whe I updated the package. I'm getting an error
Is there any way that i can cause mysql to be case sensitive whe
I have some code that looks like the following. First I have some domain
Whe have built a packager who diff the last due tag in svn and

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.