for (int i = 1; i < 22; i ){
if(i<=99 && i>=0){
printf("enter an age");
scanf("%d", &ages[i]);
}
else{
printf("enter a valid number");
}
}
When I enter a number outside the if statement such as 999, my program still accepts it instead of printing enter a valid number message. thanks in advance.
When I enter a number outside the if statement such as 999, my program still accepts it instead of printing enter a valid number message. thanks in advance.
CodePudding user response:
I think as the 'i' is the index of the 'ages' array, you are allowing values with a range of 0-99. So, if the value is out oof this range also, it will still be stored in the 'ages' array. It won't display the warning message (enter a valid number) also. Instead, you can try something like this:
for (int i = 0; i < 22; i ){
printf("enter an age: ");
scanf("%d", &ages[i]);
if(ages[i] > 99 || ages[i] < 0){
printf("enter a valid number\n");
i--;
}
}