Greets,
A string array in C++ can be initialized, just like everything else.
With that in mind, how do you count the number of items in the array?
I mean, how many rows?
Let me give you an example using Char arrays (having two dimensional since it's not like string)
char arrChar[][10] = {
"Anagram",
"Book",
"Computer",
"Dimension",
"Epic",
"Fail",
"Gyroscope",
"Hemingway",
"Irish"
};
//Amount of rows initialized:
int Rows = sizeof(arrChar)/10;
Rows is 9, right, now to do this with strings, it's a bit different.
Since a string is null-terminated I approached the problem like so:
string arrString[] = {
"I Like Turtles",
"Icecream",
"Legendary stuff",
"French Fries"
};
int GetStringRows(){
int rowcount;
for(int i = 0; !arrString[i].empty(); i++) rowcount++;
return rowcount
}
This works, but I don't know how reliable this technique is, because after creating another string array, for some reason it adds all strings together and returns the wrong value.
And here is the actual code:
int CountStructure(string *structure){
int counter = 0;
for(int i = 0; !structure[i].empty(); i++)counter++;
return counter;
}
int main(){
string menu[] = {
"---------------------------",
"- New Game -",
"---------------------------",
};
string map[] = {
"---------------------------",
"- -",
"- -",
"- -",
"- -",
"---------------------------",
};
cout<<CountStructure(menu)<<endl;
cout<<CountStructure(map)<<endl;
return R_OK;
}
Output:
3
9