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

标签 c arrays string struct scanf

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

我必须使用一个名为 Product 的结构来执行此操作。问题是,当我输入任何函数(arrangePrice 或 arrangeQuantity)时,控制台打印“产品名称是什么?” 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 模式的开头留一个空格,以消耗可能遗留的任何换行符。

此外,%[^\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);

关于c - C语言中struct中字符串的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33403769/

相关文章:

c++ - 这是 ->> 一个旧的运算符还是一个错字/错误?

c - 使用 char 指针的段错误 (C)

c - 从txt文件读取字符是错误的

c - 指针&字符段错误

c++ - 默认构造一个整数数组会对其进行零初始化吗?

c# - 按价格排序列表 C#

php - 如何使用php根据数组生成查询

c# - 如何在分隔符上拆分字符串,并将分隔符作为结果数组中的元素保留?

java - 按分隔符和其他特定字符串分割字符串

string - 在 powershell 中连接两个字符串并作为一个参数提供