It’s only running main, the output “Enter a word” but completely ignores the objects/class
I’m a newb, sorry if this is an inappropriately easy question.
This happens on both release and debug mode
#include <iostream>
using namespace std;
class WordGame
{
public:
void setWord( string word )
{
theWord = word;
}
string getWord()
{
return theWord;
}
void displayWord()
{
cout << "Your word is " << getWord() << endl;
}
private:
string theWord;
};
int main()
{
cout << "Enter a word" << endl;
string aWord;
WordGame theGame;
cin >> aWord;
theGame.setWord(aWord);
theGame.displayWord();
}
You need to enter a word and then press enter. You say “And it quits the program, nothing happens”, but something does happen. It just happens so fast you probably do see it happening and the program closes. If you are in debug mode and want to have a “press key to exit message” then do
after
And you will see your
coutdisplay.Also, there are some optimization and errors with your code.
main.setWordyou should pass by const reference, so the function would be.void setWord( const string& word )getWordyou should return by const reference, so the function would bestring getWord()For more information on passing by const reference, please take a look at Passing arguments by reference.