0

I am making fairly new to C++ and I am using it to make a text based game for a school project. However during the first section of the game the player answers questions by entering the number shown beside the answer they choose. However when I tested the variables the input going to using std::cout they return different values depending on where they are outputted. If I outputted them in the class I am using to set them (Introduction) the they return the correct value such as 1 or 3 etc. However when I output them in any file other than Introduction.cpp, the value displayed is -858993460 for all of the values. I get the same result from Main.cpp when I call them in my main function and if I call them from another function in a different class to Introduction.

This is an example of some of the code used to get input from the user:

void Introduction::CharacterCreation()
{
    Universal universal;

    std::fstream creation("Introduction_CharacterCreation.txt");
    universal.line = 5;

    for (int i = 0; i < universal.line; i++)
    {
        if (i > 0)
        {
            std::getline(creation, universal.displayText);
            std::cout << universal.displayText << std::endl;
        }
        if (i == 4)
        {
            std::cout << std::endl;
            std::cin >> universal.gender;

            while (universal.gender <= 0 || universal.gender >= 3)
            {
                std::cout << "Please make a valid choice" << std::endl;
                std::cin >> universal.gender;
            }
        }
    }
// Code cut out here
}

The gender variable is an int declared in the Universal class, and the user is prompted to enter 1 for male or 2 for female by text pulled from a separate file. If the input is not 1 or 2 then a while loop forces the player to keep re-answering the question until they enter 1 or 2. The line variable is also an int however that is used for the for loops to ensure the right lines are read by the program.

To output the gender variable this is the code I use:

std::cout << gender << std::endl;

There is no universal. as it is being called within the Universal class itself.

This has confused me massively and I can't get my head around what is causing the problem. Any help or explanation would be great, thanks in advance.

  • 1
    The `universal` variable in that function gets destroyed after this function runs. Since you're not showing how you're getting it out of there, we can't guess exactly what you're doing wrong - possibly just manipulating a completely unrelated variable in the code "outside". – Mat Aug 12 '15 at 19:43
  • It sounds like you are trying to use `universal` after it has gone out of scope (which is why you are seeing values like -858993460). You are creating it on the stack and so its scope is local to that function. – trooper Aug 12 '15 at 19:48

1 Answers1

1

Short answer: you're declaring a Universal object in your CharacterCreation() method. When this function exits since the scope of the universal variable was local so the entire object is destroyed.

Whatever you are outputting is just uninitialized garbage. It could really be any number depending on what system is compiling / running the program. To test this right after you input the gender, while still inside the function, try running

std::cout << universal.gender << std::endl;

This should output just fine.

There are a lot of ways you can go about fixing this. Since you didn't post where you call this method or your Universal class I can't say for sure. But I can speculate one such solution which is to declare the Universal object outside the method and then pass it in as a parameter:

Universal universal = Universal();
Introduction::CharacterCreation(universal);
std::cout << universal.gender << std::endl;

And just declare your function header to accept a Universal object:

void Introduction::CharacterCreation(Universal & universal)
{
//code here
}
amza
  • 780
  • 2
  • 7
  • 32
  • I just took it out, probably a little more in-depth than he needs! Also since it's a text based program I'd be willing to guess it's probably not *that* heavy. – amza Aug 12 '15 at 20:10