在将字符添加到链表结构时遇到问题?

时间:2015-12-05 12:48:38

标签: pointers linked-list structure

我目前未能成功为当前程序添加char。我可以添加一个数字,但它会发布提示添加一个字符但跳过输入部分并直接询问我是否要继续。

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

main()
{
    struct node
    {
        int num;
        char gender[3];
        struct node *ptr;
    };

    typedef struct node NODE;
    NODE *head, *first, *temp = 0;
    int count = 0;
    int choice = 1;
    first = 0;

    while (choice)
    {
        head  = (NODE *)malloc(sizeof(NODE));
        printf("Enter the Student Identification Number\n");

        scanf("%d", &head-> num);
        printf("Enter the Student gender (M)or(F)\n");
        scanf("%c", &head-> gender);
        if (first != 0)
        {
            temp->ptr = head;
            temp = head;
        }else
        {
            first = temp = head;
        }
        fflush(stdin);
        printf("Would you like to do another entry(Type No(0) or Yes(1))?\n");
        scanf("%d", &choice);
    }
    temp->ptr = 0;
    /*  reset temp to the beginning */
    temp = first;
    printf("\nStudent Information\n");
    while (temp != 0)
    {
        printf("Student ID number: %d\n", temp->num);
        printf("Student gender: %c\n", temp->gender);
        count++;
        temp = temp -> ptr;
    }
    printf("No. of Students inputted into system: %d\n", count);
}

0 个答案:

没有答案