Question about arrays

Oct 2, 2008 at 9:03pm
So I read the tutorial on Arrays, and I found out you could do:

 
int billy [5] = { 16, 2, 77, 40, 12071 }; 


But I was wondering, I have something set up like this:

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>
using namespace std;

string space[9];  //setting a global array

int main()
{
                  //coding
}


And is there a way to set all nine spots at once in the main, yet still have it set global and not constant?

Maybe something like this?

 
space[9] = { "1", "2", "3", "4", "5", "6", "7", "8", "9" }


I'm basically trying to shorten this:

1
2
3
4
5
6
7
8
9
space[1] = "1";
space[2] = "2";
space[3] = "3";
space[4] = "4";
space[5] = "5";
space[6] = "6";
space[7] = "7";
space[8] = "8";
space[9] = "9";


(I know I could use integer for the array, but I'm also setting it as "X" and "O", so that's why it's a string.)



Thanks

-MountainDew
Last edited on Oct 2, 2008 at 10:01pm
Oct 3, 2008 at 12:11am
1st of all, the array space starts from 0 ( space[0] ) and ends at 8 ( space[8] );
and you can give them values like you said, but starting from space[0] and ending at space [8], or you can enter them from your keyboard, by using

1
2
3
4
for(int i=0; i < the number you have between[ ] when you set the array; i++)
{
cin>>space[i];
}


I hope it helps :).
Last edited on Oct 3, 2008 at 12:13am
Oct 3, 2008 at 1:06am
You can't cin>> a string...you have to use getline(). However, I would suggest you just make your array an array of chars since you only have having 1 character per unit. And yes, array space[9] allocates space from [0]-[8] and if you access above that, bad things happen...
Oct 3, 2008 at 2:02am
You can cin a string, however, it won't catch any whitespace.

Duh.

............................................________
....................................,.-‘”...................``~.,
.............................,.-”...................................“-.,
.........................,/...............................................”:,
.....................,?......................................................\,---It's not a trap...
.................../...........................................................,}
................./......................................................,:`^`..}
.............../...................................................,:”........./
..............?.....__.........................................:`.........../
............./__.(.....“~-,_..............................,:`........../
.........../(_....”~,_........“~,_....................,:`........_/
..........{.._$;_......”=,_.......“-,_.......,.-~-,},.~”;/....}
...........((.....*~_.......”=-._......“;,,./`..../”............../
...,,,___.\`~,......“~.,....................`.....}............../
............(....`=-,,.......`........................(......;_,,-”
............/.`~,......`-...............................\....../\
.............\`~.*-,.....................................|,./.....\,__
,,_..........}.>-._\...................................|..............`=~-,
.....`=~-,_\_......`\,.................................\
...................`=~-,,.\,...............................\
................................`:,,...........................`\..............__
.....................................`=-,...................,%`>--==``
........................................_\..........._,-%.......`\
...................................,<`.._|_,-&``................`\

j/k.

BTW, helios r0xx0rz for making/finding this.
Last edited on Oct 3, 2008 at 8:11pm
Oct 3, 2008 at 2:05am
Gah! So all this time I've got in wrong -_-

But yeah, that image owns.
Oct 3, 2008 at 6:28pm
Dear Sir,
When we declare global variable it does not mean that variable is constant,it simply means that if value of that variable is changed in the program then after that the changed value is used not the earlier value.Moreover in string data type array will be initialized as:
space[9] = { "harry", "tom", "jenny", "doly", "diana", "jhon",
"adam", "anthony", "michel" }
Hope this will help you.
With Regards,
Matanuragi
Topic archived. No new replies allowed.