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.