Delaying program without using sleep

Ok so im trying to make a timer that i will start counting when then program starts, its use is to tell the player how long they played the game for. I dont want to use windows.h so i want a different way to pause my program for a second exactly so that the time increments correctly but im having a bit of a hard time. Ignore the system("cls"); its only there for debugging purposes, once i get this figured out its gone.

Here is my code, everything im working on is in the timer function
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
//Crypto Guess is based off of Crypto Logic for the Oddessy 2
//Created Tuesday, June 4th 2013 by Chay Hawk
*/

#include <iostream>
#include <string>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#include <fstream>
#include <vector>
#include <time.h>
#include <windows.h>

using namespace std;

string word_list(string RW, string GL)
{
    string store_word;
    srand(time(0));

    ifstream fileIn;
    fileIn.open(GL.c_str());

    if(fileIn.fail())
    {
        cout << "File failed to open." << endl;
    }

    vector<string> words;

    while(!fileIn.eof())
    {
        /*not needed as vectors resize themselves
        if(words.size() == words.capacity())
        {
            words.reserve(words.size() * 3);
        }
        */
        fileIn >> store_word;
        words.push_back(store_word);
    }

    RW = words[rand() % words.size()];

    fileIn.close();

    return RW;
}

void timer()
{
    int second = 0,
        minute = 0,
        hour = 0,
        delay = 0;

    bool timer_loop = true;

    while(timer_loop != false)
    {
        system("cls");

        if(delay == 60)
        {
            second++;
        }

        if(second == 60)
        {
            second = 0;
            delay = 0;
            minute++;
        }

        if(minute == 60)
        {
            minute = 0;
            hour++;
        }

        cout << second << endl;
        cout << minute << endl;
        cout << hour << endl;

        for(int j = 0; j < 61; j++)
        {
            delay++;
        }
    }
}

void save_game(int save_rightGuesses, int save_wrongGuesses, string save_list_type)
{
    time_t current = time(0);
    ofstream fileOut;
    fileOut.open("Scores.txt", fstream::app);

    fileOut << "CRYPTO GUESS SCORES FOR " << ctime(&current) << endl;
    fileOut << "From list: " << save_list_type << endl;
    fileOut << "Right Guesses: " << save_rightGuesses << endl;
    fileOut << "Wrong Guesses: " << save_wrongGuesses << "\n" << endl;

    fileOut.close();
}

int main()
{
    timer();

    srand(time(0));

    string original_word,
           shuffled_word,
           random_word,
           choose_list,
           guess,
           custom_word,
           shuffled_custom_word,
           custom_word_guess,
           test;//change this later

    int guess_number = 1,
        right_guesses = 0,
        wrong_guesses = 0,
        select = 0;

    bool game_loop = true;

    cout << "CRYPTO GUESS" << endl;
    cout << "Version 1.1.1\n" << endl;

    cout << "Do you want to enter words yourself or have the computer give you words?" << endl;
    cout << "1) Enter words myself - NOT WORKING" << endl;
    cout << "2) Have the computer give me words" << endl;
    cin >> select;

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

    cout << "\n";

    if(select == 1)
    {
        cout << "Enter your word" << endl;
        getline(cin, custom_word);

        test = custom_word;
        random_shuffle(shuffled_custom_word.begin(), shuffled_custom_word.end());

        for(int i = 0; i < 25; i++)
        {
            cout << "\n";
        }

        cout << "Your word is: " << test << endl;
        getline(cin, custom_word_guess);
    }

    if(select == 2)
    {
        cout << "Select list" << endl;
        getline(cin, choose_list);

        while(game_loop != false)
        {
            string HWLF = word_list(random_word, choose_list);
            shuffled_word = HWLF;
            random_shuffle(shuffled_word.begin(), shuffled_word.end());//#1

            cout << "----------------------------------------------------" << endl;
            cout << "|| Guess # " << guess_number << " || Right Guesses: " << right_guesses << " || Wrong Guesses: " << wrong_guesses << endl;
            cout << "----------------------------------------------------\n" << endl;

            cout << "Word: " << shuffled_word << endl;

            cout << "\n";

            while(guess != HWLF)
            {
                cout << "Your guess: ";
                cin >> guess;

                if(guess == HWLF)
                {
                    cout << "Is correct!\n" << endl;
                    guess = " ";
                    guess_number++;
                    right_guesses++;
                    break;
                }
                else if(guess == "Quit" || guess == "quit")
                {
                    cout << "Goodbye thanks for playing." << endl;
                    save_game(right_guesses, wrong_guesses, choose_list);
                    game_loop = false;
                    return 0;
                }
                if(guess == "Skip" || guess == "skip")
                {
                    cout << "You skipped the word." << endl;
                    cout << "The correct word was " << HWLF << endl;
                    cout << "\n";
                    wrong_guesses++;
                    break;
                }
                else
                {
                    wrong_guesses++;
                    cout << "Is wrong! Try again\n" << endl;
                }
            }
        }//End of game while loop
    }//End of if statement
}
Last edited on
bump
Have you looked at the references section for <ctime>?

The functions under "Time manipulation" would probably help you calculate the runtime length.

http://www.cplusplus.com/reference/ctime/
I know but i want to make my own timer.
Topic archived. No new replies allowed.