Thread: String arrays

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    30

    Question String arrays

    Hi all,
    I am pretty new to c++ programming but fairly good at VB.

    My question is this:-
    I want to create an array of strings such that
    string[1]="sheep"
    string[2]="cows "
    string[3]="pigs " etc etc...(which is easy in VB)

    The problem is that strings themselves are treated as character arrays in c++

    so I need a 2 dimensional array, eg. animals[10][10]

    the problem is I can declare the array, but then I cannot get it to accept any input as strings for example...

    animals[5][5]="A" works and puts the character "A" at 5,5 but

    animals[5][5]="pigs " does not, neither does
    animals[][5]="pigs " or
    animals[5][]="pigs ".

    is there any way of assigning strings directly to arrays like this or will I have to start using pointers?

    Thanks in anticipation

    by the way, this is not homework, I am 42 years old!
    Homer

    D'OH!

    mmmmmmmm... iterations

  2. #2
    Registered User jasrajva's Avatar
    Join Date
    Oct 2001
    Posts
    99
    you'll need to include string.h

    and use strcpy

    eg

    strcpy(animal[5], " pigs ");

    would copy pgs into animal[5] which is a char ptr

    and btw i'm 19
    jv

  3. #3
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    To initialise a character string array, (I'm assuming that's what you want), use this construction...

    char array[10][10] = {"horse", "pig"};
    cout << array[0]<<" "<<array[1]<<endl;
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  4. #4
    Unregistered
    Guest
    Create and initialize an array of character pointers as follows:

    char* cpArray[3] = { "sheep", "cows", "pigs" };

    Doing this will give you:

    cpArray[0] equal to "sheep"
    cpArray[1] equal to "cows"
    cpArray[2] equal to "pigs"

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    If you're using C++, you might want to use the string class, because then you don't need to worry about string lengths.

    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
    string animal[5];
    
    animal[0] = "pig";
    animal[1] = "cows";
    animal[2] = "sheep";
    animal[3] = "horse";
    animal[4] = "4/2/3 legged creature";
    
    cout << "Animals:\n";
    cout << animal[0] << ", " << animal[1] << ", " 
         << animal[2] << ", " << animal[3] << ", and " 
         << animal[4] << ".\n";
    
    return 0;
    }

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    30

    Arrow Thanks.

    Thank everybody, you've been a great help.

    The copying using strcpy() 'sort of' worked but gave a system error message (I think I tried to put too many characters into my array

    The creation of an array of pointers worked well (thanks unregistered) and will give me what I needed.

    I was intrigued by the string class thingie(robwhit), although this didn't work as it was written, it came up with an undeclared variable 'string' error, I think this was because the string class had not yet been defined and I intend to go away and read up about classes.

    Thanks again
    Homer

    D'OH!

    mmmmmmmm... iterations

  7. #7
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Robwhit's example omits the line...

    using namespace std;

    ... after the includes.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  5. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM