0

can you help me? i have a problem with char* station; when i fill my gaps, everithing is alright, but when i am with printf("%d)Input its stations: ",i+1);. It's a problem, i mean: i enter chech-joch-chor-dsh-dsh but i need to enter chech joch chor dsh dsh(these are names of stations,it's an example).So it prints ONLY THE FIRST WORD, i dont why.. check this out please... (i understand that i need to free what i'have taken). Please, explain why it is so, why the first?.. give me a hint..

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



typedef struct info_bus_{
    int number;
    int begin;
    int end;
    char* stations;
    int time_working;
}info_bus;


int main()
{

    info_bus *b=NULL;
    int i,n;
    char buffer[128];
    printf("How many buses u have: ");
    scanf("%d",&n);

    b=(info_bus *)malloc(n*sizeof(info_bus));

    for(i=0;i<n;i++){
    printf("Input the number of a bus: ");
    scanf("%d",&(b+i)->number);

    printf("%d)Input when it starts to work: ",i+1);
    scanf("%d",&(b+i)->begin);

    printf("%d)Input when it  finishes to work: ",i+1);
    scanf("%d",&(b+i)->end);

        printf("%d)Input its stations: ",i+1);
        scanf("%127s", buffer);
        b[i].stations = (char*) malloc(strlen(buffer) + 1);
        strcpy(b[i].stations, buffer);

    printf("Input time working: ");
    scanf("%d",&(b+i)->time_working);
    }
    for (i=0;i<n;i++){
        printf("\n[%d].the number of a bus: %d",i+1,b->number);
        printf("\n[%d]. Begin at: %d",i+1,b->begin);
        printf("\n[%d]. Finishes at: %d",i+1,b->end);
        printf("\n[%d]. Stations: %s",i+1,b->stations);
        printf("\n[%d]. Time working: %d",i+1,b->time_working);
        printf("\n");
    }

    return 0;
}

but when i use gets() it is: enter image description here

Nikita Gusev
  • 143
  • 10

1 Answers1

0
    scanf("%127s", buffer);

stops reading after encountering a newline. If you want to be able to read multiple words then use fgets():

    fgets(buffer, sizeof buffer, stdin);

Note: fgets() will also read the newline character if there's room. You can remove it if necessary:

    buffer[strcspn(buffer, "\n")] = 0; /* to remove the newline */

Generally, avoid using scanf() even for other inputs. It's error prone. See: Why does everyone say not to use scanf? What should I use instead?

Also, the cast of malloc() is unncessary. See: What's wrong with casting malloc's return value?

P.P
  • 117,907
  • 20
  • 175
  • 238
  • Don't use `gets()`. Use `fgets()` instead. The reason for your problem is `fgets()` will stop reading input after a newline. But the previous `scanf()` leaves a newline. That's why you shouldn't mix `fgets()` and `scanf()`. – P.P Dec 24 '16 at 18:52
  • please, look at my question, i've chaged it. when i use `gets()`. it happens me it – Nikita Gusev Dec 24 '16 at 18:52
  • `"Input when it finishes to work:"` and `"Input its stations:" `at the same line – Nikita Gusev Dec 24 '16 at 18:56
  • You'll have to read out the newline character: just use `getchar()` right before calling `fgets()`. But I'd recommend avoiding `scanf()` altogether. – P.P Dec 24 '16 at 18:57
  • Thank you for helping , used `getchar()` and `scanf(" %127[^\n]%*c", buffer);`,now it works. But i also got that i shouldnt use `scanf()`.. Can you the right code of my program with `fgets()`. FIX ME:) If you can. Thanks, for helping one more time – Nikita Gusev Dec 24 '16 at 19:04