在循环中使用scanf()和gets()读取用户输入无效

问题描述 投票:0回答:1

我刚刚在教科书的帮助下在结构主题中编写了我的第一部分代码,我感觉好像很好地遵循了说明,但是只要我运行代码,它就会在到达for循环的第二轮。有人有什么想法吗?

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

struct invStruct {
   int quantity;
   float price;
   char manuf[26];
};


int main()
{

    int i;

    struct invStruct item[3];

    for (i = 0; i < 3; i++)
    {

       printf("What is the quantity of this computer #%d?\n", i+1);
       scanf(" %d", &item[i].quantity);

       printf("What is the price of this computer?\n", i+1);
       scanf(" %.2f", &item[i].price);

       puts("What is the manufacturer of this computer?");
       gets(item[i].manuf);

       getchar();
    }

    printf("Here are the computers and their info\n");

    for (i = 0; i < 3; i++)
    {
       printf("#%d:\n\tPrice: %.2f\n\tQuantity: %d\n\tManufacturer: %s\n", i+1, item[i].price, item[i].manuf);
    }



    return(0);
}
sample inputs:
5
250
lenovo
//at this point the loop stops functioning completely

c input scanf gets
1个回答
0
投票

您已经获得了数量的展示位置,但是忘记了将其包含在要打印的项目中。这给您带来不确定的行为。

© www.soinside.com 2019 - 2024. All rights reserved.