gpt4 book ai didi

c 无限循环错误

转载 作者:行者123 更新时间:2023-11-30 21:08:05 25 4
gpt4 key购买 nike

程序构建正确,但我得到了随机数的无限循环,我不确定为什么。我

int getInt(void);
double getDbl(void);
void prnTitle(void);
void prnFooter(double gTotal);
void pause(void);
double getDblLimited(double lowerLimit, double upperLimit);
  1. 提交里程碑一时注释掉 main
  2. 您的最后一个里程碑应该可以与以下测试程序完美运行。

要注释掉 main(),请删除箭头后面的两个字符

int main (void)
{
int iVal;
double dVal;
welcome();

// testing prnTitle()
printf("listing header and footer with grand total:\n");
prnTitle();



// Testing prnFooter()
prnFooter(1234.5678);
printf("listing header and footer without grand total:\n");
prnTitle();
prnFooter(-1);


// Testing Pause() and clrKyb()
pause();


// Testing getInt()
printf("Enter an integer: ");
iVal = getInt();
printf("You entered: %d\n", iVal);


// Testing Get Int Limited
printf("Enter an integer between 10 an 20: ");
iVal = getIntLimited(10, 20);
printf("Your entered %d\n", iVal);


// Testing Get Double
printf("Enter a floating point number: ");
dVal = getDbl();
printf("You entered: %0.2lf\n", dVal);


// Tesing Get Double Limited
printf("Enter a floating point number between 10.00 an 20.00: ");
dVal = getDblLimited(10.0, 20.0);
printf("You entered: %0.2lf\n", dVal);



printf("End of tester program for milestone one!\n");
return 0;
}


/* End Commenting out the main out*/

// implement your functions for milestone one down here:
void welcome(void)
{
printf("---=== Grocery Inventory System ===---\n");

}

void prnTitle(void)
{
printf("Row |SKU| Name \t | Price |Taxed| Qty | Min | Total |Atn \n");
printf("----+---+--------------------+--------+-----+-----+-----+------------+|---\n");

}
void prnFooter(double gTotal)
{
printf("--------------------------------------------------------+-----------------\n");
//float gTotal = 1234.57;
while (gTotal > 0)
{
printf("%12.21f", gTotal);
}
}
void clrKyb(void)
{
char b;
while (b != '\n')
{
scanf("%c", &b);
}

}
void pause(void)
{
printf("Press <Enter> to continue...");

clrKyb();

}
int getInt(void)
{
//printf("Enter an interger:");
int d;
char c;
scanf("%d%c", &d, &c);
while (c != '\n')
{
printf("Invalid integer, please try again ");
scanf("%d%c", &d, &c);
}
return d;
}
int getIntLimited(int lowerLimit, int upperLimit)
{
int a;
//printf("Enter an integer between %d and %d:", lowerLimit, upperLimit);
scanf("%d", &a);
while (a <= lowerLimit || a >= upperLimit)
{
// printf("Invalid value, %d < value < %d:", lowerLimit, upperLimit);
scanf("%d", &a);
}
return a;
}
double getDbl(void)
{
double d;
char c;
// printf("Enter a floating point number:");
scanf("%lf,%c", &d, &c);
while (c != '\n')
{
printf("Invalid number, please try again ");
scanf("%lf%c", &d, &c);
}
return d;
}

double getDblLimited(double lowerLimit, double upperLimit)
{
double a;
printf("Enter a floating point number between %f and %f:", lowerLimit, upperLimit);
scanf("%lf", &a);
while (a <= lowerLimit || a >= upperLimit)
{
printf("Invalid value, %f < value < %f:", lowerLimit, upperLimit);
scanf("%lf", &a);

}
return a;
return 0;
}

最佳答案

void prnFooter(double gTotal)
{
printf("--------------------------------------------------------+-----------------\n");
//float gTotal = 1234.57;
while (gTotal > 0)
{
printf("%12.21f", gTotal);
}
}

您永远不会在循环内修改gTotal 的值。如果进入循环时它大于 0,它将永远大于 0,从而进入无限循环。

关于c 无限循环错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40432191/

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