Multiple Output Files

I have this program to create science fiction planets. In it I output a file for each planet created. The problem is after all the output files are created they all contain the same planet. Does anyone know why this is happening? Here's my code.

main.cpp
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
#include "world.h"

#include <cmath>
#include <fstream>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
using namespace std;

// Define functions
int dieroll(int x);
double calculate_density(string a);
double calculate_diameter(string a, int x, double y);
bool check_atmospheric_mass(string a);
int determine_average_surface_temperature(string a);
double determine_atmospheric_pressure(string a, double x, double y);
int determine_blackbody_temperature(string a, double x, int y, int z);
string determine_marginal_atmosphere();
int determine_RVM(string a);
string get_climate_type(int x);
double round(double x, int y);
string select_world_type(double x, char a);
int vary_step(int x);

int main()
{
	int amount;
	char name[30];
	World creator;

	cout << "How many worlds would you like to create? ";
	cin >> amount;

	for (int i=1; i<=amount; i++)
	{
		if (i < 10)
			sprintf(name, "World00%d.txt", i);
		else if (i < 100)
			sprintf(name, "World0%d.txt", i);
		else
			sprintf(name, "World%d.txt", i);
		cout << name << endl;
		creator.generate_world();
		creator.output_world(name);
	}

	return 0;
}


output function in world.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void World::output_world(string a)
{
	ofstream world(a.c_str());

	world << "World Type: " << world_type << endl;
	world << "Atmospheric Mass: " << atmospheric_mass << "     ";
	world << "Atmospheric Composition: " << atmospheric_composition << endl;
	world << "Hydrographic Percentage: " << hydrographic_percentage << "%" << endl;
	world << "Climate Type: " << climate_type << "     ";
	world << "Average Surface Temperature: " << average_surface_temperature << "K (" << (1.8 * average_surface_temperature) - 460 << "° F)     ";
	world << "Blackbody Temperature: " << blackbody_temperature << "K (" << (1.8 * blackbody_temperature) - 460 << "° F)" << endl;
	world << "Density: " << density << " Earths (" << density * 5.52 << " g/cc)     ";
	world << "Diameter: " << round(diameter, 2) << " Earths (" << diameter * 7930 << " mi)     ";
	world << "Surface Gravity: " << round(surface_gravity, 2) << " Gs" << endl;
	world << "Mass: " << round(mass, 5) << " Earths     ";
	world << "Atmospheric Pressure: " << round(atmospheric_pressure, 2) << " atms" << endl;

	world.close();
}
Doesn't seem to be anything in the code you've posted. Maybe you're calling srand() more than once?

By the way, "%03d" is the format string for an integer with three leading zeroes.
Please include 'generate_world' method.
generate_world won't make sense by itself so here's the rest of world.cpp split into 3 posts.
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
/*
 * world.cpp
 *
 *  Created on: Nov 23, 2011
 *      Author: James
 */

#include "world.h"

#include <fstream>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

World::World()
{
	d6 = new Dice(6);
	world_type = "";
	atmospheric_mass = 0;
	atmospheric_composition = "";
	hydrographic_percentage = 0;
	average_surface_temperature = 0;
	climate_type = "";
	blackbody_temperature = 0;
	density = 0;
	diameter = 0;
	surface_gravity = 0;
	mass = 0;
	atmospheric_pressure = 0;
	RVM = 0;
}

void World::generate_world()
{
	world_type = determine_world_type();
	cout << world_type << endl;
	world_type = determine_world_type();
	cout << world_type << endl;
	atmospheric_mass = determine_atmospheric_mass();
	atmospheric_composition = determine_atmospheric_composition();
	hydrographic_percentage = determine_hydrographic_percentage();
	average_surface_temperature = determine_average_surface_temperature();
	climate_type = determine_climate_type();
	blackbody_temperature = determine_blackbody_temperature();
	density = determine_density();
	diameter = determine_diameter();
	surface_gravity = density * diameter;
	mass = density * pow(diameter, 3);
	atmospheric_pressure = determine_atmospheric_pressure();
	RVM = determine_RVM();
}

void World::output_world(string a)
{
	ofstream world(a.c_str());

	world << "World Type: " << world_type << endl;
	world << "Atmospheric Mass: " << atmospheric_mass << "     ";
	world << "Atmospheric Composition: " << atmospheric_composition << endl;
	world << "Hydrographic Percentage: " << hydrographic_percentage << "%" << endl;
	world << "Climate Type: " << climate_type << "     ";
	world << "Average Surface Temperature: " << average_surface_temperature << "K (" << (1.8 * average_surface_temperature) - 460 << "° F)     ";
	world << "Blackbody Temperature: " << blackbody_temperature << "K (" << (1.8 * blackbody_temperature) - 460 << "° F)" << endl;
	world << "Density: " << density << " Earths (" << density * 5.52 << " g/cc)     ";
	world << "Diameter: " << round(diameter, 2) << " Earths (" << diameter * 7930 << " mi)     ";
	world << "Surface Gravity: " << round(surface_gravity, 2) << " Gs" << endl;
	world << "Mass: " << round(mass, 5) << " Earths     ";
	world << "Atmospheric Pressure: " << round(atmospheric_pressure, 2) << " atms" << endl;

	world.close();
}

string World::determine_world_type()
{
	char basic_type;
	int x = d6->roll(3);

	if (x < 8)
		basic_type = 'h';
	else if (x < 14)
		basic_type = 'b';
	else
		basic_type = 'g';

	x = d6->roll(3);

	if (basic_type == 'h')
		switch (x)
		{
	    case 3:
	    case 4:
	    	return "Standard (Chthonian)";
	    case 5:
	    case 6:
	    	return "Standard (Greenhouse)";
	    case 7:
	    case 8:
	    case 9:
	    	return "Tiny (Sulfer)";
	    case 10:
	    case 11:
	    case 12:
	    	return "Standard (Ammonia)";
	    case 13:
	    case 14:
	    	return "Large (Ammonia)";
	    case 15:
	    case 16:
	    	return "Large (Greenhouse)";
	    case 17:
	    case 18:
	    	return "Large (Chthonian)";
		}
	else if (basic_type == 'b')
		switch (x)
		{
		case 3:
			return "Small (Hadean)";
		case 4:
			return "Small (Ice)";
		case 5:
		case 6:
			return "Small (Rock)";
		case 7:
		case 8:
			return "Tiny (Rock)";
		case 9:
		case 10:
			return "Tiny (Ice)";
		case 11:
		case 12:
			return "Asteroid Belt";
		case 13:
		case 14:
			return "Standard (Ocean)";
		case 15:
			return "Standard (Ice)";
		case 16:
			return "Standard (Hadean)";
		case 17:
			return "Large (Ocean)";
		case 18:
			return "Large (Ice)";
		}
	else
		switch (x)
		{
		case 3:
		case 4:
		case 5:
		case 6:
		case 7:
		case 8:
		case 9:
		case 10:
		case 11:
		case 12:
		case 13:
		case 14:
		case 15:
		case 16:
			return "Standard (Garden)";
		case 17:
		case 18:
			return "Large (Garden)";
		}

	return "None";
}
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
double World::determine_atmospheric_mass()
{
	if (world_type == "Asteroid Belt" || world_type == "Tiny (Ice)" || world_type == "Tiny (Rock)" || world_type == "Tiny (Sulfer)" || world_type == "Small (Hadean)" || world_type == "Small (Rock)" || world_type == "Standard (Hadean)" || world_type == "Standard (Chthonian)" || world_type == "Large (Chthonian)")
		return 0;

	int x = d6->roll();

	if (x < 3)
		return (d6->roll(3) / 10.0) - 0.05;
	else if (x < 5)
		return (d6->roll(3) / 10.0);
	else
		return (d6->roll(3) / 10.0) + 0.05;

	return -1;
}

