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.