string array in a class help.

im making a hangman game but the problem i have right now is that i can't declair my string inside of my class :(
here is what im using inside of the public part of the class
string words[10] = { "program", "networking", "software", "torrent", "java", "ubuntu", "windows", "processor", "motherboard", "hardware"};
i could put it in main but i would like it to be in the class.
Define it in your class's constructor:

1
2
3
4
5
6
7
8
9
class MyClass
{
    string mystr;
    MyClass() //this is a constructor, it takes the name of the class, and it gets executed when the class's object instance is created
    {
        mystr = "whatever you want here";
    }
};
thanks :) i have initialized it now but how do i use the string now in my main.
i have this class
1
2
3
4
5
6
7
class hangman{
private:
public:
		hangman(){ 
		string words [10] = {"program", "networking", "software", "torrent", "java", "ubuntu", "windows", "processor", "motherboard", "hardware"};
	};
};



in my main i also have
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main(){
	hangman object;
	char playagain;
	srand(time(NULL));

	do{
		system("CLS");
		int a = rand() % 10 + 1;
		cout << object.words[a]; //how do i use the words string now??
		cout << "Do you want to play again? Y/N ";
		cin >> playagain;
		}while(playagain == 'Y' || playagain == 'y');
	system("PAUSE");
	return 0;
}
One method:


In .h file:
1
2
3
4
5
6
7
8
9
class GameManager
{
public:
      // ....

private:
     static std::string wordPool[] ;
     static unsigned wordsInPool ;
};


in .cpp file:
1
2
3
4
5
6
7
8
std::string GameManager::wordPool[] =
{
     "program", "networking", "software", "torrent", 
     "java", "ubuntu", "windows", "processor", 
     "motherboard", "hardware"
};

unsigned GameManager::wordsInPool = sizeof(GameManager::wordPool) / sizeof(GameManager::wordPool[0]) ;


snig, my friend, you have to read more about classes. I can't give you a tutorial on how to use classes. The way you use it depends very much on what you wanna do. Please read the tutorial of this website about classes:

http://www.cplusplus.com/doc/tutorial/classes/

Good luck! and if you have any specific questions, we're all glad to help!
Topic archived. No new replies allowed.