Reputation:
I am coding a function to validate a number inserted by the user (between 1 and 3) and it doesn't matter what number I put the program crashes.
Here's my code:
int validate(int low, int high) {
int selection;
do {
scanf("%d", selection);
if (selection < low || selection > high)
printf("Invalid Input, try again: ");
} while (selection > low || selection < high);
return selection;
}
Anyone any idea, please?
Upvotes: 0
Views: 35
Reputation: 946
While reading scanf, use & with the variable
int validate(int low, int high) {
int selection;
printf("Enter your Selection ");
do {
scanf("%d", &selection);
if (selection < low || selection > high)
{
printf("Invalid Input, try again: ");
}
} while (selection < low || selection > high) ;
return selection;
}
Upvotes: 1
Reputation: 199
2 bugs I see.
makdu, mention the scanf issue.
your while loop should use the && operator.
} while (selection > low && selection < high);
Upvotes: 0