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

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -