c - 非 NULL 用户输入

标签 c string struct

I'm trying to understand the whole concept of pointers, structures, etc so I've created a program that gets user input for two different books and then swaps the info of the two books. I had no trouble doing that, however, a problem arose- when I pressed enter the name of the book would be plain blank and at the output I would, of course, see a blank space. My problem is, how am I able to limit the user to input any letter (A-Z, a-z) and not blank space?

一串字符当输入到数组中时,它们被保存在连续的内存地址中。我们也知道 'NULL' 在数组中表示为 '\0'。

考虑到上述情况,我进行了多次测试,其中所有 都未能产生预期的结果。

以下是我所做的一些尝试。

第一次尝试

while (pBook1->name[0] == '\0')
{
    printf("\n Please enter a valid book name: ");
    fgets(pBook1->name, MAX, stdin);
}

第二次尝试

while (strcmp(pBook1->name, ""))
{
    printf("\n Please enter a valid book name: ");
    fgets(pBook1->name, MAX, stdin);
}

此外,将以下代码视为我程序的源代码:

#include <stdio.h>
#include <string.h>
#define MAX 50

struct Books
{
    char name[MAX];
    int ID;
    float price;
};

void swap(struct Books *, struct Books *);

void main()
{

    struct Books Book1, Book2, *pBook1, *pBook2;

    pBook1 = &Book1;
    pBook2 = &Book2;

    // Input for the 1st book
    printf("\n 1st Book \n ------------------------------");
    printf("\n Enter the name: ");
    fgets(pBook1->name, MAX, stdin);
    while (pBook1->name[0] == '\0')
    {
        printf("\n Please enter a valid book name: ");
        fgets(pBook1->name, MAX, stdin);
    }
    printf("\n Enter the ID: ");
    scanf("%d", &pBook1->ID);
    printf("\n Enter the price: ");
    scanf("%f", &pBook1->price);

    // Input for the 2nd book
    printf("\n 2nd Book \n ------------------------------");
    printf("\n Enter the name: ");
    fgets(pBook2->name, MAX, stdin);
    while (pBook2->name[0] == '\0')
    {
        printf("\n Please enter a valid book name: ");
        fgets(pBook2->name, MAX, stdin);
    }
    printf("\n Enter the ID: ");
    scanf("%d", &pBook2->ID);
    printf("\n Enter the price: ");
    scanf("%f", &pBook2->price);

    printf("\n Let's swap the info of the two books...");

    swap(pBook1, pBook2);

    printf("\n The info of the two books is now:");

    printf("\n------------------------------ \n 1st Book \n ------------------------------------");
    printf("\n Name \t\t ID \t Price \n %s \t\t %d \t %f", pBook1->name, pBook1->ID, pBook1->price);

    printf("\n------------------------------ \n 2nd Book \n ------------------------------------");
    printf("Name \t\t ID \t Price \n %s \t\t %d \t %f", pBook2->name, pBook2->ID, pBook2->price);
}

 void swap(struct Books *pB1, struct Books *pB2)
 {
   char temp[MAX];

   strcpy(temp, pB1->name);
   strcpy(pB1->name, pB2->name);
   strcpy(pB2->name, temp);

   int tempID = pB1->ID, tempPrice = pB1->price;

   pB1->ID = pB2->ID;
   pB2->ID = tempID;

   pB1->price = pB2->price;
   pB2->price = tempPrice;
 }

最佳答案

fgets 读取直到遇到EOF\nN-1 字节已被读取。因此,如果您的程序的用户按下回车键,它将读取 \n 并停止。这意味着 pBook1->name[0] == '\n'。这就是为什么您检查 "" 是否相等以及 pBook1->name[0] == '\0' 失败的原因。

查看此 example .

这意味着您需要检查 \n\0 以防用户按 Ctrl-D 输入EOF 在 *nix 系统上。

关于c - 非 NULL 用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54317756/

相关文章:

c - 获取 avro 字符串时出错

c - 具有位域的结构的内存布局

java - 字符串中的数字频率

c++ - 将模板参数传递给嵌套结构

c# - 当您需要值类型和继承时

c# - 如果一个结构不能继承另一个类或结构,为什么 Int32 有 ToString() 方法?

c - 从 C 中的文件读取时使用 fscanf()

c - 在 C++/Fortran 中集成贝塞尔函数

C:使用 RegEx 将十六进制字符串分离为字符数组

java - 如何将不明确的字符串解析为日期?