string World::determine_atmospheric_composition()
{
	if (world_type == "Asteroid Belt" || world_type == "Tiny (Ice)" || world_type == "Tiny (Rock)" || world_type == "Tiny (Sulfer)" || world_type == "Small (Hadean)" || world_type == "Small (Rock)" || world_type == "Standard (Hadean)" || world_type == "Standard (Chthonian)" || world_type == "Large (Chthonian)")
		return "None";
	else if (world_type == "Small (Ice)")
	{
		if (d6->roll(3) <= 15)
			return "Suffocating and Mildly Toxic";
		else
			return "Suffocating and Highly Toxic";
	}
	else if (world_type == "Standard (Ammonia)" || world_type == "Standard (Greenhouse)" || world_type == "Large (Ammonia)" || world_type == "Large (Greenhouse)")
		return "Suffocating, Lethally Toxic, and Corrosive";
	else if (world_type == "Standard (Ice)" || world_type == "Standard (Ocean)")
	{
		if (d6->roll(3) <= 12)
			return "Suffocating";
		else
			return "Suffocating and Mildly Toxic";
	}
	else if (world_type == "Standard (Garden)" || world_type == "Large (Garden)")
	{
		if (d6->roll(3) <= 11)
			return "Normal";
		else
			return "Marginal - " + determine_marginal_atmosphere();
	}
	else if (world_type == "Large (Ice)" || world_type == "Large (Ocean)")
		return "Suffocating and Highly Toxic";

	return "N/A";
}

string World::determine_marginal_atmosphere()
{
	switch(d6->roll(3))
	{
	case 3:
	case 4:
		return "Chlorine or Fluorine";
	case 5:
	case 6:
		return "Sulfer Compounds";
	case 7:
		return "Nitrogen Compounds";
	case 8:
	case 9:
		return "Organic Toxins";
	case 10:
	case 11:
		return "Lox Oxygen";
	case 12:
	case 13:
		return "Pollutants";
	case 14:
		return "High Carbon Dioxide";
	case 15:
	case 16:
		return "High Oxygen";
	case 17:
	case 18:
		return "Inert Gases";
	}

	return "N/A";
}

int World::determine_hydrographic_percentage()
{
	int x = d6->roll(), y = 0, z = 0;

	if (x < 3)
		x = -5;
	else if (x < 5)
		x = 0;
	else
		x = 5;

	if (world_type == "Small (Ice)")
		y = (d6->roll() + 2) * 10;
	else if (world_type == "Standard (Ammonia)" || world_type == "Large (Ammonia)")
		y = (d6->roll(2) * 10);
	else if (world_type == "Standard (Ice)" || world_type == "Large (Ice)")
		y = (d6->roll(2) - 10) * 10;
	else if (world_type == "Standard (Ocean)" || world_type == "Standard (Garden)")
		y = (d6->roll() + 4) * 10;
	else if (world_type == "Large (Ocean)" || world_type == "Large (Garden)")
		y = (d6->roll() + 6) * 10;
	else if (world_type == "Standard (Greenhouse)" || world_type == "Large (Greenhouse)")
		y = (d6->roll(2) - 7) * 10;
	else
		x = 0;

	z = x + y;
	if (z < 0)
		return 0;
	else if (z > 100)
		return 100;

	return z;
}

int World::determine_average_surface_temperature()
{
	int x = d6->roll(3) - 3, y = d6->roll(), min = 0, step = 0;

	if (world_type == "Asteroid Belt" || world_type == "Tiny (Rock)" || world_type == "Small (Rock)")
	{
		step = 24;
		min = 140;
	}
	else if (world_type == "Tiny (Ice)" || world_type == "Tiny (Sulfer)" || world_type == "Small (Ice)")
	{
		step = 4;
		min = 80;
	}
	else if (world_type == "Small (Hadean)" || world_type == "Standard (Hadean)")
	{
		step = 2;
		min = 50;
	}
	else if (world_type == "Standard (Ammonia)" || world_type == "Large (Ammonia)")
	{
		step = 5;
		min = 140;
	}
	else if (world_type == "Standard (Ice)" || world_type == "Large (Ice)")
	{
		step = 10;
		min = 80;
	}
	else if (world_type == "Standard (Ocean)" || world_type == "Standard (Garden)" || world_type == "Large (Ocean)" || world_type == "Large (Garden)")
	{
		step = 6;
		min = 250;
	}
	else if (world_type == "Standard (Greenhouse)" || world_type == "Standard (Chthonian)" || world_type == "Large (Greenhouse)" || world_type == "Large (Chthonian)")
	{
		step = 30;
		min = 500;
	}

	if (y < 3)
		y = -0.5 * step;
	else if (y < 5)
		y = 0;
	else
		y = 0.5 * step;

	return x * step + min + y;
}

