getchar()

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2008
Posts: 5
Reputation: tufzz is an unknown quantity at this point 
Solved Threads: 0
tufzz's Avatar
tufzz tufzz is offline Offline
Newbie Poster

getchar()

 
0
  #1
Oct 18th, 2008
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. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: getchar()

 
0
  #2
Oct 18th, 2008
> 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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 148
Reputation: devnar will become famous soon enough devnar will become famous soon enough 
Solved Threads: 16
devnar's Avatar
devnar devnar is offline Offline
Junior Poster

Re: getchar()

 
0
  #3
Oct 18th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 31
grumpier grumpier is offline Offline
Posting Whiz in Training

Re: getchar()

 
0
  #4
Oct 18th, 2008
Originally Posted by devnar View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 148
Reputation: devnar will become famous soon enough devnar will become famous soon enough 
Solved Threads: 16
devnar's Avatar
devnar devnar is offline Offline
Junior Poster

Re: getchar()

 
1
  #5
Oct 18th, 2008
>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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: getchar()

 
0
  #6
Oct 18th, 2008
It should be like this (yeah, I like writing nonesence):
  1. int i = 0;
  2. while ((string[i++] = getchar())!='\n');
  3. string[i]='\0';
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 60
Reputation: HiHe is an unknown quantity at this point 
Solved Threads: 4
HiHe's Avatar
HiHe HiHe is offline Offline
Junior Poster in Training

Re: getchar()

 
0
  #7
Oct 18th, 2008
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. }
In C you can code our own bugs, in C++ you can inherit them.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: getchar()

 
0
  #8
Oct 18th, 2008
> 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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 113
Reputation: ahamed101 is an unknown quantity at this point 
Solved Threads: 14
ahamed101's Avatar
ahamed101 ahamed101 is offline Offline
Junior Poster

Re: getchar()

 
0
  #9
Oct 18th, 2008
Just modifying Sci@phy's code...

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

This will do the trick...
regards,
Ahamed.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 113
Reputation: ahamed101 is an unknown quantity at this point 
Solved Threads: 14
ahamed101's Avatar
ahamed101 ahamed101 is offline Offline
Junior Poster

Re: getchar()

 
0
  #10
Oct 18th, 2008
Originally Posted by tufzz View 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. }
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...

Originally Posted by Salem View Post
> 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...

Originally Posted by HiHe View Post
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';
regards,
Ahamed.
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 2578 | Replies: 16
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC