用C语言

时间:2015-10-29 00:08:04

标签: c arrays string struct scanf

我已经学习了将近三个月的C,而且我在途中遇到了很多麻烦。但是,我有这个任务来创建一个程序,用于安排一系列产品(由用户选择),或者按价格或可用数量排列。

我不得不使用一个名为Product的结构来实现。问题是,当我输入任何功能(arrangePrice或arrangeQuantity)时,控制台会输出“产品的名称是什么?” printf命令和IMMEDIATELY打印出“产品的价格是多少?” 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;
}

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

2 个答案:

答案 0 :(得分:2)

您的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);

答案 1 :(得分:0)

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

struct Product vet_array[ 2 ];

arrangePrice( vet_array, 2 );

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