Reputation: 3
I'm new to programming and I want to know how to check if the user input is within my condition. For example, the program only accepts inputs which are below 25.
Here is my code. But it isn't working. :(
for(i=1; i<=5; i++);
{
if(act[i]<25)
{
scanf("%d", &act[i]);
}
}
Upvotes: 0
Views: 133
Reputation: 62
This code is not working because you are trying to evaluate the if condition if(act[i]<25)
before act[i]
is actually received from the user.
What you need is this:
for(i=1;i<=5;i++)
{
again:
scanf("%d",&act[i]);
if(!(act[i]<25))
{
printf("Invalid input! Enter again..\n");
goto again:
}
}
the if condition has to be after you have scanned act[i]. If the user enters a number that is not within the desired range then a message is printed and you have to enter the number again.
Also, there shouldn't be a semicolon after for(i=1;i<=5;i++)
Upvotes: 1
Reputation: 1339
Assuming, you want to try forever if you keep receiving numbers grater than or equal to 25.
for(i=0; i<5; i++)
{
int n;
do{
scanf("%d", &n);
} while(n>=25);
act[i]=n;
}
Upvotes: 0