Hi, I have the next C code: PHP: #include <stdio.h> int main(void) { char test = '\0'; //test the value for ending input int iCount=0; //counter in for loop for(iCount=0; iCount<15 ;iCount++) { printf("\nDo yout want to enter details of a%s car [Y/N]?", iCount!=0?"nother":""); scanf("%c", &test); if(tolower(test)=='n') //testing if n or N was pressed break; printf("\nThis is car nr. %d", iCount); //output a statement in every loop } return 0; } I simplified for better understanding, the output it's a little strange because the testing is made on every second loop. I don't understand why. Please help.
Sorry, i didn't posted my output. Here they are: Do you want to enter details of a car [Y/N]?y This is car nr. 0 Do you want to enter details of another car [Y/N]? This is car nr. 1 Do you want to enter details of another car [Y/N]?y This is car nr. 2 Do you want to enter details of another car [Y/N]? This is car nr. 3 Do you want to enter details of another car [Y/N]?y This is car nr. 4 Do you want to enter details of another car [Y/N]? This is car nr. 5 Do you want to enter details of another car [Y/N]?n i'm expecting to ask in every loop if i want to continue or not.
sir(rakoczimrks) what you have entered source code is absolutely correct but when you are giving input a small problem occur i.e.., after entering input you will press enter key so ,when you are reading for second time it will treat the previous enter as a character and it may print its ascii value or some garbage so please use fflush(stdin) means you are buffering the output from the input device you have given