I am looking for some help with scanned user-provided values. In my code below I want to check if every entered value is integer (ca, ta, dd, gs), and if not - loop a message about it to the point where it will be. I saw few old questions about it, but I couldn't get it to work anyways. I am fairly new to programming, so I don't know much. I appreciate every answer.
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;