c - Incorrect output from while loop -


this function asks integer between lolimit , hilimit , allows user safely enter it. keeps asking until user enters number in legal range. returns legal number.

the correct output should be: please enter integer between 32 , 127: 19 please enter integer between 32 , 127: 128 please enter integer between 32 , 127: 48 please enter integer between 48 , 127: 47 please enter integer between 48 , 127: 65 

however, output behaves strangely, seems if lolimit value changes , forth. also, numbers recorded after entered twice.

please enter integer between 32 , 127: 2 please enter integer between 32 , 127: 150 please enter integer between 32 , 127: 56 please enter integer between 32 , 127: 56 please enter integer between 56 , 127: 66 please enter integer between 32 , 127: 66 please enter integer between 66 , 127: 77 

here's code:

int enternumber (int lolimit, int hilimit){     int min;     int max;      {     printf("please enter integer between %d , %d:\n", lolimit, hilimit);     scanf("%d", &min);     }     while (min<lolimit || min>hilimit);     return min; }  int main(){     enternumber(32,127);     int min=enternumber(32,127);      enternumber(min,127);     int max=enternumber(enternumber(32,127),127);   } 

you call enternumber() function 5 times 5 accepted values taken :

  enternumber(32,127);   please enter integer between 32 , 127: 56    1st accepted number    int min=enternumber(32,127);   please enter integer between 32 , 127: 56  // 2nd 1   2nd accepted number    enternumber(min,127);   please enter integer between 56 , 127: 66   3rd accepted answer.    int max=enternumber(enternumber(32,127),127);    //called twice   please enter integer between 32 , 127: 66   please enter integer between 66 , 127: 77   4th , 5 th accepted answer  

you can correct logic deleting first , third enternumber call shown .

   int enternumber (int lolimit, int hilimit){    int min;    int max;     {       printf("please enter integer between %d , %d:\n", lolimit, hilimit);       scanf("%d", &min);       }    while (min<lolimit || min>hilimit);    return min;   }    int main(){  // enternumber(32,127); comment out   int min=enternumber(32,127);    // enternumber(min,127);    int max=enternumber(min,127);    int no=enternumber(min,max); 

}

first accepts minimum value , maximum , enters no between range.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -