Endless loop question

S

S.Tobias

I withdraw what I have said above, I might have been wrong, I'm sorry.
As a matter of fact, I'm not yet sure on either side, but it seems
very probable that the "Ick" was valid here.
 
D

Dave Thompson

This has been puzzling me all this week. This is actually a homework
assignment
from last semesmter. But the teacher wouldn't tell us why certain
things didn't work, but it didn't just work. My thing was what
actually turn this while loop into an endless loop instead of waiting
for user response it'll would skip right over it. Could someone with
the time explain this to me what would make it behave like this
int main() {
char again = 'Y';
Aside from your inconsistent and sometimes odd layout, which makes
your code hard(er) to read, so I suggest you work on that:
while (again != 'N')
{

printf("\nEnter degree type: (B for Bachelors), (M for Masters), (D
for Doctorate\n");
printf("\t\t\t: ");
scanf("%1c", &degree);
printf("\nEnter number of years of work experience: ");
scanf("%2d",&wk_exp);
printf("\nEnter current Pay: ");
scanf("%5d",&p_pay);
printf("\nWant to do this again? Press N for NO: ");
/*scanf("%1c", &again); */
getchar();

This reads a character but doesn't do anything with it, in particular
it doesn't store to 'again', so the loop continues. You probably meant

again = getchar();

But once you do that, or if you do the scanf("%1c",&again) instead --
or it could be just %c, 1 is the default width for that specifier --
you will find that the loop _always_ exits because the character you
read is the newline (or perhaps other garbage character) following the
pay value, because the scanf %d above reads only through the end of
the number leaving any other input, in particular the newline,
buffered for later reads. This is (almost) 12.18 in the FAQ, in the
usual places and at http://www.eskimo.com/~scs/C-faq/top.html .

scanf has other "features" that can be problematical as well, (many)
also detailed in that part of the FAQ. Especially if you fail to check
its return value, as you did.

The way of doing input usually recommended here is to read each line
into a buffer with fgets(), and then pick the desired data value(s)
out of it with sscanf() (note the addtional s -- that's scan formatted
_from string_) or sometimes explicit code and/or other functions like
strtol[l](), strtok(), strchr(), str[c]spn(), etc. Even that isn't
perfect, if you (might) get input lines longer than your fixed-size
buffer, but that gets into complications I suggest you leave aside for
now and come back to later when you are a little more experienced.

- David.Thompson1 at worldnet.att.net
 
K

Kevin Bracey

Well, according to 6.7.5.3#14, I think it makes it
pretty clear that int main() semantically acts the same
as int main(void) but as though there was an explicit void.

We've just had this discussion on comp.std.c throughout September. I suggest
you check the thread "main prototype" on Google. I don't feel like going
through it again.
 
O

Old Wolf

Kevin Bracey said:
We've just had this discussion on comp.std.c throughout September. I suggest
you check the thread "main prototype" on Google. I don't feel like going
through it again.

That thread didn't explicitly answer the question of whether
int main() { ... violates 5.1.2.2.1 (the bit that says
main must be either int main(void) or int main(int, char **)
or equivalents). In other words, is a compiler allowed to
reject int main().

The majority view seemed to be that int main() and int main(void)
were equivalent as a part of the function definition, but did
not provide an equivalent prototype. So does it constitute
an "equivalent" as worded in 5.1.2.2.1? (And what's the wording
in C90 and does it comply with that?)
 
D

Douglas A. Gwyn

Old said:
... In other words, is a compiler allowed to
reject int main().

No, conforming compilers accept int main() { whatever }
as a valid function definition. That also qualifies as
one of the two accepted shapes for the main function
invoked to start the program by a hosted implementation.
 

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

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,592
Members
45,103
Latest member
VinaykumarnNevatia
Top