-
Multiple messages
This program works okay except it prints the same messages more than once for unmatched letter. Can anyone give a solution to this?The programs very easy....
Code:
#include<stdio.h>
int main(void)
{
int tries = 0;
char pass;
for(tries = 0;tries<=5;tries++)
{
printf("Enter a letter to pass");
pass = getchar();
if(pass == 'b')
{printf("welcome aboard");
break;}
else
{printf("try again \n");}
}
return 0;
}
-
Perhaps add
Code:
else if ( pass == '\n' ) {
// ignore newlines
}
-
I would change the getchar() line to
scanf(" %c",&pass);
Note the space before the %c - that will eliminate your troublesome newline, each time.
-
Well it removed one of the messages...but the first message "Enter a letter to pass" pops up twice as before...
-
I used scanf but the program stops by giving the message "This program has stopped working" message...