我刚刚在教科书的帮助下在结构主题中编写了我的第一部分代码,我感觉好像很好地遵循了说明,但是只要我运行代码,它就会在到达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
您已经获得了数量的展示位置,但是忘记了将其包含在要打印的项目中。这给您带来不确定的行为。