Thread: 'overwriting arrays' question

  1. #1
    Registered User
    Join Date
    Jan 2005

    'overwriting arrays' question

    Lets say I wrote in a function in my program where if you select a name to delete (the name is set to: NameArray[x]). Now what I want to do is.. if that name is selected for deletion, that NameArray[x] to be renamed as '<space>' (to replace with a space). What 'method' can I use to do this. I already tried
    NameArray={' '}
    and I get a syntax error. Please provide any information to resolve this issue, any help will be appreciated. Thanks.

  2. #2
    Registered User dan20n's Avatar
    Join Date
    Jan 2005
    On the right path, you need to specify what element in the array you want to 'delete'

    array[i] = " ";
    not at a computer where i can test, silly though i can not remember if the { } are needed, just give it a test.

    EDIT: just looking over your post you had the answer within
    Last edited by dan20n; 04-04-2005 at 08:58 PM.
    Meg: Everybody! Guess what I am?
    Stewie: Hm, the end result of a drunken back-seat grope-fest and a broken prophylactic?

  3. #3
    Registered User
    Join Date
    Apr 2003
    Here is another example of how arrays work:
    string Names[] = {"tom", "dick", "harry"}; //initializer list
    cout<<Names[0]<<endl<<endl;; //tom
    Names[2] = "alice";
    for(int i=0; i<3; i++)
    Names[1] = " "; 
    cout<<Names[1]<<endl; //<a space is displayed>
    1)You can also make that array value nothing--not even a space--by doing this:

    Names[1] = "";

    2)The initializer list:

    {"tom", "dick", "harry"}

    gets its name from the fact that you can only use it to 'initialize' an array. Initializing an array means you assign it some values as at the same time that you declare the array. You can't use an initializer list after you declare an array. For instance, this doesn't work:
    string Names[3];
    Names = {"tom", "dick", "harry"};
    Last edited by 7stud; 04-04-2005 at 10:08 PM.

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    >>the name is set to: NameArray[x]
    Are you using an array of std::string or are you using an array of char[]?

    If std::string, then 7stud's solution (names[x] = "") will probably be the best to clear a string; if it's char[], then you can do:

    names[x][0] = '\0';

    Basically that just sets the null-terminating character at the first element of the 'string', effectively making the rest of it invisible to any function that cares.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question about arrays
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 11-08-2006, 03:09 AM
  2. A question concerning character arrays
    By ellipses in forum C Programming
    Replies: 3
    Last Post: 03-08-2005, 08:24 PM
  3. basic question to Arrays
    By doneirik in forum C++ Programming
    Replies: 1
    Last Post: 01-25-2005, 02:57 AM
  4. Question about char arrays
    By PJYelton in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2003, 12:44 AM
  5. Question about arrays?? PLease read..
    By foofoo in forum C Programming
    Replies: 3
    Last Post: 06-24-2002, 02:40 PM