Reputation: 2772
I'm new at programming in c but I just don't understand why this code won't run properly after compiling. For now, I just want it to take numbers between 10 and 100 without errors. Then later I'll add return 1 for errors and 0 for success.
#include <stdio.h>
#include <stdlib.h>
int intGet(int, int);
int error();
int main(void)
{
int Min, Max, UserIn;
Min = 10;
Max = 100;
printf("Enter a number in between [10 -100]: \n");
scanf("%d", &UserIn);
printf("Read %d\n", UserIn);
while (UserIn < Min && UserIn > Max)
{
printf("Invalid \n");
scanf("%d", &UserIn);
}
/* What I did to fix
while ((UserIn > Min) || (UserIn < Max)){
printf("Enter a number in between [10 -100]: \n");
scanf("%d",&UserIn);
printf("Read %d\n",UserIn);
while ((UserIn < Min) || (UserIn > Max)){
printf("Invalid \n");
scanf("%d", &UserIn);
printf("Read %d\n", UserIn);
}
}*/
return EXIT_SUCCESS;
}
int intGet(int min, int max)
{
}
int error()
{
}
Upvotes: 0
Views: 538
Reputation: 3807
To help you debug this, try this code:
while ((UserIn < Min) || (UserIn > Max))
{
printf("Invalid \n");
scanf("%d", &UserIn);
printf("Read %d\n", UserIn);
}
Also, this while loop will only run if the first inputted value for Userin was < 10 or > 100.
Upvotes: 0
Reputation: 59997
You do realise that scanf
does return a value? See http://linux.die.net/man/3/scanf
If it does not return 1 then you need to "eat" some input.
Better to read in a string and parse that.
Also
while (UserIn < Min && UserIn > Max)
should be
while (UserIn < Min || UserIn > Max)
Upvotes: 1
Reputation: 61
replace
while (UserIn<Min && UserIn>Max){
with:
while (UserIn<Min || UserIn>Max){
also you can replace your while loop with do ... while. then you will not have double scanf
Upvotes: 0
Reputation: 629
while (UserIn < Min && UserIn > Max)
userIn
can never meet both conditions. Change it to:
whihe (userIn < Min || userIn > Max)
Upvotes: 2