0

I am creating an RPG with an in built clock, incrementing after every four moves. The clock is accessible by typing "time" to display the game time. I've also put in "sleep" which increments game time by 4 hours (time and sleep as inputs will not increment game time). It all works as it should with no problems.

My variable, int hours, is a global variable but is not used by main, however when I move this variable into my function, void dayNight (),(making it a local variable here) my function then fails to increment? Could someone please explain why this is?

Also some of my other global variables are used by both main and a function, but as this game will be incorporating a lot more functions, I am concerned about the amount of global variables that I will possibly end up with. Is there a way not to have all my variables global, such as, declare them in main and then use a pointer and/or an ampersand to adjust the variable from the function if that's how it works? (Or vice versa). Should my variables be stored in main or the function if both adjust it or have I got to keep them global? As you can probably gather I'm not to sure on how pointers exactly work as yet and have no idea how I would implement it in my code below. It took me ages but I beleive the code below works exactly how it is meant to, (well it does for me but you may find I have overlooked somethink?

//n, s, e & w movement routine is in another function that works but not included here!
//to shorten my code (to my current problem).
#include <iostream>

void dayNight ();
int movesReset = 4;

int moves = 0;
std::string move;

int main(){

std::cout << "Press any key to move, or \"quit\" to quit!" << std::endl;
std::cout << "\"time\" displays the time, (incements every " << movesReset << " moves)." << std::endl;
std::cout << "\"sleep\" is to sleep jumping time by four hour periods." << std::endl;

while (1){
        dayNight ();
        std::cin >> move;
        moves++;
        if (move == "quit"){return 0;} 
        }
system ("Pause");
return 0;
}

void dayNight (){
int hours = 12;
int timer = 0;

    if (move == "sleep") {moves--;}        //removes mains increment when moves is "sleep"

    if (move == "sleep")  
    { hours += 4; timer = hours; std::cout << "You sleep for 4 hours!" << std::endl;}
    
    if (hours <= 12)  {timer = hours;}
        else if (hours > 12 && hours <= 24)  {timer = hours -12;}
        else if (hours == 25)  {hours = 1; timer = 1;}
        else if (hours == 26)  {hours = 2; timer = 2;}
        else if (hours == 27)  {hours = 3; timer = 3;}
        else if (hours == 28)  {hours = 4; timer = 4;}
        
    if (moves ==1 && hours == 7)  {std::cout << "1 Hour to Dawn!" << std::endl;}
        else if (moves ==1 && hours == 8)  {std::cout << "It's Daylight!" << std::endl;}
        else if (moves ==1 && hours == 19)  {std::cout << "It's Dusk!" << std::endl;}
        else if (moves ==1 && hours == 20)  {std::cout << "Darkness falls!" << std::endl;}
    
    if (move == "time") {moves--;}
    
    if (move == "time" && hours <= 12) {std::cout << timer << ":AM " <<std::endl;}
        else if (move == "time" && hours > 12) {std::cout << timer << ":PM " << std::endl;}

    if (moves == movesReset)  {hours++; moves = 0;}

}

Thanks in advance for any help with this, Leppie.

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Sky Diploma
0

>>My variable, int hours, is a global variable but is not used by main, however when I move this variable into my function, void dayNight (),(making it a local variable here) my function then fails to increment? Could someone please explain why this is?

This is a question about Scope and Lifetime of a variable. You could think of it like this . Basically with all local variables, the variable is created when the function is called and the flow of execution reaches that declaration syntax. Secondly it will be destroyed when the function call routine is complete.

Therefore, everytime the function is called your variable int hours is created in the start with the initial value of '12' and then destroyed at the end of the call, there by storing nothing.

if you want the value of the variable to be stored even after the function call you should take a look at static variables.

void dayNight()
{
static int hours=12;
.....

Now if you edit the value of 12, it will still remain with the value it was lately assigned.

AS with your second question . It is a simple matter of how you would like to code when it comes to choosing a separate mechanism. I mean, you could write the program with all global variables or pass them around using pointers. Although it is very hard to manage Global variables when you start taking the game to the n-th level.

However good programming practices would encourage you to learn more about pointers and references as they could be helpful as well. So its all upto you !

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.