0

I defined my list like this:

struct student {

char vname[30];    // Vorname
char nname[30];   // Nachname
int mnr;        // Matrikelnummer
char adresse[50]; // Adresse
int pfkur;      // Anzahl der bereits belegten Pflichtkurse
struct student *next; //Zeiger auf Nachfolger
struct student *prev; // Zeiger auf Vorgänger

};
typedef struct student student; 
typedef struct student *pstudent;

Now I'm having trouble with scanning strings with blank spaces such as "van Helsing"

My implementation for scanning in the things into the list is this:

pstudent allocate(void){ 
  pstudent elp;
  elp = (pstudent)malloc(sizeof(*elp));
  if (elp == NULL){printf("ERROR: malloc"); exit(1);}
  elp -> next = NULL;
  return elp;
} /* allocate */

    elp = allocate();
    printf("\n Vorname:\t\t "); scanf("%s", elp -> vname); // Vorname
    printf(" Nachname:\t\t "); scanf("%s", elp -> nname); //Nachname
    printf(" Matrikelnummer:\t\t "); scanf("%d",&(elp -> mnr)); 
    printf(" Adresse:\t\t "); scanf("%s", elp -> adresse); // Adresse
    printf(" Pflichtkurse:\t\t "); scanf("%d", &(elp -> pfkur)); // 
    elp -> next = studentlist;
    studentlist = elp;

These are just code snippets out of a bigger project so if you need any context feel free to ask but I think the sufficient informations are provided. I also know that there are missing things for example the complete connection of the list and so on.

I'm looking for a way to scan strings into vname, nname and adresse with blank spaces in them. Right now the whole programm gets very buggy when I'm trying to do that.

Thank you in advance.

Mathmeeeeen
  • 119
  • 1
  • 11
  • 2
    I would try fgets(), which reads until new line or max capacity as set – H.cohen Jan 07 '19 at 16:24
  • I guess that would work but what would the syntax look like then? Because for scanf() im using the syntax elp -> vname for example – Mathmeeeeen Jan 07 '19 at 16:48
  • fgets() declaration :char *fgets(char *str, int n, FILE *stream). So:* fgets(elp -> vname, 30, stdin)* check out fgets() man page for more – H.cohen Jan 07 '19 at 17:55

2 Answers2

0

Try to use scanf("%[^\n]s", elp -> vname); It will read string upto newline character.

Akash Gupta
  • 23
  • 1
  • 5
  • Doesn't work either it just skips the scans with this syntax scanf("%[^\n]s", elp -> vname); – Mathmeeeeen Jan 07 '19 at 17:32
  • scanf("%[^\n]s", elp->vname); use this syntax for taking input . and for print - printf("%s\n",elp->vname); I have just run this code on ide and it worked for me . – Akash Gupta Jan 07 '19 at 18:37
-2

You can use gets instead, like this: gets(elp -> vname);

Yakov Dan
  • 2,157
  • 15
  • 29