-1

Here is my code. I need to let the user enter more fields, not just val. It's just a code I'm using to test lists, so, for exemple I want the user to add val, name and surname. How can I do it? Some terms are not in english but I think the overall of the code is clear.

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


    struct lista{
    int val;
    struct lista *next;
    }Lista;

    struct lista* crea(struct lista* head);
    void stampa(struct lista* testa);

    int main()
    {
    struct lista *head=NULL;
    int insert=0;

    while(1){
        printf("\n *** MENU ***\n 1.Add in list\n 2.Print\n 3.Exit\n\n Input: ");
        scanf("%d", &insert);
        switch(insert){
            case 1:
                head = crea(head);
                break;
            case 2:
                stampa(head);
                break;
            case 3:
                exit(1);
            default:
                printf("\n Errore: scelta non valida!\n");
                break;
            }
    }


    return 0;
}

struct lista* crea(struct lista* head){
    struct lista *nuovo=NULL; //sarà la nuova head
    int valore=0;
    nuovo = (struct lista*)malloc(sizeof(struct lista));
    printf("\nValue: ");
    scanf("%d", &valore);
    nuovo->val=valore;
    nuovo->next=head;
    head = nuovo;
    return nuovo;
};

void stampa(struct lista* head){
    struct lista* temp=NULL;
    temp = head;

    while(temp != NULL){
        printf("\nvalore: %d\n", temp->val);
        temp = temp->next;

    }
}
ennedes
  • 43
  • 4

1 Answers1

1

If you want to enter more fields, than you have to enter them in your node. Right now, in your node is only element val which is of int type and pointer to the next node. If you want user to enter name or surname, than you have to declare them inside a node. Your struct should look like this:

    struct lista{
    int val;
    char name[20];
    char surname[30];
    struct lista *next;
    }Lista; //if you are not typedefing than you dont need this name because you are just making the global node you will not use

than in your function just ask user for name and surname and add them to your list just like you did for val(take care those are the strings).

Nikola Jokic
  • 103
  • 9
  • But, for exemple in the while, do I need to have a "temp" for each one? – ennedes Jun 18 '16 at 09:50
  • You have to use 1 temp to make a node but that node you make is the same as your struct so when you are trying to fill the node, you can fill them directly for example: scanf("%d", &head -> val); or gets(head -> name); You dont need temporary variables for scanf every time. – Nikola Jokic Jun 18 '16 at 10:37
  • `struct lista* crea(struct lista* head){ struct lista *nuovo=NULL; //sarà la nuova head int valore=0; nuovo = (struct lista*)malloc(sizeof(struct lista)); printf("\nInserisci valore: "); scanf("%d", &head->val); printf("\nInserisci name: "); gets(head->name); printf("\nInserisci surname: "); gets(head->surname); nuovo->val=valore; nuovo->next=head; head = nuovo; return nuovo; };` It crashes after the name... – ennedes Jun 18 '16 at 11:09
  • my bad not head -> val or head -> name, instead of that use temp -> val and temp -> name sorry :) I was in other project – Nikola Jokic Jun 18 '16 at 11:39
  • And use `fflush(stdin)` before gets to clear the stdin buffer just don't forget that – Nikola Jokic Jun 18 '16 at 11:51
  • I had to use nuovo->val and so on, but thanks a lot you make me understand how to do it! :D – ennedes Jun 18 '16 at 12:17
  • Note the limitations on [Using `fflush(stdin)`](http://stackoverflow.com/questions/2979209/using-fflushstdin). – Jonathan Leffler Jun 18 '16 at 23:35
  • @JonathanLeffler oh thanks this was useful for me too. I didn't know for undefined behavior but it worked for me every time. Do you have any suggestion to clear the input buffer beside the fflush(stdin)? – Nikola Jokic Jun 19 '16 at 20:14
  • Often the most reasonable alternative is a function such as: `void gobble_to_newline(FILE *fp) { int c; while ((c = getc(fp)) != EOF && c != '\n') ; }`. That is at least reasonably portable. You can look at various alternatives on POSIX, such as [`tcflush()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/tcflush.html). Or you can read whole lines (POSIX [`getline()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/getline.html) or Standard C [`fgets()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/fgets.html), for example), and then parse the string. – Jonathan Leffler Jun 19 '16 at 20:49
  • I know how to implement a function that clears the stdin buffer, I just wanted to ask you does some of the standard library's functions do that – Nikola Jokic Jun 19 '16 at 21:14