C 陷入getIntLimited函数中
我在使用getIntLimited函数时遇到问题。在adjustQuantity中,我需要它来检查用户是否输入了正确的数字,不多于或少于所需的数字,而不是字母。我没有对“股票”期权这么做,只是对“支票”期权。但我的第一个问题是我必须输入两次数字才能从中得到响应,第二个问题是我无法退出getIntLimited循环。 以下是输出的图像: 我的代码部分:C 陷入getIntLimited函数中,c,C,我在使用getIntLimited函数时遇到问题。在adjustQuantity中,我需要它来检查用户是否输入了正确的数字,不多于或少于所需的数字,而不是字母。我没有对“股票”期权这么做,只是对“支票”期权。但我的第一个问题是我必须输入两次数字才能从中得到响应,第二个问题是我无法退出getIntLimited循环。 以下是输出的图像: 我的代码部分: void GroceryInventorySystem(void){ struct Item Items[MAX_ITEM_NO];
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");
}
}
}
}
}
void杂货店复仇系统(void){
结构项目项目[最大项目编号];
int opt,记录;
int循环=0;
欢迎();
while(!loop){
如果(opt==3){
调整数量(项目、记录、结帐);
保存项目(项目、数据文件、记录);
if(0==保存项(项、数据文件、记录)){
printf(“无法更新数据文件%s\n”,数据文件);
}
暂停();
}
}
int getInt(void){//检查用户是否输入了字符并断开,如果是数字,则返回值//
字符字母='x';
int值;
而(1){
scanf(“%d%c”、&value和字母);
如果(字母!='\n'){
printf(“无效整数,请重试:”;
刷新键盘();
}否则{
返回值;
}
}
}
int GetIntLimit(int lowerLimit,int upperLimit){//检查用户是否键入了较高/较低的值并重复。如果用户输入了正确的数字,则返回该值//
int值;
而(1){
Value=getInt();
如果(值=上限){
printf(“无效值,%d<值<%d:”,下限,上限);
}否则{
返回值;
}
}
}
无效调整数量(结构项目[],内部无库存,内部库存){
整数检查、sku、索引、opt;
char tostock[]=“待售”,tocheck[]=“待售”;
printf(“请输入SKU:”;
scanf(“%d”和&sku);
检查=定位项目(项目、NoOfRecs、sku和索引);
如果(检查==0){
printf(“未找到项目!\n”);
}否则{
显示项目(项目[索引],表格);
如果(股票==股票){
printf(“请输入数量%s;最多%d或0以中止:”,tostock,MAX_QTY-物料[index]。数量);
scanf(“%d”和&opt);
如果(opt==0){
printf(“--==中止!==-\n”);
}否则{
项目[索引]。数量+=opt;
printf(“--==库存!=-\n”);
}
}否则{
printf(“请输入数量%s;最多%d或0以中止:”,tocheck,item[index]。数量);
scanf(“%d”和&opt);
如果(opt==0){
printf(“--==中止!==-\n”);
}else if(项目[索引]。数量 如果(item[index].quantity我在opt=getIntLimited(0,item[index].quantity);
中犯了一个错误,应该是相反的opt=getIntLimited(item[index].quantity,0);
现在只要我在0到5之间键入正确的数字,它就会退出循环。
我还放入了opt=getIntLimited(0,项[index].quantity);
而不是scanf
,并删除了else,如果语句您真的调试过它吗?我在opt=getIntLimited(0,项[index].quantity)中犯了一个错误;它应该与opt=getIntLimited(项[index].quantity)相反;现在,每当我在0和5之间键入正确的数字时,它就会退出循环。在scanf(“%d%c”、&value、&letter)之前移动字符字母='x';没有做任何操作。