Made a timer but need tons of them

Ok so im makeing a time management game and you buy trucks and planes and send them off to different parts of the world and it takes real time so i made a timer to keep tract of stuff and every vehicle will have its own timer but i want to know how do i create multiple timers without copying and pasting in the code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <iostream>
#include <string>
#include <fstream>
#include <time.h>
#include <cstdlib>
#include <windows.h>

/*

int x = 800;
int *pointer = &x;

cout << *pointer << endl;

*/


using namespace std;

void timer(int &Sec, int &Min, int &Hou, int &Day)
{
    while(true)
    {
        system("cls");

        cout << "Seconds: " << Sec << endl;
        cout << "Minutes: " << Min << endl;
        cout << "Hours: " << Hou << endl;
        cout << "Days: " << Day << endl;

        for(int i = 0; i < 1; i++)
        {
            Sec++;
        }

        if(Sec == 60)
        {
            Sec = 0;
            Min++;
        }

        if(Min == 60)
        {
            Min = 0;
            Hou++;
        }

        if(Hou == 24)
        {
            Hou = 0;
            Day++;
        }
        Sleep(1000);
    }
}

void Load(string &PN, int &M, int &XP, int &L)
{
    ifstream File_In;

    File_In.open("WMSF.txt");

    getline(File_In, PN);
    File_In >> M;
    File_In >> XP;
    File_In >> L;
}

void Save(string &PN, int &M, int &XP, int &L)
{
    ofstream File_Out;

    File_Out.open("WMSF.txt");

    File_Out << PN << endl;
    File_Out << M << endl;
    File_Out << XP << endl;
    File_Out << L << endl;
}

void stats(string &PN, int &M, int &XP, int &L)
{
    cout << "Your Stats\n" << endl;

    cout << "Name: " << PN << endl;
    cout << "Money $" << M << endl;
    cout << "Experience: " << XP << endl;
    cout << "Level: " << L << endl;
}

void Game(string &PN, int &M, int &XP, int &L)
{
    int choice = 0;
    int choice2 = 0;

    int r;
    srand(time(0));

    r = rand() % 15;

    cout << "MENU\n" << endl;

    cout << "1) Ship products" << endl;
    cout << "2) Stats" << endl;
    cin >> choice;

    if(choice == 1)
    {
        cout << "where are you shipping to" << endl;
        cout << "1) Chicago" << endl;
        cout << "2) Los Angeles" << endl;
        cout << "3) Tokyo" << endl;
        cin >> choice2;

        switch(choice2)
        {
            case 1:
                cout << "Delivering to chicago" << endl;

        }
    }
    if(choice == 2)
    {
        stats(PN, M, XP, L);
    }
}

int main()
{
    int choice = 0;
    int money = 0;
    int xp = 0;
    int level = 1;
    string Player_Name;

    int seconds = 0;
    int minutes = 0;
    int hours = 0;
    int days = 0;

    timer(seconds, minutes, hours, days);

    cout << "Warehouse Manager\n" << endl;

    cout << "1) New Game" << endl;
    cout << "2) Load Game" << endl;
    cin >> choice;

    cin.ignore(200, '\n');

    if(choice == 1)
    {
        cout << "Welcome, what is your name?" << endl;
        getline(cin, Player_Name);

        cout << "\nOk " << Player_Name << " Lets get some info before we start\n" << endl;
        cout << money << endl;

        money += 300;

        Save(Player_Name, money, xp, level);
        Game(Player_Name, money, xp, level);
    }
    if(choice == 2)
    {
        Load(Player_Name, money, xp, level);
        Game(Player_Name, money, xp, level);
    }
}
closed account (3qX21hU5)
Use a class that is in charge of managing all your timers. Also I would recommend you look into the time header for a better timer, and look into using classes.
Last edited on
but the player will be allowed to have an unlimited number of vehicles so how would that work? I dont know how to set that up in a class anyways.
Last edited on
Oh and im facing another problem, my timer is not running unless im in the function, can i make it run while im in other functions without making it global?
closed account (3qX21hU5)
Yes that is why I am telling you to make a timer class, and a timer manager class and to look into the time header (time.h).

That way you can have multiple timers running in the background.


Here is some links that might help

http://www.cplusplus.com/reference/ctime/

http://www.cplusplus.com/reference/ctime/clock/

http://www.cplusplus.com/reference/ctime/tm/

http://www.cplusplus.com/reference/ctime/time/
Ok im going to be honest here, i looked at that documentation and am not any further along with my project, I need to see examples, maybe someone could start me off with a shell of a timer class and see if i can fill in the rest, i have a learning disability so it takes me a while to grasp something.
bump
Take a look at the example for
http://www.cplusplus.com/reference/ctime/clock/

clock_t t;
makes a timer object.

t = clock();
sets it to the current time.

elapsedtime = clock() - t;
gives you the time that has passed

Play with the example on that page to get to grips with it.
You don't need multiple timers, but you do need classes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Clock {
private:
  unsigned int time_in_seconds;

public:
  Clock(void) : time_in_seconds(0) {}
  Clock(unsigned int seconds) : time_in_seconds(seconds) {}

  void increase(void) {
    time_in_seconds++;
  }
  unsigned int time(void) {
    return time_in_seconds;
  }
};


Your shipments should be classes too:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Shipment {
private:
  unsigned int shipping_date;
  unsigned int distance_in_seconds;
  bool         arrived;
  unsigned int recieved_date;
public:
  Shipment(unsigned int date, unsigned int dist_seconds)
    :shipping_date(date), distance_in_seconds(dist_seconds)
  ,arrived(false), recieved_date(0)
  {}
  bool hasArrived(unsigned int time_in_seconds) {
    if (arrived) return true;

    if (time_in_seconds < shipping_date + distance_in_seconds)
      return false;

    recieved_date = time_in_seconds;
    arrived = true;
    return true;
  }
  unsigned int time_recieved(void){
    return recieved_date;
  }
};


To use these classes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main(void)
{
  // Create the clock
  Clock myClock;

  // Make some shipments
  Shipment myFirstShipment(myClock.time(), 5);
  Shipment mySecondShipment(myClock.time(), 7);

  // Pass the time
  while (!myFirstShipment.hasArrived(myClock.time())
      || !mySecondShipment.hasArrived(myClock.time()))
  {
    myClock.increase();
  }

  // Results
  std::cout << "F Shipment @ " << myFirstShipment.time_recieved() << '\n';
  std::cout << "S Shipment @ " << mySecondShipment.time_recieved() << '\n';

  return 0;
}
Last edited on
Ok now in my game there will be tons of different vehicles all with their own timers and stuff, this code will be able to handle that?
closed account (3qX21hU5)
That means you don't understand what it does. I would recommend you figure out exactly what his classes do before you use them :)
Vehicles don't have timers. They have times they expect to arrive, and given a time, know if they have arrived or not.
Ok well I guess i need to understand the clock function and time stuff before i continue, is there any videos or really good tutorials about that stuff anywhere besides the ones on this site?
Ok here is my clock class and im having trouble with it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#ifndef CLOCK_H_INCLUDED
#define CLOCK_H_INCLUDED

class Clock
{
    public:
        Clock();
        void Timer();
        void Save();
        void Load();
        void Game();

    private:
        int seconds;
        int minutes;
        int hours;
        int days;
};

Clock::Clock()
{
    seconds = 0;
    minutes = 0;
    hours = 0;
    days = 0;
}

void Clock::Timer()
{
    while(true)
    {
        system("cls");

        /* Debug
        cout << "Seconds: " << Sec << endl;
        cout << "Minutes: " << Min << endl;
        cout << "Hours: " << Hou << endl;
        cout << "Days: " << Day << endl;
        */

        for(int i = 0; i < 1; i++)
        {
            seconds++;
        }

        if(seconds == 60)
        {
            seconds = 0;
            minutes++;
        }

        if(minutes == 60)
        {
            minutes = 0;
            hours++;
        }

        if(hours == 24)
        {
            hours = 0;
            days++;
        }
        Sleep(1000);
    }
}


#endif // CLOCK_H_INCLUDED 



main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <iostream>
#include <string>
#include <fstream>
#include <time.h>
#include <cstdlib>
#include <windows.h>

#include "Clock.h"

/*

int x = 800;
int *pointer = &x;

cout << *pointer << endl;

*/


using namespace std;

void timer(int &Sec, int &Min, int &Hou, int &Day)
{

}

void Clock::Load
(
    string &PN, long unsigned int &M, int &XP, int &L
)
{
    ifstream File_In;

    File_In.open("WMSF.txt");

    getline(File_In, PN);
    File_In >> M;
    File_In >> XP;
    File_In >> L;
    File_In >> Sec;
    File_In >> Min;
    File_In >> Hou;
    File_In >> Day;
}

