objective c - Fgets isn't getting any input from stdin or stdin isn't being equaled to the input? -
example code:
char cnumb1[2]; nslog(@"do have account already?(1 yes, 0 no)"); int c; //flushes input while((c = getchar()) != '\n' && c != eof); fgets(cnumb1, sizeof cnumb1, stdin); size_t length = strlen(cnumb1); if (cnumb1 [length-1] == '\n'){ // in case input string has 1 character plus '\n' cnumb1 [length-1] = '\0';} // plus '\0', '\n' isn't added , if condition false. nsstring* string = [nsstring stringwithutf8string: cnumb1]; nslog(@"string:%@", string);
with input "0" (edit: reason, need press enter twice), output "string:" far can tell, stdin isn't being set correctly. also, started without flush lines, caused lots of problems because have more fgets later on , stdin initial input, added those.
why isn't input being set stdin? or problem entirely?
the line
while((c = char()) != '\n' && c != eof);
needs go directly after fgets, not before.
also, while not answering question directly, better do
fgets(cnumb1, sizeof cnumb1, stdin);
then hardcode in sizeof cnumb1, @h2co3 kindly pointed out me.
Comments
Post a Comment