pouvez-vous m'aider? J'ai un problème avec char* station;
quand je comble mes lacunes, tout va bien, mais quand je suis avec printf("%d)Input its stations: ",i+1);
. C'est un problème, je veux dire: j'entre chech-joch-chor-dsh-dsh
mais je dois entrer chech joch chor dsh dsh
(ce sont des noms de stations, c'est un exemple) .So il imprime SEULEMENT LE PREMIER MOT, je ne sais pas pourquoi .. vérifier ceci s'il vous plaît ... (Je comprends que j'ai besoin de libérer ce que j'ai pris). S'il vous plaît, expliquer pourquoi il en est ainsi, pourquoi le premier .. me donner un indice ..Struct et un tableau de symboles
#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;
}
mais quand je l'utilise gets()
est:
'% s' de' scanf' se lit comme un séparateur d'espace. Donc, il n'inclut pas d'espaces dans le contenu. – BLUEPIXY
@BLUEPIXY comment devrait-il résoudre ce problème? –
Voir aussi [Pourquoi la fonction gets est-elle si dangereuse qu'elle ne devrait pas être utilisée?] (Http://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it- should-not-be-used) – e0k