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!
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!
Of course like this?
But insteed of using the built in arraymechanism use the STL-component vector.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; }
Only variables of the same datatype.I was wondering if you can store any variable in an array
01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
Good things donīt come easy in life!!!
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.
those are both character wtrings, for an array:
multi-valent arrays (I believe thats the correct term) look like this: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";
int mvarray[1][2];