Joe J.

asked • 07/11/17

having trouble with some very simple C code.

Hello everyone, this is my code. It's meant to simply get input from the user about whether or not they have passed and then print the corresponding result. Problem is, when I type F the code runs, but executes this line printf("Enter result (P/F)\n"); twice after completing the first loop. 

int main(){
int attempts=0;
char result;

while(attempts < 5){
printf("\n");
printf("Entered Loop\n");
printf("Enter result (P/F)\n");
scanf("%c", &result);
if(result == 'P'){
printf("Passed\n");
attempts++;
break;
}
else if(result == 'F'){
printf("Entered F Loop\n");
printf("Try again\n");
attempts++;
continue;
}
printf("Exited F Loop\n");
}

if(attempts == 5){
printf("QUIT!\n");
}
}

1 Expert Answer

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.