0

私は次のプログラムを持っています:

#include <stdio.h>
#include <stdlib.h>   /* for malloc */
#include <ctype.h>

struct employee
 {
 char first_name[10];
 char last_name[10];
 long id_number;
 float wage;
 float hours;
 float overtime;
 float gross;

 struct employee *next;
 };

 /*-----------------------------------------------------------------------------*/
 /*                                                                             */
 /* FUNCTION:  print_list                                                       */
 /*                                                                             */
 /* DESCRIPTION:  This function will print the contents of a linked             */
 /*               list.  It will traverse the list from beginning to the        */
 /*               end, printing the contents at each node.                      */
 /*                                                                             */
 /* PARAMETERS:   emp1 - pointer to a linked list                               */
 /*                                                                             */
 /* OUTPUTS:      None                                                          */
 /*                                                                             */
 /* CALLS:        None                                                          */
 /*                                                                             */
 /*-----------------------------------------------------------------------------*/
 void print_list(struct employee *emp1)
 {
    struct employee *tmp;   /* tmp pointer value to current node */
    int i = 0;              /* counts the nodes printed          */

    /* Start a beginning of list and print out each value               */
    /* loop until tmp points to null (remember null is 0 or false)      */
    for(tmp = emp1; tmp ; tmp = tmp->next)
    {
        i++;

        /* TODO - print other members as well */
        printf("\nEmployee ID: %6d, Wage: %8.2f, Hours: %5.2f\n",tmp->id_number,
                                                  tmp->wage,tmp->hours);
    }

    printf("\n\nTotal Number of Employees = %d\n", i);

  }

  /*----------------------------------------------------------------------------*/
  /*                                                                            */
 /* FUNCTION:  main                                                            */
  /*                                                                            */
 /* DESCRIPTION:  This function will prompt the user for an employee           */
 /*               id and wage until the user indicates they are finished.      */
 /*               At that point, a list of id and wages will be                */
 /*               generated.                                                   */
 /*                                                                            */
 /* PARAMETERS:   None                                                         */
 /*                                                                            */
  /* OUTPUTS:      None                                                         */
 /*                                                                            */
 /* CALLS:        print_list                                                   */
 /*                                                                            */
/*----------------------------------------------------------------------------*/
int main ()
{

char   answer[80];       /* to see if the user wants to add more employees */
int    more_data = 1;    /* flag to check if another employee is to be processed */
char   value;             /* gets the first character of answer */

struct employee *current_ptr,   /* pointer to current node */
                *head_ptr;       /* always points to first node */

  /* Set up storage for first node */
head_ptr = (struct employee *) malloc (sizeof(struct employee));
current_ptr = head_ptr;

while (more_data)
{
   /* Read in Employee ID and Hourly Wage */

    printf("\nEnter employee ID: ");
    scanf("%li", & current_ptr -> id_number);

    printf("\nEnter employee weekly wage: ");
    scanf("%f", & current_ptr -> wage);

    printf("\nEnter employee weekly hours: ");
    scanf("%f", & current_ptr -> hours);

    printf("\nEnter First Name: ");
    scanf(" %c", & current_ptr -> first_name);

    printf("\nEnter Last Name: ");
    scanf("%c", & current_ptr -> last_name);


    printf("Would you like to add another employee? (y/n): ");
    scanf("%s", answer);

    /* Ask user if they want to add another employee */
    if ((value = toupper(answer[0])) != 'Y')
       {
       current_ptr->next = (struct employee *) NULL;
       more_data = 0; 
       }
    else
       {
       /* set the next pointer of the current node to point to the new node */
       current_ptr->next = (struct employee *) malloc (sizeof(struct employee));
      /* move the current node pointer to the new node */
       current_ptr = current_ptr->next;
       }
} /* while */

/* print out listing of all employee id's and wages that were entered */
print_list(head_ptr);

 printf("\n\nEnd of program\n");

 return 0;
  }

First_nameとlast_nameのifnormationを読み始めたいのですが、ユーザーにfirst_nameの入力を求めたら、値を入力してEnterキーを押すと、last_nameリクエストがスキップされます。なぜこれが起こっているのかわかりません。

4

3 に答える 3

2

文字列を読み取るため%sに代わりに使用します。%c読んだとき、%cあなたはたった1つの文字を取得し、残りはバッファに残しています。

printf("\nEnter First Name: ");
scanf("%s", &current_ptr->first_name);
于 2013-03-25T05:41:38.840 に答える
0

あなたの質問の理解によると。scanf()ステートメントの前にfflush()関数を呼び出す必要があると思います。あなたの問題は解決するかもしれませんが、私にはわかりません。この関数の構文を確認してください。

于 2013-03-25T05:46:59.410 に答える
0

c.fogelklouが述べたよう%sに、文字列と文字に使用します%c。ただし、scanfバッファオーバーフロー攻撃を引き起こす可能性があるため、この投稿このページを読んで、これを回避する方法を確認することをお勧めします。

于 2013-03-25T08:12:58.617 に答える