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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
#include <iostream> //For input, output funcitons...
#include <stdlib.h> //For rand function
#include <iomanip> //for setw function
#include <fstream> //for file related functions...
#include <stdio.h>
#include <string.h>
#include <time.h>
using namespace std;
double random(double minprice, double maxprice);
string printRandomString(int n);
bool coinToss();
int clicks();
int runme();
int createfiles(const char *);
void LoadArray(const char *);
struct things{
int id;
string name;
string category;
double price;
bool two_day_shipping;
int numclicked;
} thingDetails[1000];
int main()
{
srand(time(NULL));
for(int z=0; z<3; z++)
{
char filename[20];
cout << "Please enter a filename for file #" << z << " :" ;
cin >> filename;
strcat(filename,".txt");
createfiles(filename);
}
}
int createfiles(const char *filename)
{
ofstream fileout; //open file
fileout.open(filename); //convert open file to output stream....
bool two_day_shipping;
fileout << left << setw(7) << "id"
<< left << setw(15) << "name"
<< right << setw(15)<< "price"
<< " "
<< right << setw(15)<< "category"
<< " "
<< left << setw(15) << "2D ship"
<< left << setw(15) << "number of clicks" << endl;
for(int j=0; j<1000; j++)
{
string category;
double price;
int id;
int click;
int cat;
cat = rand() % 6 +1;
double minprice;
double maxprice;
fileout << fixed << setprecision(2);
id = j;
int n;
string name;
switch (cat){
case 1:
category = "Electronics";
minprice = 35;
maxprice = 1500;
price = random (minprice,maxprice);
n= 5 + (rand()%4);
name = printRandomString(n);
two_day_shipping= coinToss();
click = clicks();
fileout << left << setw(7) << id
<< left << setw(15) << name
<< right << setw(15)<< price
<< " "
<< right << setw(15)<< category
<< " "
<< left << setw(15) << two_day_shipping
<< left << setw(15) << click << endl;
break;
case 2:
category = "Clothing";
minprice = 45;
maxprice = 550;
price = random (minprice,maxprice);
n= 5 + (rand()%4);
name = printRandomString(n);
two_day_shipping= coinToss();
click = clicks();
fileout << left << setw(7) << id
<< left << setw(15) << name
<< right << setw(15)<< price
<< " "
<< right << setw(15)<< category
<< " "
<< left << setw(15) << two_day_shipping
<< left << setw(15) << click << endl;
break;
case 3:
category = "Footwear";
minprice = 99;
maxprice = 399;
price = random (minprice,maxprice);
n= 5 + (rand()%4);
name = printRandomString(n);
two_day_shipping= coinToss();
click = clicks();
fileout << left << setw(7) << id
<< left << setw(15) << name
<< right << setw(15)<< price
<< " "
<< right << setw(15)<< category
<< " "
<< left << setw(15) << two_day_shipping
<< left << setw(15) << click << endl;
break;
case 4:
category = "Books";
minprice = 7;
maxprice = 45;
price = random (minprice,maxprice);
n= 5 + (rand()%4);
name = printRandomString(n);
two_day_shipping= coinToss();
click = clicks();
fileout << left << setw(7) << id
<< left << setw(15) << name
<< right << setw(15)<< price
<< " "
<< right << setw(15)<< category
<< " "
<< left << setw(15) << two_day_shipping
<< left << setw(15) << click << endl;
break;
case 5:
category = "Appliances";
minprice = 150;
maxprice = 600;
price = random (minprice,maxprice);
n= 5 + (rand()%4);
name = printRandomString(n);
two_day_shipping= coinToss();
click = clicks();
fileout << left << setw(7) << id
<< left << setw(15) << name
<< right << setw(15)<< price
<< " "
<< right << setw(15)<< category
<< " "
<< left << setw(15) << two_day_shipping
<< left << setw(15) << click << endl;
break;
case 6:
category = "Apps";
minprice =.5;
maxprice = 4.99;
price = random (minprice,maxprice);
n= 5 + (rand()%4);
name = printRandomString(n);
two_day_shipping= coinToss();
click = clicks();
fileout << left << setw(7) << id
<< left << setw(15) << name
<< right << setw(15)<< price
<< " "
<< right << setw(15)<< category
<< " "
<< left << setw(15) << two_day_shipping
<< left << setw(15) << click << endl;
break;
}
LoadArray(filename);
}
};
double random(double minprice, double maxprice)
{
double price;
price = ((double)rand()/RAND_MAX)* (maxprice-minprice)+minprice;
return price;
}
string printRandomString(int n)
{
const int MAX = 26;
char alphabet[MAX] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u',
'v', 'w', 'x', 'y', 'z' };
string res = "";
for (int i = 0; i < n; i++)
res = res + alphabet[rand() % MAX];
return res;
}
bool coinToss()
{
return rand() % 2;
}
int clicks()
{
int x;
x = rand()%10000;
return x;
}
void LoadArray(string filename)
{
//string filename;
ifstream infile;
infile.open(filename);
for(int i=0; i<1000; i++)
{
infile>>thingDetails[i].id;
infile>>thingDetails[i].name;
infile>>thingDetails[i].category;
infile>>thingDetails[i].price;
infile>>thingDetails[i].two_day_shipping;
infile>>thingDetails[i].numclicked;
}
infile.close();
int a=0;
for(int a=0; a<50; a++)
cout<<thingDetails[a].id;
cout<<thingDetails[a].name;
cout<<thingDetails[a].category;
cout<<thingDetails[a].price;
cout<<thingDetails[a].two_day_shipping;
cout<<thingDetails[a].numclicked;
}
|