gpt4 book ai didi

c - 陷入 getIntLimited 函数中

转载 作者:行者123 更新时间:2023-11-30 19:34:53 25 4
gpt4 key购买 nike

我遇到了陷入 getIntLimited 函数的问题。在调整数量中,我需要它检查用户是否输入了正确的数字,而不是多于或少于所需的数字,而不是字母。我没有对“库存”选项这样做,只在“检查”选项中这样做。但我的第一个问题是我必须输入数字两次才能得到响应,第二个问题是我无法退出 getIntLimited 循环。这是输出的图像: enter image description here

以及我的代码部分:

void GroceryInventorySystem(void){
struct Item Items[MAX_ITEM_NO];
int opt, records;
int loop = 0;
welcome();
while(!loop){
if(opt == 3){
adjustQuantity(Items, records, CHECKOUT);
saveItems(Items, DATAFILE, records);
if(0 == saveItems(Items, DATAFILE, records)){
printf("Could not update data file %s\n", DATAFILE);
}
pause();
}
}
int getInt(void){ // Check if user entered the character and breaks, returns the value if a number //
char letter = 'x';
int value;
while(1){
scanf("%d%c", &value, &letter);
if(letter != '\n'){
printf("Invalid integer, please try again: ");
flushKeyboard();
}else{
return value;
}
}
}

int getIntLimited(int lowerLimit, int upperLimit){ // Check if user typed the value higher/lower and repeats. Returns the value if user entered the right number //
int Value;
while(1){
Value = getInt();
if(Value <= lowerLimit || Value >= upperLimit){
printf("Invalid value, %d < value < %d: ", lowerLimit, upperLimit);
}else{
return Value;
}
}
}
void adjustQuantity(struct Item item[], int NoOfRecs, int stock) {
int check, sku, index, opt;
char tostock[] = "to stock", tocheck[] = "to checkout";
printf("Please enter the SKU: ");
scanf("%d", &sku);
check = locateItem(item, NoOfRecs, sku, &index);
if (check == 0) {
printf("Item not found!\n");
} else {
displayItem(item[index], FORM);
if (stock == STOCK) {
printf("Please enter the quantity %s; Maximum of %d or 0 to abort: ", tostock, MAX_QTY - item[index].quantity);
scanf("%d", &opt);
if (opt == 0) {
printf("--== Aborted! ==--\n");
} else {
item[index].quantity += opt;
printf("--== Stocked! ==--\n");
}
} else {
printf("Please enter the quantity %s; Maximum of %d or 0 to abort: ", tocheck, item[index].quantity);
scanf("%d", &opt);
if (opt == 0) {
printf("--== Aborted! ==--\n");
} else if (item[index].quantity < opt){
opt = getIntLimited(item[index].quantity, 0);
} else {
item[index].quantity -= opt;
printf("--== Checked out! ==--\n");
if (item[index].quantity <= opt) {
printf("Quantity is low, please reorder ASAP!!!\n");
}
}
}
}
}

最佳答案

我在 opt = getIntLimited(0, item[index].quantity); 中犯了一个错误,它应该是相反的 opt = getIntLimited(item[index].quantity, 0); 现在,只要我在 0 和 5 之间键入正确的数字,它就会退出循环。我还使用了 opt = getIntLimited(0, item[index].quantity); 而不是 scanf,并删除了 else if 语句

关于c - 陷入 getIntLimited 函数中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43329333/

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