0

我正在制作一个程序,要求用户输入 5 种产品及其价格的列表。名称和价格都存储在两个并行数组中,稍后将一起使用这些数组来输出每种产品的价格。用于请求输入和存储信息的函数如下:

void NamesAndPrices(char products[5][41], float prices[5]) {
    for (int i = 0; i < 5; i++) {
        printf("Type the name of the product: ");
        fgets(products[i], 41, stdin);
        strtok(products[i], "\n");
        printf("Type the price: ");
        scanf("%f", &prices[i]);
    }
}

它接收两个参数:将存储信息的数组,都在main()函数中声明:

int main() {
    char products[5][41];
    float prices[5];
    NamesAndPrices(products, prices);
    return 0;
}

编译和运行代码时,它只询问产品名称一次。这是一个例子:

Type the name of the product: 40 x 60 Rug
Type the price: 39.99
Type the name of the product: Type the price: 0
Type the name of the product: Type the price: 0
Type the name of the product: Type the price: 0
Type the name of the product: Type the price: 0

为什么fgets()在第一次执行语句后被忽略?

4

0 回答 0