C:gets()跳过第一个输入

时间:2016-04-06 17:45:45

标签: c string input gets

#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;

}

添加学生时,计算机会要求用户输入姓氏而不是姓名。为什么程序会跳过“姓名”?显示“名称”,但是要求用户改写姓氏。

1 个答案:

答案 0 :(得分:1)

首先,不要使用gets,这是不安全的。请改用fgets

第一次拨打gets时跳过一行的原因是,当您拨打'\n'时,缓冲区中有enterRecordDetails()。通常,'\n'是早期输入操作的剩余部分,例如,使用int阅读scanf

解决此问题的一种方法是在阅读int时将字符串读取到最后,以便fgets的连续调用将获得实际数据。您可以在enterRecordDetails()函数中执行此操作:

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',并传递一个限制输入长度的格式说明符,并在scanf(" %19[^\n]", aRecord.name); // ^ 处停止:

import time
import datetime

class Time():
def __init__(self):
    currentTime = time.time()
    totalSeconds = int(currentTime)    
    self.__second = totalSeconds % 60
    totalMinutes = totalSeconds // 60
    self.__minute = totalMinutes %60
    totalHours = totalMinutes// 60
    self.__hour = totalHours % 12

def getSecond(self):
    return self.__second

def getMinute(self):
    return self.__minute

def getHour(self):
    return self.__hour
def main():
    t = Time()
    print("Current Time is : ", t.getHour(), ":", t.getMinute(), ":",   t.getSecond())
    print(datetime.datetime.now().time())
    print(time.ctime())

main()