Random numbers where variable should be, I have no idea why this is happening.

0 Richard Horton · March 16, 2015
Hello everyone, I am writing a very basic C++ text based game. I understand everything I have used so far, but just to check all the variables were working I decided to print them at the end of the program as it is still quite short I could get away with this.

This is the code for main:

#include "StoryIntroduction.h"
#include
#include

using namespace std;

int main()
{
    StoryIntroduction storyIntro;
    storyIntro.CharacterBackground();

    StoryIntroduction StoryIntro;

    cout


This is the code for the header file:

#ifndef STORYINTRODUCTION_H
#define STORYINTRODUCTION_H

#include

class StoryIntroduction
{
    public:
        std::string playerName;
        bool startAlone;
        int answer1;

        StoryIntroduction();
        void CharacterBackground();
};

#endif // STORYINTRODUCTION_H

This is the code for the .cpp file:

#include "StoryIntroduction.h"
#include
#include

using namespace std;

StoryIntroduction::StoryIntroduction()
{
    //This is the constructor
}


/* The following function goes through the very start of the story which goes through
setting up some of the player's basic information and some of their back story. This
prepares them for the next stage of the introduction allowing them to progress into
the story more easily. */

void StoryIntroduction::CharacterBackground()
{
    StoryIntroduction storyIntro;

    cout

I think it is all correct but I could be wrong as I am new to learning C++ and it is my first programming language.  But this is what is so confusing, when I run the program I follow it through and I believe the variables should be as follows:

answer1 = 1
playerName = Ric
startAlone = true

But when I output at the end, I get the following values:

answer1 = 1989898784
playerName = This one just gives a blank space
startAlone = 204

What is even more confusing is that when I print playerName earlier in the program I get the correct value that I entered using cin but not at the end of the program.

Anyone who can explain or just tell me how to fix it would be greatly appreciated.

Post a Reply

Replies

Oldest  Newest  Rating
0 Jageshwar Sah · March 23, 2015
prototype your class instead of including as header file
  • 1

C++

106,932 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator