Thread: Inputting with SPACES...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    25

    Inputting with SPACES...

    I can't seem to successfully output characters with spaces. I inputted using getchar().
    Any other ideas?
    Thank you in advance!

    Code:
    printf ("Enter the Title [Use dots instead of spaces]: ");
          int i = 0;
          int c1;
          while (( c1!= '\n')&& (i <21))
          {
          new_name->title[i] = c1;
          i++;                
          c1= (char)getchar();
          }
          new_name->title[i] = '\0';
          while  (( c1= (char) getchar()) != '\n');
    OutPut:
    Code:
    void list_names (void)
    /* List all names in the book */
    {
        REMINDER *tmp_ptr; /* Traverses list */
        printf ("All names in address book:\n");
        tmp_ptr= hol;
    
        while (tmp_ptr != NULL)
         {
     printf ("title: %s\n",tmp_ptr->title);
    }
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
          int c1;
          while (( c1!= '\n')&& (i <21))
    You're declaring c1 uninitialized and then using its garbage value as the while loop condition. This is a "bad thing". And even if c1 doesn't by chance start off with '\n' as the value, what's new_name->title[0] going to end up as?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    25
    Quote Originally Posted by itsme86 View Post
    You're declaring c1 uninitialized and then using its garbage value as the while loop condition. This is a "bad thing". And even if c1 doesn't by chance start off with '\n' as the value, what's new_name->title[0] going to end up as?
    I see. So would initializing c1 to zero do the trick?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No. Your list_names function doesn't actually change your pointer. It just loops over and over using the same pointer.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by Nephilim View Post
    I see. So would initializing c1 to zero do the trick?
    Only if you want new_name->title[0] to always equal zero. You should be using a do/while loop here because you want to perform an action before you check the condition.
    bit∙hub [bit-huhb] n. A source and destination for information.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by bithub View Post
    Only if you want new_name->title[0] to always equal zero. You should be using a do/while loop here because you want to perform an action before you check the condition.
    I don't think a do while loop is the answer, unless he wants to store garbage in the first spot. A while loop would work fine, but he needs to read the first character before entering the loop body:
    Code:
    while ((c1 = getchar()) != '\n' && i < 21)
    {
        new_name->title[i++] = c1;  // this increments i after assigning to title
    }
    or use fgets:
    Code:
    fgets(new_name->title, 21, stdin);
    // fgets stores the newline, so we will trim it if it's there
    if (new_name->title[strlen(new_name->title)] == '\n') {
        new_name->title[strlen(new_name->title) = '\0';
    }

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    25
    I'll try that, thanks. I'll post back.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    25
    Works like a charm Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 45
    Last Post: 08-10-2010, 12:41 PM
  2. Replies: 7
    Last Post: 10-03-2009, 10:58 PM
  3. Inputting to an array.
    By mintsmike in forum C++ Programming
    Replies: 3
    Last Post: 03-27-2009, 09:26 AM
  4. why its not inputting as before..
    By transgalactic2 in forum C Programming
    Replies: 5
    Last Post: 01-26-2009, 06:11 AM
  5. inputting the date using C
    By jeunefemme in forum C Programming
    Replies: 13
    Last Post: 01-26-2003, 06:10 PM

Tags for this Thread