Thread: Storing values in arrays

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    52

    Storing values in arrays

    Hi, I was wondering if it is possible to store strings in a different array element. for example variable[1] = some_string. If that is possible, I was wondering if you can store any variable in an array? Ok thanks!

  2. #2
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Of course like this?
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	string names[] = {"John", "Steve", "Alice"};
    
    	for (int j = 0; j < 3; ++j)
    		cout << names[j] << endl;
    return 0;
    }
    But insteed of using the built in arraymechanism use the STL-component vector.

    I was wondering if you can store any variable in an array
    Only variables of the same datatype.
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    char arrayOfStrings[10][20];
    strcpy(arrayOfStrings[0], "Hello");
    strcpy(arrayOfStrings[1], "world");
    strcpy(arrayOfStrings[2], "my name is");
    //etc.

    or a variant to adding string literals

    char word[20] ;
    cout << "enter a word/phrase of less than 20 characters, including any spaces" << endl;
    cin >> word;
    strcpy(arrayOfStrings[8], word);
    Last edited by elad; 08-25-2003 at 12:05 PM.

  4. #4
    Spaced Cadet
    Join Date
    Aug 2003
    Posts
    110
    those are both character wtrings, for an array:

    Code:
    int numbers[20];
    //all values in array 'numbers' are of int type, and the maximum capacity of the array is 20
    
    numbers[1] = 1;
    numbers[2] = 2;
    //this method gets dull, try something like this
    int i =3;
    while (i < 20) {
    numbers[i] = i;
    i++;
    }
    i = 0;
    //lets output it all
    while (i < 20) {
    i++;
    cout << numbers[i] <<"\n";
    multi-valent arrays (I believe thats the correct term) look like this:
    int mvarray[1][2];

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-05-2009, 05:57 PM
  2. Replies: 5
    Last Post: 06-10-2007, 05:54 AM
  3. Storing a String with values
    By dgibson2004 in forum C Programming
    Replies: 2
    Last Post: 05-10-2006, 11:39 AM
  4. Storing strings in 2d char arrays problem
    By rainmanddw in forum C++ Programming
    Replies: 5
    Last Post: 10-22-2003, 05:41 PM
  5. storing values
    By hen in forum C Programming
    Replies: 4
    Last Post: 07-08-2002, 03:20 PM