Hi,
I need a hint on how to make sure the user is entering a positive integer (no strings, no characters in between numbers, no negative numbers). The method I am trying to use is "atoi" conversion and the problem is it messes up when I try to enter zero although zero is an acceptable number. Here is my code;
int confirm, flag, down, up;
char number_of_entities[200], answer[100];
while(confirm)
{
printf("Input the number of entities> "
scanf("%s", &number_of_entities);
printf("\n"
flag = 1;
while(flag)
{
down = (int)(floor(atof(number_of_entities)));
up = (int)(ceil(atof(number_of_entities)));
if(atoi(number_of_entities)>MAX_NUMBER || atoi(number_of_entities)<0)
{
printf("Please enter an appropriate number!!!\n"
scanf("%s", &number_of_entities);
}
else
{
if(up == down)
flag = 0;
else
{
printf("Please enter an appropriate number!!!\n"
scanf("%s", &number_of_entities);
}
}
}
printf("Number of entities you entered is> %d\n", atoi(number_of_entities));
printf("Is this correct?(y/n)\n"
scanf("%s", &answer);
if(answer[0] == 'y' || answer[0] == 'Y')
{
confirm = 0;
}
if(answer[0] == 'n' || answer[0] == 'N')
{
confirm = 1;
}
}
Thanx
I need a hint on how to make sure the user is entering a positive integer (no strings, no characters in between numbers, no negative numbers). The method I am trying to use is "atoi" conversion and the problem is it messes up when I try to enter zero although zero is an acceptable number. Here is my code;
int confirm, flag, down, up;
char number_of_entities[200], answer[100];
while(confirm)
{
printf("Input the number of entities> "
scanf("%s", &number_of_entities);
printf("\n"
flag = 1;
while(flag)
{
down = (int)(floor(atof(number_of_entities)));
up = (int)(ceil(atof(number_of_entities)));
if(atoi(number_of_entities)>MAX_NUMBER || atoi(number_of_entities)<0)
{
printf("Please enter an appropriate number!!!\n"
scanf("%s", &number_of_entities);
}
else
{
if(up == down)
flag = 0;
else
{
printf("Please enter an appropriate number!!!\n"
scanf("%s", &number_of_entities);
}
}
}
printf("Number of entities you entered is> %d\n", atoi(number_of_entities));
printf("Is this correct?(y/n)\n"
scanf("%s", &answer);
if(answer[0] == 'y' || answer[0] == 'Y')
{
confirm = 0;
}
if(answer[0] == 'n' || answer[0] == 'N')
{
confirm = 1;
}
}
Thanx