Jump to content
  • Advertisement
Sign in to follow this  
mckoo

getchar() problem ...

This topic is 5227 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

excuse me: while( exit_flag ==0 ) { printf("Enter a line of text "); ch = getchar(); while((ch!='\n') && (char_count<MAXBUFFERSIZE)) { buffer[char_count++] = ch; ch = getchar() } } can u help me explain the statement? does it mean the value of ch has been assigned to buffer[char_count++]? any replies are appreciated

Share this post


Link to post
Share on other sites
Advertisement
Yes...This code gets input from the input stream (ch=getchar) and then stores it in the array ( buffer[char_count++] = ch;).

Share this post


Link to post
Share on other sites
thx in advance, thisisbob~

but why::::::

ch = getchar();
buffer[char_count++] = ch;

is not valid?

Share this post


Link to post
Share on other sites
Quote:
Original post by mckoo
does it mean the value of ch has been assigned to buffer[char_count++]?


Basically what that does is that it assigns a character to your buffer then adds one to the counter after it assigns it. However, if you had something like buffer[++char_count] then it would add one to the counter and then assign it to your char array.

[Edited by - NullPointer33 on August 17, 2004 11:03:34 PM]

Share this post


Link to post
Share on other sites
I'm grateful to all replies, thx a lot~

so that means

buffer[char_count++] ====> get the character and add one to char_count after it is been assigned

buffer[++char_count] ===> adds one to the char_count and then assign the value?

buffer[char_count++] = ch;
ch = getchar();

as ch is not defined....let ch be assigned to buffer first, then the value of ch was been assigned also to buffer[char_count++] = ch; as well?

Share this post


Link to post
Share on other sites
Yes, first you grab a character from the input stream, then you move on to the loop assign a character to the buffer and add one to the counter. Then you grab the next character from the input stream and repeat until the while statement isn't true...Also don't forget to add the terminating character to the string '\0'

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!