gpt4 book ai didi

如果输入超过可用库存,则检查验证

转载 作者:太空宇宙 更新时间:2023-11-04 03:31:46 24 4
gpt4 key购买 nike

谁能帮我解决程序验证问题

我程序的实际输出:

选择1。导入 我输入了 2 次,我的第一个输入是 100 第二个输入是 200

所以查看时选择2。存储输出将是这个

Year    sets
1 100
2 200

入库共300台电脑

现在选择 3。卖单 我输入 400 所以它会显示 Sorry we have No enough Stocks !

我的问题是在查看我的 3 时。再次存储所有库存现在都是空的

Year    sets
1 0
2 0

我期望当我的输入超过可用库存时,它不会继续减少我存储中的库存

int main(void) {
int choice = 0;
int year = 1, i, com;
int storage[99] = { 0 };

for (;;) {
clrscr();

printf("Year %d\n\n", year);

printf("1. Import\n");
printf("2. Storage\n");
printf("3. Sell Order\n");

printf("\nchoice: ");

scanf("%d", &choice);

if (choice == 1) { // import
clrscr();
printf("Enter sets of computer's imported: ");
scanf("%d", &storage[year]);
year++;
}
if (choice == 2) { // storage
clrscr();
printf("Year sets\n");

for (i = 1; i < year; i++) {
printf("%2d %4d\n", i, storage[i]);
}
getch();
}
if (choice == 3) { //order
printf("Enter Sets of Computer ordered: ");
scanf("%d", &com);

for (i = 0; com && i < 99; i++) {
if (com <= storage[i]) {
storage[i] = storage[i] - com;
com = 0;
} else {
com = com - storage[i];
storage[i] = 0;
}
}
if (com > storage[i]) { // validation
printf("Sorry we have No enough Stocks !");
getch();
}
}
}
}

最佳答案

你在计算你有多少存货之前就清空了存货。你想先加起来你有多少库存,检查你是否有足够的,然后如果你有足够的库存就清空

    if (choice == 3) { //order
printf("Enter Sets of Computer ordered: ");
scanf("%d", &com);

for(i=0;i<99;i++) //calculates the amount of stock you have
{
Total_Stock+=storage[i];
}

for (i = 0; com && i < 99; i++) {

if(Total_Stock<com) //If not enough stock it breaks the loop
//before subtracting the stock
{
printf("Not enough stock.\n");
break;
}
if (com <= storage[i]) {
storage[i] = storage[i] - com;
com = 0;
} else {
com = com - storage[i];
storage[i] = 0;
}
}

关于如果输入超过可用库存,则检查验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35957746/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com