Thread: Getchar

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    37

    Getchar

    Okay, so the word search didn't go over well. Fine.

    I was hoping someone could identify what the problem is here: I can't get out of the while loop, even when I simply press enter on the command line (which is the new character, it should take me out of the loop). Any ideas?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    
    {
        char *string=(char*)malloc(sizeof(char)*100);
        char x=getchar();
      while(x!='\n')
      {
      *string=x;
      string=string+1;
      char x=getchar();
      }
      *string='\0';
      printf("%s",string);
    }

  2. #2
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    Quote Originally Posted by CProgramming11 View Post
    Okay, so the word search didn't go over well. Fine.

    I was hoping someone could identify what the problem is here: I can't get out of the while loop, even when I simply press enter on the command line (which is the new character, it should take me out of the loop). Any ideas?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    
    {
        char *string=(char*)malloc(sizeof(char)*100);
        char x=getchar();
      while(x!='\n')
      {
      *string=x;
      string=string+1;
      char x=getchar();
      }
      *string='\0';
      printf("%s",string);
    }
    First things first :-) You're declaring main an int (as you should be) where
    is main's() return value?

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    37
    Right, I sometimes forget about the return value when I'm experimenting with simple code like this.

    I found some errors, I declared x twice for some reason. I modified the code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(void)
    
    {
        char *string=(char*)malloc(sizeof(char)*100);
        char x;
        x=getchar();
      while(x!='\n')
      {
      *string=x;
      string=string+1;
      x=getchar();
      }
      *string='\0';
      printf("%s",string);
      return 0;
    }
    Now the problem is, at the end of the first getchar() the program just returns 0 regardless of which character I enter.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    17
    I'm not sure what you mean by returning 0, but I assume you mean that you aren't seeing what you typed as output to the terminal.

    In this case, you may want to look at what you are doing to your string char pointer.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    37
    Ah so stupid, I added another string point to point to the original address of the string.

    Problem solved, thanks.

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    17
    You aren't stupid, people get tripped on that all the time.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    74
    don't change your string pointer.

    this: string=string+1;
    you loose the beginning of your string.

    Try this instead: string[i] = getchar();
    (you would need an i variable that gets incremented in each loop)

  8. #8
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,671
    And in C you should never cast the return value of malloc.

  9. #9
    Registered User l2krence's Avatar
    Join Date
    Nov 2010
    Posts
    24
    so well, one more question extra added by me {
    printf("What does getchar() stands for // represents of what? How to use it? My lecturer didnt teach this to me and i saw a lot of ppl using it and thats where the lecturer didnt even mention! ");

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by l2krence View Post
    so well, one more question extra added by me {
    printf("What does getchar() stands for // represents of what? How to use it? My lecturer didnt teach this to me and i saw a lot of ppl using it and thats where the lecturer didnt even mention! ");
    Did you try looking up getchar() in your C library documentation?

    // signals a short (one line) comment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I don't understand "while (getchar() != '\n');"
    By valedor in forum C++ Programming
    Replies: 13
    Last Post: 09-08-2009, 05:12 PM
  2. getchar() problem
    By jlharrison in forum C Programming
    Replies: 6
    Last Post: 01-25-2006, 02:49 PM
  3. getchar() problem from K&R book
    By anemicrose in forum C Programming
    Replies: 13
    Last Post: 04-04-2004, 11:06 PM
  4. help with getchar lol
    By Taco Grande in forum C Programming
    Replies: 5
    Last Post: 03-18-2003, 09:25 PM
  5. Can anybody take a look at this?
    By TerryBogard in forum C Programming
    Replies: 10
    Last Post: 11-21-2002, 01:11 PM