Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

beginner-a confusing while loop, for me

Status
Not open for further replies.

KPlateo

Programmer
Mar 14, 2001
6
US
Could you please help with this, a while loop that doesnt
step out of the loop, how do i do what i intended that is
if a number other than 1-3 is inputed continue with the loop
i am using djgpp on windows 98.

while ( (place_f < 0) || (place_f >3 ) ) /* place_f is */
{ /* initialised */
puts(&quot;Enter sqaure value 1-3\n&quot;); /* to 0. */
scanf(&quot;%d&quot;, &place_f );
}
return 0;
}
 
If you initialized place_f by 0 this programm will not go to you loop.
Initialize place_f by -1;
int place_f = -1;
Regards.
 
As Lim mentioned, make sure you initialize place_f to something less than 0 or greater than 3, otherwise you won't get into the loop.

I tested it with place_f initialized to 1 and it worked fine as long as I typed in valid integers, but would infinitely loop if I typed something like &quot;ldkjfaldfkjsd&quot; in. scanf() is a tricky function to use correctly, you might want to use fgets() to grab a string from the user and then parse it with strtol().

Russ
bobbitts@hotmail.com
 
I would advise you to make do { } while() ; loop (ie, bottom tested loop) . Because at least once you want the user to input the data. Otherwise you will have to initialize the variable to enter the loop.

For e.g,

do {
puts(&quot;Enter sqaure value 1-3\n&quot;);
scanf(&quot;%d&quot;, &place_f );
} while ( (place_f < 0) || (place_f >3 ) ) ;

Even if you want to use while() {} loop. Then you must initialize the variable place_f to any value other than 0 to 3 .



Siddhartha Singh
siddhu_singh@bharatmail.com
siddhu.freehosting.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top