944,008 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 6853
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 18th, 2008
0

getchar()

Expand Post »
I've been trying to read characters from the keyboard using 'getchar()' but my code always misses one character. Can anyone tell me why this is

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int main(int argc, char *argv[]) {
  6.  
  7. char string[50];
  8. int i;
  9. printf("Enter a string: ");
  10.  
  11. for (i=0; i < strlen(string); i++){
  12. string[i] = getchar();
  13. }
  14.  
  15. printf("%s", string);
  16. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tufzz is offline Offline
8 posts
since Oct 2008
Oct 18th, 2008
0

Re: getchar()

> for (i=0; i < strlen(string); i++)
You're lucky this works at all.
string starts off uninitialised, so who knows what strlen() on it will return.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Oct 18th, 2008
0

Re: getchar()

string[i]=getchar(); I don't think this will work properly. getchar() expects you to hit enter after the input of each character which means that '\n' will be left in the input buffer which will be read the next time getchar() executes. Which means that each character will alternate with a '\n'.
I might be wrong though. Feel free to correct me.
Reputation Points: 124
Solved Threads: 18
Junior Poster
devnar is offline Offline
148 posts
since Sep 2008
Oct 18th, 2008
0

Re: getchar()

Click to Expand / Collapse  Quote originally posted by devnar ...
string[i]=getchar(); I might be wrong though. Feel free to correct me.
You're wrong. getchar() does not behave in quite the way you described. Consider yourself corrected.

Salem is right. Once you get past the concern he pointed out, there are also additional problems that the code has no error checking, nor does it terminate the input string with a zero byte.
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008
Oct 18th, 2008
1

Re: getchar()

>You're wrong. getchar() does not behave in quite the way you described.

My bad. Did some research on getchar(). Seems getchar() doesn't need enter to be pressed after the input of each character. It can only see the input buffer after hitting the enter. Gotta get my basics straightened.
Reputation Points: 124
Solved Threads: 18
Junior Poster
devnar is offline Offline
148 posts
since Sep 2008
Oct 18th, 2008
0

Re: getchar()

It should be like this (yeah, I like writing nonesence):
  1. int i = 0;
  2. while ((string[i++] = getchar())!='\n');
  3. string[i]='\0';
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Oct 18th, 2008
0

Re: getchar()

You could do it like that:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define SIZE 50
  6.  
  7. int main(int argc, char *argv[]) {
  8.  
  9. char string[SIZE], c;
  10. int i;
  11.  
  12. // just a test
  13. printf("len = %d\n", strlen(string)); // len = 0
  14.  
  15. printf("Enter a string: ");
  16.  
  17. for (i=0; i < SIZE; i++){
  18. c = getchar();
  19. if (c == '\n'){
  20. string[i] = '\0';
  21. break;
  22. }
  23. string[i] = c;
  24. }
  25.  
  26. printf("string entered = %s", string);
  27. }
Reputation Points: 95
Solved Threads: 15
Junior Poster
HiHe is offline Offline
172 posts
since Oct 2008
Oct 18th, 2008
0

Re: getchar()

> printf("len = %d\n", strlen(string)); // len = 0
Nope, read my first post again.
string isn't initialised, so your length is junk.
If you get zero, that just makes you lucky.

The rest is getting better though.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Oct 18th, 2008
0

Re: getchar()

Just modifying Sci@phy's code...

  1. while ((string[i++] = getchar())!='\n' && i < SIZE);
  2. string[i]='\0';

This will do the trick...
Reputation Points: 51
Solved Threads: 14
Junior Poster
ahamed101 is offline Offline
114 posts
since Jul 2008
Oct 18th, 2008
0

Re: getchar()

Click to Expand / Collapse  Quote originally posted by tufzz ...
I've been trying to read characters from the keyboard using 'getchar()' but my code always misses one character. Can anyone tell me why this is

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int main(int argc, char *argv[]) {
  6.  
  7. char string[50];
  8. int i;
  9. printf("Enter a string: ");
  10.  
  11. for (i=0; i < strlen(string); i++){
  12. string[i] = getchar();
  13. }
  14.  
  15. printf("%s", string);
  16. }
This aint right...
  1. for (i=0; i < strlen(string); i++){
  2. string[i] = getchar();
  3. }
strlen(string) will have some random number... you dont want that...

Click to Expand / Collapse  Quote originally posted by Salem ...
> printf("len = %d\n", strlen(string)); // len = 0
Nope, read my first post again.
string isn't initialised, so your length is junk.
If you get zero, that just makes you lucky.

The rest is getting better though.
Salem is right...
String is uninitialised... you get junk for len... for sure...

Click to Expand / Collapse  Quote originally posted by HiHe ...
You could do it like that:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define SIZE 50
  6.  
  7. int main(int argc, char *argv[]) {
  8.  
  9. char string[SIZE], c;
  10. int i;
  11.  
  12. // just a test
  13. printf("len = %d\n", strlen(string)); // len = 0
  14.  
  15. printf("Enter a string: ");
  16.  
  17. for (i=0; i < SIZE; i++){
  18. c = getchar();
  19. if (c == '\n'){
  20. string[i] = '\0';
  21. break;
  22. }
  23. string[i] = c;
  24. }
  25.  
  26. printf("string entered = %s", string);
  27. }
This is good... but it will take one junk character... try executing this code... and its a bit lengthy code for such an read operation...

better way...

  1. while ((string[i++] = getchar())!='\n' && i < SIZE);
  2. string[i]='\0';
Reputation Points: 51
Solved Threads: 14
Junior Poster
ahamed101 is offline Offline
114 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: PROBLEM: getchar() does not work within a function.
Next Thread in C Forum Timeline: pthread





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC