simple question

T

Tony Jo

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");
}
}
 
R

Russell Hanneken

H

herrcho

Russell said:
Tony Jo said:
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
(e-mail address removed)


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

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

is the url valid ?
 
T

Tony Jo

Russell Hanneken said:
Tony Jo said:
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
(e-mail address removed)

Thanks. I totally forgot about that. I've been at my assignment for so long
I totally forgot about clearing the buffer
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,077
Messages
2,570,566
Members
47,202
Latest member
misc.

Latest Threads

Top