同时声明错误的傻瓜证明
这个错误发生在int getInt(void)的while循环中,我想循环并要求用户输入一个数字,所以就像他们输入10那样它会通过,但是如果他们要输入10abc,它应该循环并再次询问并且不断询问,直至获得有效的输入。 它应该是万无一失的,所以它只会通过一个数字来处理它,而那就是它。 出于某种原因,while循环内的printf语句无限循环,我无法输入新值。
{#include <stdio.h>
void welcome(void);
int getInt(void);
double getDbl(void);
void prnTitle(void);
void prnFooter(double gTotal);
void pause(void);
double getDblLimited(double lowerLimit, double upperLimit);
// 1-提交里程碑1时注释掉主要内容// 2-您的最后一个里程碑应该与以下测试程序完美结合。 //注释掉main()删除箭头后面的下两个字符
int main(void)
{
int iVal;
double dVal;
welcome();
// testing prnTitle()
printf("listing header and footer with grand total:n");
prnTitle();
printf("");
// 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: %dn", iVal);
// Testing Get Int Limited
printf("Enter an integer between 10 an 20: ");
iVal = getIntLimited(10, 20);
printf("Your entered %dn", iVal);
// Testing Get Double
printf("Enter a floating point number: ");
dVal = getDbl();
printf("You entered: %0.2lfn", 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.2lfn", dVal);
printf("End of tester program for milestone one!n");
return 0;
}
/ *结束注释掉主* /
//在这里实现里程碑一的功能: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;
if (gTotal > 0)
{
printf("%12.21f", gTotal);
}
else
{
printf("");
}
}
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;
}
tsk tsk jason。 阅读大纲并查看流程图。 你需要添加“clrKyb();” 进入while循环。
同样清晰的字符已经是stdio.h库中包含的一个函数(这实际上就是在我们的教科书和在线笔记中)
void clrkyb(void)
{
while( getchar() != 'n' );
} ;
int getInt(void)
{
int d ;
char c ;
scanf( "%d%c", &d, &c );
while( c != 'n' )
{
clrKyb();
printf( "Invalid"...": " );
scanf( "..." &.. );
}
return d ;
} ;
链接地址: http://www.djcxy.com/p/12519.html