Thread: How to erase strings

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    34

    How to erase strings

    I've really only programmed with C++ and not C so far, and can't find out how to clear a string in C. I have tired using clear() and erase() but get the following error message:
    "error: request for member ‘erase’ in something not a structure or union"
    error: request for member ‘clear’ in something not a structure or union"

    Can someone please help? Thanks.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    First, why do you want to erase a string?
    Set the first character to '\0'.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    34
    I am writing a simple calculator using GTK and one of the buttons is clear, which is supposed to do just that. I am using strings to get the numbers and also to display the result.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
       char str[80];
    
       strcpy( str, "Hello World" );
       puts( str );
    
       str[0] = '\0';  /* Clear the string. */
       puts( str );
    
       return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM
  5. Replies: 13
    Last Post: 04-11-2002, 08:46 AM