void Clock::Save
(
    string &PN, long unsigned int &M, int &XP, int &L
)
{
    ofstream File_Out;

    File_Out.open("WMSF.txt");

    File_Out << PN << endl;
    File_Out << M << endl;
    File_Out << XP << endl;
    File_Out << L << endl;
    File_Out << Sec << endl;
    File_Out << Min << endl;
    File_Out << Hou << endl;
    File_Out << Day << endl;
}

void stats
(
    string &PN, long unsigned int &M, int &XP, int &L
)
{
    cout << "Your Stats\n" << endl;

    cout << "Name: " << PN << endl;
    cout << "Money $" << M << endl;
    cout << "Experience: " << XP << endl;
    cout << "Level: " << L << endl;
}

void Clock::Game
(
    string &PN, long unsigned int &M, int &XP, int &L
)
{
    int choice = 0;
    int choice2 = 0;

    int r;
    int r2;
    srand(time(0));

    r = rand() % 15;
    r2 = rand() % 3;

        //Debug
        cout << "Seconds: " << Sec << endl;
        cout << "Minutes: " << Min << endl;
        cout << "Hours: " << Hou << endl;
        cout << "Days: " << Day << endl;
        cout << "\n" << endl;
        //Debug End

    cout << "MENU\n" << endl;

    cout << "1) Ship products" << endl;
    cout << "2) Stats" << endl;
    cout << "3) Save" << endl;
    cin >> choice;

    if(choice == 1)
    {
        cout << "where are you shipping to" << endl;

        cin >> choice2;

        switch(choice2)
        {
            case 1:
                cout << "Delivering to" << endl;

        }
    }
    if(choice == 2)
    {
        stats(PN, M, XP, L);
    }
    if(choice == 3)
    {
        Save(PN, M, XP, L, Sec, Min, Hou, Day);
    }
}

int main()
{
    int choice = 0;
    long unsigned int money = 0;
    int xp = 0;
    int level = 0;
    string Player_Name;

    Clock C;

    //Timer Debug
    //timer(seconds, minutes, hours, days);

    cout << "Warehouse Manager\n" << endl;

    cout << "1) New Game" << endl;
    cout << "2) Load Game" << endl;
    cin >> choice;

    cin.ignore(200, '\n');

    if(choice == 1)
    {
        cout << "Welcome, what is your name?" << endl;
        getline(cin, Player_Name);

        cout << "\nOk " << Player_Name << " Lets get some info before we start\n" << endl;

        money += 30000;
        level++;

        C.Save(Player_Name, money, xp, level, C.seconds, C.minutes, C.hours, C.days);
        C.Game(Player_Name, money, xp, level, C.seconds, C.minutes, C.hours, C.days);
    }
    if(choice == 2)
    {
        C.Load(Player_Name, money, xp, level, C.seconds, C.minutes, C.hours, C.days);
        C.Game(Player_Name, money, xp, level, C.seconds, C.minutes, C.hours, C.days);
    }
}
errors

