1

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();

}
Seb
  • 1,966
  • 2
  • 17
  • 32

3 Answers3

4

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

 system("PAUSE");

after

theGame.displayWord();

And you will see your cout display.

Also, there are some optimization and errors with your code.

  1. You are missing a return value from main.
  2. For setWord you should pass by const reference, so the function would be.

void setWord( const string& word )

  1. For getWord you should return by const reference, so the function would be

string getWord()

For more information on passing by const reference, please take a look at Passing arguments by reference.

josephthomas
  • 3,256
  • 15
  • 20
  • You don't need a return value, as it defaults to 0 automatically [see here](http://stackoverflow.com/questions/204476/what-should-main-return-in-c-c). – Jesse Good Apr 13 '12 at 05:07
2

In Visual Studio if you right-click on the project then go to Properties->Linker->System->SubSystem, you can set it to Console so it will not exit immediately and prevents you from having to use System("pause"). System("pause") is a Windows thing and prevents portability.

keelerjr12
  • 1,693
  • 2
  • 19
  • 35
  • This is fine for his own testing purposes but will not help the end-user if they attempt to load the program in a manner which results in the console being closed when the program terminates. See my answer for a solution which resolves that problem whilst retaining portability. – JBentley Apr 13 '12 at 05:36
  • 2
    @JonBentley: that's not a problem, if the program is executed from a real console, then the console will not close as soon as the program ends. So it *is* strictly a developer issue. – Matthieu M. Apr 13 '12 at 06:31
  • @Matthieu How do you know the user will run it from a console? Many unsophisticated Windows users will probably just try to double click on your program, and then wonder why it "flashes" and disappears. – JBentley Dec 13 '12 at 21:42
  • @JonBentley: I am confused, are we not talking about developers here ? – Matthieu M. Dec 14 '12 at 07:27
  • @matthieu No, the question doesn't limit itself to just developers - it's entirely possible his code could eventually target an end user, or that it won't, but he will eventually want to know how to solve this problem in that context. – JBentley Dec 15 '12 at 01:08
0

Other answers have already suggested changing your IDE properties to prevent the console exiting immediately, or using system("PAUSE"); you can also simply load your own console, and run the executable manually from there (which is neither IDE nor platform dependent).

Ultimately however, your don't know what environment your user will be working from or how they will be loading the program, so a more suitable solution would be to implement something yourself which prevents the program from exiting until you are sure the user is done reading the output. For example:

WordGame theGame;
bool exit = false
while (!exit)
{
    cout << "Enter a word. Entering \"exit\" will terminate the program." << endl; 
    string aWord; 
    cin >> aWord;
    if (aWord == "exit") exit = true;
    else
    {
        theGame.setWord(aWord); 
        theGame.displayWord(); 
    }
}
JBentley
  • 6,099
  • 5
  • 37
  • 72
  • 1
    +1, a nicer solution than `system("PAUSE")`. But you could just write `while (true) {... if (aWord == "exit") break; }` – Alex Z Apr 13 '12 at 05:36
  • 1
    That is true, and I did consider a version involving break for simplicity, but break is generally considered bad style (it's pretty much just a particular type of goto statement), so I opted for a controlling variable instead. – JBentley Apr 13 '12 at 05:45
  • Fair enough, each to his own I guess. – Alex Z Apr 13 '12 at 06:02
  • @JonBentley: *`break` is generally considered bad style* => Uh ? Actually, **not** using `break` is considered bad style in all the good C++ code bases I have seen. This is FUD. – Matthieu M. Apr 13 '12 at 06:34
  • Obviously opinions will differ, but there is a a large school of thought which discourages the use of goto-like statements in all but simple cases, and with plenty of good reasons (albeit it there are plenty of people who disagree with those reasons). Note that I used the word 'generally' - there are cases (such as the simple loop above) where it doesn't matter, but the OP is a self-professed newbie, so I felt it best not to suggest the use of a goto-like statement at a point in his studies when he won't be able to appreciate the pros/cons in a way that a more experienced programmer would. – JBentley Apr 13 '12 at 12:44