Code:
while((pay = getchar()) != -1){
switch(pay){
case '1': /*find managers weekly salary */
...
break;
case '2': /*Find hourly workers salary */
...
break;
case '3':
...
break;
case '4':
break;
case '\n':
break;
default:
printf("You have entered an incorrect paycode.");
printf("Please enter a correct paycode:\n");
break;
}
printf("Please enter a paycode(Enter -1 to end): ");
}
Well for starters, 'getchar' isn't going to return -1 ever. The 'getchar' function returns a keystroke (not entirely accurate, but close enough). What single key on your keyboard is '-1'?
Ok, now let's examine the loop itself:
Code:
while the key read isn't something
switch this key
lets say we match
oh, there's an enter left because of 'getchar'(enter)
that skips your first scanf call
lets say you have another, so you type 'foo'(enter)
that stores foo
we break from the getchar match
we're out of the switch, but in the loop, so we display
printf("Please enter a paycode(Enter -1 to end): ");
(enter) from the last scanf is here
this runs us through the loop, to the '\n' case
all this does is 'break'
we're out of the switch, but in the loop, so we display
printf("Please enter a paycode(Enter -1 to end): ");
That's the general idea. And there you have it, your message displayed twice, just like I said it would, for the reason I stated.
Quzah.