C:gets()跳过第一个输入 [英] C: gets() skips the first input
问题描述
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct record
{
char name[20];
char surname[20];
char telephone[20];
}Record;
typedef struct node
{
Record data;
struct node *next;
}Node;
Node *head = NULL;
void addRecord(Record s)
{
Node *newNode;
Node *n;
newNode = (Node*)malloc(sizeof(Node));
newNode->data = s;
newNode->next = NULL;
if (head == NULL)
{
// The linked list is empty
head = newNode;
}
else
{
// Traverse the whole list until we arrive at the last node
n = head;
while (n->next != NULL)
{
n = n->next;
}
n->next = newNode;
}
}
Record enterRecordDetails()
{
Record aRecord;
int len;
int valid = 0;
int valid2 = 0;
printf("ENTER THE FOLLOWING RECORD DETAILS:\n");
printf(" NAME : ");
gets(aRecord.name);
printf(" SURNAME : ");
gets(aRecord.surname);
do {
valid = 0;
valid2 = 0;
printf(" TELEPHONE NO. (8 digits): ");
gets(aRecord.telephone);
len = strlen(aRecord.telephone);
for (int i = 0; i < len; i++)
{
if (!isdigit(aRecord.telephone[i])) {
printf("You enterred an invalid number\n");
valid = 1; break;
}
}
Node *p = head;
while (p != NULL)
{
Node *next = p->next;
for (; next; p = next, next = next->next) {
if (strcmp(p->data.telephone, aRecord.telephone) == 0)
{
valid2 = 1; break;
}
}
if(p = NULL)break;
}
} while((valid == 1) || (len != 8) || (valid2 == 1));
getchar();
fflush(stdin);
return aRecord;
}
int main(void)
{
char menuOption;
do
{
system("cls");
printf("~~~ MAIN MENU ~~~\n");
printf("1. Add Telephone Record\n");
printf("2. Delete Telephone Record\n");
printf("3. Search\n");
printf("4. Display All Records\n");
printf("5. Exit\n\n");
menuOption = getchar();
fflush(stdin);
switch (menuOption)
{
case '1':
addRecordToList();
break;
case '4':
displayList();
break;
}
} while (menuOption != '5');
getchar();
return 0;
}
添加学生时,计算机会要求用户输入姓氏而不是姓名.为什么程序跳过名称"?显示名称",但是希望用户改写姓氏.
When adding a student, the computer asks the user to enter the surname rather than the name. Why is the program skipping 'Name' ? 'Name' is displayed, however the user is expected to write the surname instead.
推荐答案
首先,不要使用gets
,这是不安全的.代替使用 fgets
.
First, do not use gets
, it's unsafe. Use fgets
instead.
第一次调用gets
跳过行的原因是,在调用enterRecordDetails()
时缓冲区中有一个'\n'
.通常,'\n'
是早期输入操作(例如,用scanf
读取int
)的遗留物.
The reason why the first call to gets
skips a line is that there is a '\n'
in the buffer at the time that you are making a call to enterRecordDetails()
. Typically, the '\n'
is a leftover from an earlier input operation, for example, from reading an int
with scanf
.
解决此问题的一种方法是在读取int
时将字符串读取到末尾,以便连续调用fgets
将获得实际数据.您可以在enterRecordDetails()
函数中完成此操作:
One way to fix this problem is to read the string to the end when reading the int
, so that the consecutive call of fgets
would get the actual data. You can do it in your enterRecordDetails()
function:
Record enterRecordDetails()
{
Record aRecord;
printf("ENTER THE FOLLOWING RECORD DETAILS:\n");
printf(" NAME : ");
fscanf(stdin, " "); // Skip whitespace, if any
fgets(aRecord.name, 20, stdin);
printf(" SURNAME : ");
fgets(aRecord.surname, 20, stdin);
}
但是,请注意,fgets
将'\n'
保留在字符串中,只要它适合缓冲区即可.更好的方法是使用scanf
,并传递一个格式说明符,该说明符限制输入的长度并在'\n'
处停止:
Note, however, that fgets
keeps '\n'
in the string, as long as it fits in the buffer. A better approach would be using scanf
, and passing a format specifier that limits the length of input and stops at '\n'
:
scanf(" %19[^\n]", aRecord.name);
// ^
这篇关于C:gets()跳过第一个输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!