string World::determine_climate_type()
{
	if (average_surface_temperature < 244)
		return "Frozen";
	else if (average_surface_temperature < 255)
		return "Very Cold";
	else if (average_surface_temperature < 266)
		return "Cold";
	else if (average_surface_temperature < 278)
		return "Chilly";
	else if (average_surface_temperature < 289)
		return "Cool";
	else if (average_surface_temperature < 300)
		return "Normal";
	else if (average_surface_temperature < 311)
		return "Warm";
	else if (average_surface_temperature < 322)
		return "Tropical";
	else if (average_surface_temperature < 333)
		return "Hot";
	else if (average_surface_temperature < 344)
		return "Very Hot";

	return "Infernal";
}

int World::determine_blackbody_temperature()
{
	double cor = 0, ab = 0, gr = 0;

	if (world_type == "Asteroid Belt" || world_type == "Tiny (Rock)" || world_type == "Standard (Chthonian)" || world_type == "Large (Chthonian)")
		ab = 0.97;
	else if (world_type == "Tiny (Ice)")
		ab = 0.86;
	else if (world_type == "Tiny (Sulfer)")
		ab = 0.77;
	else if (world_type == "Small (Hadean)" || world_type == "Standard (Hadean)")
		ab = 0.67;
	else if (world_type == "Small (Ice)")
	{
		ab = 0.93;
		gr = 0.10;
	}
	else if (world_type == "Small (Rock)")
		ab = 0.96;
	else if (world_type == "Standard (Ammonia)" || world_type == "Large (Ammonia)")
	{
		ab = 0.86;
		gr = 0.20;
	}
	else if (world_type == "Standard (Ocean)" || world_type == "Standard (Garden)" || world_type == "Large (Ocean)" || world_type == "Large (Garden)")
	{
		if (hydrographic_percentage < 21)
			ab = 0.95;
		else if (hydrographic_percentage < 51)
			ab = 0.92;
		else if (hydrographic_percentage < 91)
			ab = 0.88;
		else
			ab = 0.84;

		gr = 0.16;
	}
	else
	{
		ab = 0.77;
		gr = 2.00;
	}

	cor = ab * (1 + (atmospheric_mass * gr));
	return average_surface_temperature / cor;
}
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
double World::determine_density()
{
	if (world_type == "Asteroid Belt")
		return 0;

	double x = d6->roll(), min = 0;
	int y = d6->roll(3);

	if (x < 3)
		x = -0.05;
	else if (x < 5)
		x = 0;
	else
		x = 0.05;

	if (world_type == "Tiny (Ice)" || world_type == "Tiny (Sulfer)" || world_type == "Small (Hadean)" || world_type == "Small (Ice)" || world_type == "Standard (Hadean)" || world_type == "Standard (Ammonia)" || world_type == "Large (Ammonia)")
		min = 0.3;
	else if (world_type == "Tiny (Rock)" || world_type == "Small (Rock)")
		min = 0.6;
	else
		min = 0.8;

	switch (y)
	{
	case 3:
	case 4:
	case 5:
	case 6:
		return min + x;
	case 7:
	case 8:
	case 9:
	case 10:
		return min + 0.1 + x;
	case 11:
	case 12:
	case 13:
	case 14:
		return min + 0.2 + x;
	case 15:
	case 16:
	case 17:
		return min + 0.3 + x;
	case 18:
		return min + 0.4 + x;
	}

	return 0;
}

