C语言中struct中字符串的使用

Use of strings in structs in C language

我学C已经快三个月了,一路上从来没有遇到过什么麻烦。但是,我的任务是创建一个程序,该程序将按价格或可用数量排列一系列产品(由用户选择)。

我不得不使用一个名为 Product 的结构来这样做。问题是,当我输入任何函数(arrangePrice 或 arrangeQuantity)时,控制台打印 "What is the name of product?" printf 命令并立即打印出 "What is the price of the product?" printf 命令。它似乎只是忽略了那些让用户在字符串上写入产品名称的命令之间的 scanf 函数。为什么会这样???

完整代码如下:

#include <stdio.h>
#include <string.h>

struct Product
{
    char name[80];
    double price;
    int quantity;
};

void arrangePrice(struct Product vet[], int n)
{
    int i, j, auxQuant;
    double aux, auxprice;
    char auxname[80];
    for (i = 0; i < n; i++)
    {
        printf("What is the name of the product?\n");
        scanf("%[^\n]", vet[i].name);
        printf("What is the price of the product?\n");
        scanf("%lf", &vet[i].price);
        printf("What is the available quantity of the product?\n");
        scanf("%d", &vet[i].quantity);
    }
    printf("\n\nArranging by price:\n\n");
    for (j = 0; j < n; j++)
    {
        aux = vet[j].price;
        for (i = j + 1; i < n; i++)
        {
            if (vet[i].price < aux)
            {
                auxprice = vet[i].price;
                strcpy(auxname, vet[i].name);
                auxQuant = vet[i].quantity;
                vet[i] = vet[j];
                strcpy(vet[j].name, auxname);
                vet[j].price = auxprice;
                vet[j].quantity = auxQuant;
            }
        }
    }
    for (i = 0; i < n; i++)
    {
        printf("%[^\n] -> %.2lf\n", vet[i].name, vet[i].price);
    }
}

void arrangeQuant(struct Product vet[], int n)
{
    int i, j, aux, auxQuant;
    double auxprice;
    char auxname[80];
    for (i = 0; i < n; i++)
    {
        printf("What is the name of the product?\n");
        scanf("%[^\n]", vet[i].name);
        printf("What is the price of the product?\n");
        scanf("%lf", &vet[i].price);
        printf("What is the available quantity of the product?\n");
        scanf("%d", &vet[i].quantity);
    }
    printf("\n\nArranging by available quantity:\n\n");
    for (j = 0; j < n; j++)
    {
        aux = vet[j].quantity;
        for (i = j + 1; i < n; i++)
        {
            if (vet[i].quantity < aux)
            {
                auxprice = vet[i].price;
                strcpy(auxname, vet[i].name);
                auxQuant = vet[i].quantity;
                vet[i] = vet[j];
                strcpy(vet[j].name, auxname);
                vet[j].price = auxprice;
                vet[j].quantity = auxQuant;
            }
        }
    }
    for (i = 0; i < n; i++)
    {
        printf("%[^\n] -> %d\n", vet[i].name, vet[i].quantity);
    }
}

int main()
{
    struct Product prod[51];
    int n;
    int choice;
    printf("How many products will be added? (Maximum of 50)\n");
    scanf("%d", &n);
    while (n < 1 || n > 50)
    {
        printf("Invalid value. Try again.\n");
        scanf("%d", &n);
    }
    printf("Do you want to arrange by price or by available quantity? (0 or 1)\n");
    scanf("%d", &choice);
    if (choice == 0) arrangePrice(prod, n);
    else if (choice == 1) arrangeQuant(prod, n);
    else printf("Invalid value.\n");
    return 0;
}

我必须说我实际上仍然不知道代码是否正确,因为我无法输入产品名称。感谢您的帮助!

您的 scanf 调用在输入缓冲区中留下换行符,这些换行符将在后续调用中读取。

您需要在每个 scanf 模式的开头留下一个 space 以消耗可能遗留的任何换行符。

此外,%[^\n] 不是 printf 的有效格式说明符。请改用 %s

示例:

    printf("What is the name of the product?\n");
    scanf(" %s", vet[i].name);    // use %s for strings
    printf("What is the price of the product?\n");
    scanf(" %lf", &vet[i].price);
    printf("What is the available quantity of the product?\n");
    scanf(" %d", &vet[i].quantity);

并且:

printf("How many products will be added? (Maximum of 50)\n");
scanf(" %d", &n);
while (n < 1 || n > 50)
{
    printf("Invalid value. Try again.\n");
    scanf(" %d", &n);
}
printf("Do you want to arrange by price or by available quantity? (0 or 1)\n");
scanf(" %d", &choice);

struct product vet[] 是一个指针。它实际上并不指向一个数组,除非您在调用 arrangePrice 之前分配了这样一个数组。例如:

struct Product vet_array[ 2 ];

arrangePrice( vet_array, 2 );

或者你可以调用 malloc 但只是为了让它开始工作,在堆栈上分配一个具有固定数量元素的局部变量。