我正在寻找一些有关扫描用户提供的值的帮助。在下面的代码中,我想检查每个输入的值是否是整数(ca、ta、dd、gs),如果不是,则将有关它的消息循环到它将出现的位置。我看到了一些关于它的老问题,但无论如何我都无法让它工作。我对编程相当陌生,所以我不太了解。我感谢每一个答案。
int ca; // Current altitude
int ta; // Target altitude
int dd; // Distance for descent/climb
int gs; // Ground speed`
char cont; // variable to continue or close program
do
{
printf("Type your current altitude: ");
scanf("%i", &ca);
printf("\nType your target altitude: ");
scanf("%i", &ta);
while(ca == ta) // If user entered same target altitude as current one, message will prompt him to type it again
{
printf("Target altitude needs to be different than your current one! Type it again: ");
scanf("%i", &ta);
}
printf("\nType distance for descent/climb: ");
scanf("%i", &dd);
printf("\nType your ground speed: ");
scanf("%i", &gs);
int ad = ta - ca; // Calculates altitude difference - how much feet aircraft needs to descent or climb
float time = dd*1.0 / gs*1.0; // v = s / t => t = s / v - Calculates how much time aircraft has to descent or climb (time in hours) | variables are multiplied by 1.0 to receive float in result
int fpm = ad / (time*60); // Calculates FPM needed to descent in that time | time is multiplied by 60 in order to receive time in minutes
if(fpm > 0)
{
printf("\nAircraft needs to climb at rate of %i FPM (doesn't include capturing altitude)\n", fpm);
}
else
{
printf("\nAircraft needs to descent at rate of %i FPM (doesn't include capturing altitude)\n", fpm);
}
printf("\nDo you want to run a program again? (y/n) ");
scanf(" %c", &cont);
while(cont != 'y' && cont != 'Y' && cont != 'n' && cont != 'N') // If user typed different character than y or n, message will prompt him to type it again
{
printf("\nType n or y: ");
scanf(" %c", &cont);
}
printf("\n");
}
while(cont == 'Y' || cont == 'y');
return 0;