By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,119 Members | 866 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,119 IT Pros & Developers. It's quick & easy.

simple question

P: n/a
I was wondering why the following code causes an infinite loop when i type
in an alphabetic character?

while (bool == 0)
{
if(scanf("%d", &subjectDuration) == 1)
{
if(subjectDuration > 26 || subjectDuration < 0)
{
printf("The number of weeks is invalid,");
printf("please try again.\n");
}
else
bool = 1;
}
else
{
printf("You have entered an invalid character,");
printf("please try again.\n");
}
}
Nov 13 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Tony Jo" <to*****@bigpond.com> wrote in message
news:pK*******************@news-server.bigpond.net.au...

I was wondering why the following code causes an infinite loop when i type
in an alphabetic character?

while (bool == 0)
{
if(scanf("%d", &subjectDuration) == 1)
{ [ . . . ] }
else
{
printf("You have entered an invalid character,");
printf("please try again.\n");
}
}


This is addressed in the FAQ:

http://www.eskimo.com/~scs/C-faq/q12.19.html

See also

http://www.comeaucomputing.com/techtalk/#flushinput

Regards,

Russell Hanneken
rh*******@pobox.com
Nov 13 '05 #2

P: n/a

"Russell Hanneken" <rh*******@pobox.com> wrote in message
news:BZ***************@newsread3.news.pas.earthlin k.net...
"Tony Jo" <to*****@bigpond.com> wrote in message
news:pK*******************@news-server.bigpond.net.au...

I was wondering why the following code causes an infinite loop when i type in an alphabetic character?

while (bool == 0)
{
if(scanf("%d", &subjectDuration) == 1)
{

[ . . . ]
}
else
{
printf("You have entered an invalid character,");
printf("please try again.\n");
}
}


This is addressed in the FAQ:

http://www.eskimo.com/~scs/C-faq/q12.19.html

See also

http://www.comeaucomputing.com/techtalk/#flushinput

Regards,

Russell Hanneken
rh*******@pobox.com


Thanks. I totally forgot about that. I've been at my assignment for so long
I totally forgot about clearing the buffer
Nov 13 '05 #3

P: n/a
Russell Hanneken wrote:
"Tony Jo" <to*****@bigpond.com> wrote in message
news:pK*******************@news-server.bigpond.net.au...

I was wondering why the following code causes an infinite loop when
i type in an alphabetic character?

while (bool == 0)
{
if(scanf("%d", &subjectDuration) == 1)
{

[ . . . ]
}
else
{
printf("You have entered an invalid character,");
printf("please try again.\n");
}
}


This is addressed in the FAQ:

http://www.eskimo.com/~scs/C-faq/q12.19.html

See also

http://www.comeaucomputing.com/techtalk/#flushinput

Regards,

Russell Hanneken
rh*******@pobox.com

http://www.eskimo.com/~scs/C-faq/q12.19.html

i can't connect the above.. nor www.eskimo.com

is the url valid ?
Nov 13 '05 #4

P: n/a
herrcho wrote:
http://www.eskimo.com/~scs/C-faq/q12.19.html

i can't connect the above.. nor www.eskimo.com
No problems here.

is the url valid ?


Yes, it's fine. Why not re-try?

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.