double World::determine_diameter()
{
	if (world_type == "Asteroid Belt")
		return 0;

	double min = 0, max = 0, dif = 0, x = sqrt(blackbody_temperature / density), y = d6->roll(2) - 2, z = d6->roll(), total = 0;

	if (z < 3)
		z = -0.05;
	else if (z < 5)
		z = 0;
	else
		z = 0.05;

	if (world_type.find("Tiny") != (unsigned) -1)
	{
		min = 0.004 * x;
		max = 0.024 * x;
	}
	else if (world_type.find("Small") != (unsigned) -1)
	{
		min = 0.024 * x;
		max = 0.030 * x;
	}
	else if (world_type.find("Standard") != (unsigned) -1)
	{
		min = 0.030 * x;
		max = 0.065 * x;
	}
	else
	{
		min = 0.065 * x;
		max = 0.091 * x;
	}

	dif = max - min;
	total = y * (.1 * dif) + min + z;

	if (total < min)
		total = min;
	else if (total > max)
		total = max;

	return total;
}

double World::determine_atmospheric_pressure()
{
	int p = 0;

	if (world_type == "Asteroid Belt" || world_type == "Tiny (Ice)" || world_type == "Tiny (Rock)" || world_type == "Tiny (Sulfer)" || world_type == "Small (Hadean)" || world_type == "Standard (Hadean)" || world_type == "Small (Rock)" || world_type == "Standard (Chthonian)" || world_type == "Large (Chthonian)")
		p = 0;
	else if (world_type == "Small (Ice)")
		p = 10;
	else if (world_type == "Standard (Ammonia)" || world_type == "Standard (Ice)" || world_type == "Standard (Ocean)" || world_type == "Standard (Garden)")
		p = 1;
	else if (world_type == "Standard (Greenhouse)")
		p = 100;
	else if (world_type == "Large (Ammonia)" || world_type == "Large (Ice)" || world_type == "Large (Ocean)" || world_type == "Large (Garden)")
		p = 5;
	else
		p = 500;

	return atmospheric_mass * p * surface_gravity;
}

int World::determine_RVM()
{
	int x = d6->roll(3);

	if (world_type == "Asteroid Belt")
	{
		switch (x)
		{
		case 3:
			return -5;
		case 4:
			return -4;
		case 5:
			return -3;
		case 6:
		case 7:
			return -2;
		case 8:
		case 9:
			return -1;
		case 10:
		case 11:
			return 0;
		case 12:
		case 13:
			return 1;
		case 14:
		case 15:
			return 2;
		case 16:
			return 3;
		case 17:
			return 4;
		case 18:
			return 5;
		}
	}

	if (x <= 2)
		return -3;
	else if (x >= 19)
		return 3;

	switch (x)
	{
	case 3:
	case 4:
		return -2;
	case 5:
	case 6:
	case 7:
		return -1;
	case 8:
	case 9:
	case 10:
	case 11:
	case 12:
		return 0;
	case 13:
	case 14:
	case 15:
		return 1;
	case 16:
	case 17:
		return 2;
	}

	return -200;
}

double World::round(double x, int y)
{
	double t = x - floor(x);
	int p = pow(10.0, y);

	if (t >= 0.5)
	{
		x = x * p;
		x = ceil(x);
		x = x / p;
	}
	else
	{
		x = x * p;
		x = floor(x);
		x = x / p;
	}

	return x;
}
Post Dice, too, since that seems to be your randomness source.
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
/*
 * dice.cpp
 *
 *  Created on: Nov 23, 2011
 *      Author: James
 */

#include "dice.h"

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;

Dice::Dice(int x)
{
	type = x;
}

int Dice::roll()
{
	srand(time(NULL));

	return (rand() % type + 1);
}

int Dice::roll(int x)
{
	srand(time(NULL));
	int t = 0;

	for(int i=1;i<=x;i++)
		t += (rand() % type + 1);

	return t;
}
Call srand once at the start of main. If you call srand multiple times with the same seed you will get the same sequence of numbers from rand().
Last edited on
Perfect! That was it! Thanks for all the help guys!
Topic archived. No new replies allowed.