C:\Users\Chay\Desktop\Shipping Manager\main.cpp|27|error: prototype for 'void Clock::Load(std::string&, long unsigned int&, int&, int&)' does not match any in class 'Clock'|
C:\Users\Chay\Desktop\Shipping Manager\Clock.h|10|error: candidate is: void Clock::Load()|
C:\Users\Chay\Desktop\Shipping Manager\main.cpp|46|error: prototype for 'void Clock::Save(std::string&, long unsigned int&, int&, int&)' does not match any in class 'Clock'|
C:\Users\Chay\Desktop\Shipping Manager\Clock.h|9|error: candidate is: void Clock::Save()|
C:\Users\Chay\Desktop\Shipping Manager\main.cpp|78|error: prototype for 'void Clock::Game(std::string&, long unsigned int&, int&, int&)' does not match any in class 'Clock'|
C:\Users\Chay\Desktop\Shipping Manager\Clock.h|11|error: candidate is: void Clock::Game()|
C:\Users\Chay\Desktop\Shipping Manager\Clock.h||In function 'int main()':|
C:\Users\Chay\Desktop\Shipping Manager\Clock.h|14|error: 'int Clock::seconds' is private|
C:\Users\Chay\Desktop\Shipping Manager\main.cpp|162|error: within this context|
C:\Users\Chay\Desktop\Shipping Manager\Clock.h|15|error: 'int Clock::minutes' is private|
C:\Users\Chay\Desktop\Shipping Manager\main.cpp|162|error: within this context|
C:\Users\Chay\Desktop\Shipping Manager\Clock.h|16|error: 'int Clock::hours' is private|
C:\Users\Chay\Desktop\Shipping Manager\main.cpp|162|error: within this context|
C:\Users\Chay\Desktop\Shipping Manager\Clock.h|17|error: 'int Clock::days' is private|
C:\Users\Chay\Desktop\Shipping Manager\main.cpp|162|error: within this context|
C:\Users\Chay\Desktop\Shipping Manager\main.cpp|162|error: no matching function for call to 'Clock::Save(std::string&, long unsigned int&, int&, int&, int&, int&, int&, int&)'|
C:\Users\Chay\Desktop\Shipping Manager\Clock.h|9|note: candidates are: void Clock::Save()|
C:\Users\Chay\Desktop\Shipping Manager\Clock.h|14|error: 'int Clock::seconds' is private|
C:\Users\Chay\Desktop\Shipping Manager\main.cpp|163|error: within this context|
C:\Users\Chay\Desktop\Shipping Manager\Clock.h|15|error: 'int Clock::minutes' is private|
C:\Users\Chay\Desktop\Shipping Manager\main.cpp|163|error: within this context|
C:\Users\Chay\Desktop\Shipping Manager\Clock.h|16|error: 'int Clock::hours' is private|
C:\Users\Chay\Desktop\Shipping Manager\main.cpp|163|error: within this context|
C:\Users\Chay\Desktop\Shipping Manager\Clock.h|17|error: 'int Clock::days' is private|
C:\Users\Chay\Desktop\Shipping Manager\main.cpp|163|error: within this context|
C:\Users\Chay\Desktop\Shipping Manager\main.cpp|163|error: no matching function for call to 'Clock::Game(std::string&, long unsigned int&, int&, int&, int&, int&, int&, int&)'|
C:\Users\Chay\Desktop\Shipping Manager\Clock.h|11|note: candidates are: void Clock::Game()|
C:\Users\Chay\Desktop\Shipping Manager\Clock.h|14|error: 'int Clock::seconds' is private|
C:\Users\Chay\Desktop\Shipping Manager\main.cpp|167|error: within this context|
C:\Users\Chay\Desktop\Shipping Manager\Clock.h|15|error: 'int Clock::minutes' is private|
C:\Users\Chay\Desktop\Shipping Manager\main.cpp|167|error: within this context|
C:\Users\Chay\Desktop\Shipping Manager\Clock.h|16|error: 'int Clock::hours' is private|
C:\Users\Chay\Desktop\Shipping Manager\main.cpp|167|error: within this context|
C:\Users\Chay\Desktop\Shipping Manager\Clock.h|17|error: 'int Clock::days' is private|
C:\Users\Chay\Desktop\Shipping Manager\main.cpp|167|error: within this context|
C:\Users\Chay\Desktop\Shipping Manager\main.cpp|167|error: no matching function for call to 'Clock::Load(std::string&, long unsigned int&, int&, int&, int&, int&, int&, int&)'|
C:\Users\Chay\Desktop\Shipping Manager\Clock.h|10|note: candidates are: void Clock::Load()|
C:\Users\Chay\Desktop\Shipping Manager\Clock.h|14|error: 'int Clock::seconds' is private|
C:\Users\Chay\Desktop\Shipping Manager\main.cpp|168|error: within this context|
C:\Users\Chay\Desktop\Shipping Manager\Clock.h|15|error: 'int Clock::minutes' is private|
C:\Users\Chay\Desktop\Shipping Manager\main.cpp|168|error: within this context|
C:\Users\Chay\Desktop\Shipping Manager\Clock.h|16|error: 'int Clock::hours' is private|
C:\Users\Chay\Desktop\Shipping Manager\main.cpp|168|error: within this context|
C:\Users\Chay\Desktop\Shipping Manager\Clock.h|17|error: 'int Clock::days' is private|
C:\Users\Chay\Desktop\Shipping Manager\main.cpp|168|error: within this context|
C:\Users\Chay\Desktop\Shipping Manager\main.cpp|168|error: no matching function for call to 'Clock::Game(std::string&, long unsigned int&, int&, int&, int&, int&, int&, int&)'|
C:\Users\Chay\Desktop\Shipping Manager\Clock.h|11|note: candidates are: void Clock::Game()|
||=== Build finished: 42 errors, 0 warnings ===|
Topic archived. No new replies allowed.