Thread: string array assignment problem

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    3

    string array assignment problem

    hey all,
    new to the forum, hope you can help.

    Can anyone tell me why the following code doesn't work:

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        string aName[20];
        int index;
    
        index=1;
        aName[index] = "whatever";
    
        return 0;
    }
    I'm trying to assign a string to the second element of a string array.

    thanks

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Why do you think it doesn't work?
    Devoted my life to programming...

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Add a cout << aName[index] << "\n"; after it to see if it works. It should.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    3
    when i run the code it assigns an empty string ("") to aName[1]. The code will assign the string if index = 0, but not any value higher than 0.

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    3
    OK. I see my problem. The code is working. Its the way values are displayed in a watch window that confused me. When I enter aName[index] in the watch window i see the text. When i enter aName in the watch window I see "". I think the watch window is only displaying aName[0] when i enter aName (which doesn't have a value assigned).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 40
    Last Post: 06-03-2010, 12:45 PM
  2. Replies: 17
    Last Post: 11-22-2008, 03:40 AM
  3. Problem in string assignment
    By Bargi in forum C++ Programming
    Replies: 3
    Last Post: 04-15-2008, 01:28 AM
  4. problem with string and array
    By Methje in forum C++ Programming
    Replies: 9
    Last Post: 01-26-2006, 01:15 PM
  5. Problems with array to string assignment.
    By Tronic in forum C++ Programming
    Replies: 11
    Last Post: 03-15-2004, 09:06 PM