Thread: string trouble

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    31

    string trouble

    when i run this program, it doesnt do the last printf that says 'darn glad to meet you...' it just CR+LF. ive copied this straight from a book, i dont understand why it wont work.

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    #define CR 0x0d
    #define ESC ox1b
    #define TAB 0x09
    #define LF 0x0a
    #define BACKSPACE 0x08
    #define NULL 0
    #define TRUE 1
    #define FALSE 0
    #define LENGTH 15
    
    void input(char *string, int length);
    
    void main()
    {
         char string[LENGTH];
    
         printf("Whats your name?");
         input(string, LENGTH);
         printf("Darn glad to meet you, %s!\n", string);
    }
    
    void input(char *string, int length)
    {
         int done = FALSE;
         int index = 0;
         char ch;
    
         string[0] = NULL;
    
         do
         {
                   ch = getch();
    
                   if( index == length)
                   {
                       switch(ch)
                       {
                                 case CR:
                                      break;
                                 default:
                                         ch = NULL;
                                         break;
                       }
                   }
    
                   switch(ch)
                   {
                             case CR:
                                  putchar(ch);
                                  putchar(LF);
                                  string[index] = NULL;
                                  done = TRUE;
                                  break;
                             case BACKSPACE:
                                  if(index==0)
                                  {
                                              break;
                                  }
                                  else
                                  {
                                      putchar(ch);
                                      putchar(' ');
                                      putchar(ch);
                                      index--;
                                      break;
                                  }
                             case NULL:
                                  break;
                             default:
                                     putchar(ch);
                                     string[index] = ch;
                                     index++;
                                     break;
                                  }
                   }
    
         while(!done);
    }
    thanks to anyone who can help.

  2. #2
    Registered User
    Join Date
    Jan 2006
    Posts
    31
    whoops...messed up with the }'s on the last few lines...
    should be like this

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    #define CR 0x0d
    #define ESC ox1b
    #define TAB 0x09
    #define LF 0x0a
    #define BACKSPACE 0x08
    #define NULL 0
    #define TRUE 1
    #define FALSE 0
    #define LENGTH 15
    
    void input(char *string, int length);
    
    void main()
    {
         char string[LENGTH];
    
         printf("Whats your name?");
         input(string, LENGTH);
         printf("Darn glad to meet you, %s!\n", string);
    }
    
    void input(char *string, int length)
    {
         int done = FALSE;
         int index = 0;
         char ch;
    
         string[0] = NULL;
    
         do
         {
                   ch = getch();
    
                   if( index == length)
                   {
                       switch(ch)
                       {
                                 case CR:
                                      break;
                                 default:
                                         ch = NULL;
                                         break;
                       }
                   }
    
                   switch(ch)
                   {
                             case CR:
                                  putchar(ch);
                                  putchar(LF);
                                  string[index] = NULL;
                                  done = TRUE;
                                  break;
                             case BACKSPACE:
                                  if(index==0)
                                  {
                                              break;
                                  }
                                  else
                                  {
                                      putchar(ch);
                                      putchar(' ');
                                      putchar(ch);
                                      index--;
                                      break;
                                  }
                             case NULL:
                                  break;
                             default:
                                     putchar(ch);
                                     string[index] = ch;
                                     index++;
                                     break;
                   }
         }
         while(!done);
    }

  3. #3
    old man
    Join Date
    Dec 2005
    Posts
    90
    What book did you get that from? The code is awful.

    Anyway, looks like you're not explicitly adding a \0 to terminate the string -- maybe one can rely on an array to be initialized with zeroes, but that's pretty sloppy. The code in general is unnecessarily complicated for what little it does.

    For example, this does the job:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define LENGTH 15
    
    int
    main (void)
    {
      char *p, string[LENGTH];
    
      printf("Whats your name? ");
      fgets (string, LENGTH, stdin);
      if ((p = strchr (string, '\n')) != NULL)
        *p = '\0';
    
      printf("Darn glad to meet you, %s!\n", string);
      return 0;
    }
    I think the best solution to your problem is to get a better book.
    Last edited by eerok; 02-02-2006 at 11:59 PM.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    31
    lol its from C for dummies volume 2. if its not too much trobule, can you explain the if loop? i dont understand it too much :-\

  5. #5
    old man
    Join Date
    Dec 2005
    Posts
    90
    The "if" statement in my example? It just replaces the \n in the input line with a \0 to terminate the string cleanly. (There will be no \n in the buffer if the input exceeds the size of the buffer - 1, though ...)

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    31
    ummm.. i know question is extremely noob, but what does strchr do? i looked on the C programming.com tutorials and couldnt find it. and how does the if loop make when the user presses backspace not use a char in the string?

  7. #7
    old man
    Join Date
    Dec 2005
    Posts
    90
    Quote Originally Posted by chasingxsuns
    what does strchr do? i looked on the C programming.com tutorials and couldnt find it.
    strchr() returns a pointer to the first occurrence of a char in a string, or NULL if the char isn't found. It's a very useful function for parsing text, and it's in the standard C library. You really need a good reference to the standard C library (on my system I'd use "man strchr" to look this up, but I'm on gnu/linux).

    Here's an online reference -- it doesn't cover C99, but it's still good. You can also download a pdf of a fairly recent draft of the standard, which is very handy to have.

    how does the if loop make when the user presses backspace not use a char in the string?
    The system handles that until you hit enter, then fgets() grabs the string. Also, there's no loop, just an "if" statement.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    strchr looks through the string you pass it for the first occurrence of the character you pass it, and return a pointer to that character, or NULL if it's not found.

    So what the if check (it's not a loop) does is get the return value of strchr, assign that to a variable, test that variable to see if it's not NULL. If it isn't NULL, then the body of the if statement dereferences that pointer, and assigns a null character to that spot. Thus, the newline is replaced by a null character, which terminates the string there.

    [edit]
    Hah. I had this typed up, but was distracted. So I come back, referesh the page, ok, no replies. Submit... doh! Beat by seconds.
    [/edit]

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

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    ummm.. i know question is extremely noob, but what does strchr do? i looked on the C programming.com tutorials and couldnt find it.
    You should always try google. The first google hit for "strchr" was this: http://www.cplusplus.com/ref/cstring/strchr.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM