Using sleep() to make program pause...

I'm working on a program (On a unix server via putty, g++ compiler) where I need to attach the time to several pieces of data but currently my program is giving me the same time on each piece of data. I've been trying to use sleep() in my code so it waits for a random number of seconds before it generates a new piece of data but it's still the same. I did #include unistd.h. This is driving me crazy and I would appreciate any help you can give me. Thanks

int seconds;
seconds = rand() % 10 + 1;
time_t rawtime;
time (&rawtime);
for (int i = 0; i < SIZE; i++){
stamped_items[i].data = rand() % 10000 + 1;
stamped_items[i].stamp = (ctime (&rawtime)) ;
sleep(seconds);
}
What about this...

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

#include "time.h"
#include "math.h"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <iostream>
#include <string>

struct stamped_item
{
	int data;
	std::string *stamp;
};

int main(int argc, char* argv[])
{
	int seconds;
	int SIZE = 1000;
	stamped_item stamped_items[SIZE];

	time_t rawtime;
	for (int i = 0; i < SIZE; i++){
		seconds = rand() % 10 + 1;
		stamped_items[i].data = rand() % 10000 + 1;
		std::cout << "Data: " << stamped_items[i].data << std::endl;
		time (&rawtime);
		stamped_items[i].stamp = new std::string(ctime(&rawtime)) ;
		std::cout << "Time: " << *stamped_items[i].stamp << std::endl;
		std::cout << "Waiting " << seconds << " seconds" << std::endl;
		if (i > 0)
			std::cout << "Previous time: " << *stamped_items[i-1].stamp << std::endl;
		sleep(seconds);
	}
	for(int i = 0; i < SIZE; i++) {
		delete stamped_items[i].stamp;
	}
}


Edit: Fixed major bug...
Last edited on
Ummm....I have my whole program arranged differently so I'm not sure what would apply to mine. I'll just throw the whole thing on here.....do you see where I'm going wrong?


#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
#include <unistd.h>
#include <string>
#include <stdio.h>
#include <math.h>
#include <time.h>
using namespace std;
/*
*/
const int SIZE = 10;
struct entry {
unsigned int data; //random gen. data
string stamp; //time of generation
};
class timestamp {
private:
entry stamped_items[SIZE]; //array of time & data
public:
void breakTime(unsigned int seconds){
clock_t temp;
temp = clock () + seconds * CLOCKS_PER_SEC ;
while (clock() < temp) {}
}
void generate();
void listAll();
void search(unsigned int target,int action);
void compare(int position);
};
int main() {
timestamp A;
int readint();
unsigned int readTarget();
int action;
unsigned int target;
srand(((unsigned int)time(0)));
A.generate();
do {
A.listAll();
cout << endl << "MENU ACTIONS: " << endl
<< " 1. Query data items" << endl
<< " 2. List items older than a specified target"
<< endl << " 3. Exit" << endl << endl
<< "Please select an action: ";

if ((action == 1)||(action == 2)){
cout << endl << endl << "Enter a target data item: ";
target = readTarget();
A.search(target, action);
}
else
;
} while (action != 3);
}
//
void timestamp :: generate(){
unsigned int seconds;
seconds = (rand() % 3 + 1);
time_t rawtime;
time (&rawtime);
for (int i = 0; i < SIZE; i++){
stamped_items[i].data = rand() % 10000 + 1;
stamped_items[i].stamp = (ctime (&rawtime)) ;
breakTime(1);
}
}
//
void timestamp :: listAll(){
for (int i = 0; i < SIZE; i++){
cout << stamped_items[i].data <<" "
<< stamped_items[i].stamp <<" "
<< endl;
}
}
//
void timestamp :: search(unsigned int target, int action) {
//
//
bool found;
int position;
found = false;
position = 0;
while((!found)&&(position < SIZE)){
if ((stamped_items[position].data == target))
found = true;
else
position++;
}
if (found){
cout << endl << "Your target data is "
<< stamped_items[position].data
<< " and was created at: "
<< stamped_items[position].stamp << endl;
if (action == 2)
compare(position);
else
;
}
else {
cout << endl << "The target data was NOT FOUND."
<< endl << endl;
}
}
//
void timestamp :: compare(int position) {
//
//
cout << endl << endl;
if (position != 0) {
cout << "The data items older than "
<< "your target are: ";
for (int i = 0; i < position ; i++){
cout << stamped_items[i].data << " "
<< stamped_items[i].stamp << " "
<< endl;
}
}
else
cout << "Your target data is the oldest item found.";
}
//
int readint() {
//
//
int val; //user input
//Checks input and prompts user to enter different value if
// an invalid menu choice was entered
cin >> val;
while (!(cin.good())){
cin.clear();
cin.ignore(80, ’\n’);
cout << "Response must be a whole number between 1 and"
<< " 3, try again: ";
cin >> val;
}
return val;
}
//
unsigned int readTarget() {
//
//
unsigned int val;
//Checks input and prompts user to enter different value if
// an invalid menu choice was entered
cin >> val;
while (!(cin.good())){
cin.clear();
cin.ignore(80, ’\n’);
cout << "Target must be a whole number"
<< ", try again: ";
cin >> val;
}
return val;
}
P.S. Please ignore the "breakTime" part, I pasted this while I was testing something else, I had sleep there before....
Okay. I am reposting your code here, and enclosing them in code tags so we get line numbers:

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
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
#include <unistd.h>
#include <string>
#include <stdio.h>
#include <math.h>
#include <time.h>
using namespace std;
/*
*/
const int SIZE = 10;
struct entry {
	unsigned int data; //random gen. data
	string stamp; //time of generation
};

class timestamp {
private:
	entry stamped_items[SIZE]; //array of time & data
public:
	void breakTime(unsigned int seconds){
		clock_t temp;
		temp = clock () + seconds * CLOCKS_PER_SEC ;
		while (clock() < temp) {}
	}
	void generate();
	void listAll();
	void search(unsigned int target,int action);
	void compare(int position);
};

int main() {
	timestamp A;
	int readint();
	unsigned int readTarget();
	int action;
	unsigned int target;
	srand(((unsigned int)time(0)));
	A.generate();
	do {
		A.listAll();
		cout << endl << "MENU ACTIONS: " << endl
				<< " 1. Query data items" << endl
				<< " 2. List items older than a specified target"
				<< endl << " 3. Exit" << endl << endl
				<< "Please select an action: ";

		if ((action == 1)||(action == 2)){
			cout << endl << endl << "Enter a target data item: ";
			target = readTarget();
			A.search(target, action);
		}
		else
			;
	} while (action != 3);
}
//
void timestamp :: generate(){
	unsigned int seconds;
	seconds = (rand() % 3 + 1);
	time_t rawtime;
	time (&rawtime);
	for (int i = 0; i < SIZE; i++){
		stamped_items[i].data = rand() % 10000 + 1;
		stamped_items[i].stamp = (ctime (&rawtime)) ;
		breakTime(1);
	}
}
//
void timestamp :: listAll(){
	for (int i = 0; i < SIZE; i++){
		cout << stamped_items[i].data <<" "
				<< stamped_items[i].stamp <<" "
				<< endl;
	}
}
//
void timestamp :: search(unsigned int target, int action) {
//
//
	bool found;
	int position;
	found = false;
	position = 0;
	while((!found)&&(position < SIZE)){
		if ((stamped_items[position].data == target))
			found = true;
		else
			position++;
	}
	if (found){
		cout << endl << "Your target data is "
				<< stamped_items[position].data
				<< " and was created at: "
				<< stamped_items[position].stamp << endl;
		if (action == 2)
			compare(position);
		else
			;
	}
	else {
		cout << endl << "The target data was NOT FOUND."
				<< endl << endl;
	}
}
//
void timestamp :: compare(int position) {
//
//
	cout << endl << endl;
	if (position != 0) {
		cout << "The data items older than "
				<< "your target are: ";
		for (int i = 0; i < position ; i++){
			cout << stamped_items[i].data << " "
					<< stamped_items[i].stamp << " "
					<< endl;
		}
	}
	else
		cout << "Your target data is the oldest item found.";
}
//
int readint() {
//
//
	int val; //user input
	//Checks input and prompts user to enter different value if
	// an invalid menu choice was entered
	cin >> val;
	while (!(cin.good())){
		cin.clear();
		//cin.ignore(80, ’\n’);
		cout << "Response must be a whole number between 1 and"
				<< " 3, try again: ";
		cin >> val;
	}
	return val;
}
//
unsigned int readTarget() {
//
//
	unsigned int val;
	//Checks input and prompts user to enter different value if
	// an invalid menu choice was entered
	cin >> val;
	while (!(cin.good())){
		cin.clear();
		//cin.ignore(80, ’\n’);
		cout << "Target must be a whole number"
				<< ", try again: ";
		cin >> val;
	}
	return val;
}


Take a look at line 62. You are setting the number of seconds to wait there to a random number. Let's say that becomes a 4.

Then on line 64, you are getting the time, and putting it in "rawtime".

Line 66 assigns a random value to the "data".
Line 67 takes the time from line 64 and puts it in "stamp".

This is in a loop. So while "data" is constantly changing, the "stamp" will remain the same, since rawtime is never reset.

I believe your goal is to get a new "time" each time. If that is the case, move time(&rawtime) to the inside of your "for" loop.

If you wish, you could do this:

1
2
3
4
5
6
7
8
9
10
11
void timestamp :: generate(){
	unsigned int seconds;
	seconds = (rand() % 3 + 1);
	time_t rawtime;
	for (int i = 0; i < SIZE; i++){
		stamped_items[i].data = rand() % 10000 + 1;
		time(&rawtime);
		stamped_items[i].stamp = ctime(&rawtime) ;
		sleep(seconds);
	}
}


You may wish to be getting a new random time each time. If that is the case, you'll have to get a new random time in each loop too.


1
2
3
4
5
6
7
8
9
10
11
void timestamp :: generate(){
	unsigned int seconds;
	time_t rawtime;
	for (int i = 0; i < SIZE; i++){
		stamped_items[i].data = rand() % 10000 + 1;
		time(&rawtime);
		stamped_items[i].stamp = ctime(&rawtime) ;
	        seconds = (rand() % 3 + 1);
		sleep(seconds);
	}
}


I hope this gets you closer to your goal. Also if you surround your source code in "code" tags, it makes it easier to use. ["code"] before the code, and ["/code"] after the code puts them in neat little sections and preserves indentation. Do not put quotes in the brackets as I did, however.
YES!! Thank you so much! I can't believe I forgot that the second's random # would need to be in the loop to refresh...wow, it always is something so simple that ends up getting me...
I'm glad you figured it out. It may be of benefit to learn the ins and outs of your debugger. I spend more time in that than I do my editor by far. Watch variables. Watch memory addresses. I learn a lot by doing that. Don't get frustrated. Revel in the fact you figured it out.
Topic archived. No new replies allowed.