displaying sorted variables

How would I display variables and write that to a file.

E.G. Oklahoma has 7. Oklahoma State has 10. So the file would look like

Oklahoma State = 10
Oklahoma = 7


Now, this is probably completely messed up(it runs, shockingly), but I just want some advice on where to go from here(or if I need to completely start over.

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
#include <iostream>
#include <fstream>
#include <stdio.h>
using namespace std;

int Oklahoma;
int OklahomaSt;
int KansasSt;
int TexasAM;
int Texas;
int TexasTech;
int Missouri;
int Baylor;
int IowaSt;
int Kansas;
int firstplaceteam;
int lastplaceteam;
int SIZE=10;
int teamarray[] = {Oklahoma, OklahomaSt, KansasSt, TexasAM,	Texas, TexasTech, Missouri, Baylor, IowaSt, Kansas};

void sort();
void firstplace();
void lastplace();
void firstplacedisplay();

void lastplace()
{
		if(OklahomaSt < Oklahoma && OklahomaSt < KansasSt && OklahomaSt < TexasAM && OklahomaSt < Texas && OklahomaSt < TexasTech && OklahomaSt < Missouri && OklahomaSt < Baylor && OklahomaSt < IowaSt && OklahomaSt < Kansas)
		{
			lastplaceteam=OklahomaSt;
		}
		else if(Oklahoma < OklahomaSt && Oklahoma < KansasSt && Oklahoma < TexasAM && Oklahoma < Texas && Oklahoma < TexasTech && Oklahoma < Missouri && Oklahoma < Baylor && Oklahoma < IowaSt && Oklahoma < Kansas)
		{
			lastplaceteam=Oklahoma;
		}
		else if(KansasSt < Oklahoma && KansasSt < OklahomaSt && KansasSt < TexasAM && KansasSt < Texas && KansasSt < TexasTech && KansasSt < Missouri && KansasSt < Baylor && KansasSt < IowaSt && KansasSt < Kansas)
		{
			lastplaceteam=KansasSt;
		}	
		else if(TexasAM < Oklahoma && TexasAM < KansasSt && TexasAM < OklahomaSt && TexasAM < Texas && TexasAM < TexasTech && TexasAM < Missouri && TexasAM < Baylor && TexasAM < IowaSt && TexasAM < Kansas)
		{
			lastplaceteam=TexasAM;
		}
		else if(Texas < Oklahoma && Texas < KansasSt && Texas < OklahomaSt && Texas < TexasAM && Texas < TexasTech && Texas < Missouri && Texas < Baylor && Texas < IowaSt && Texas < Kansas)
		{
			lastplaceteam=Texas;
		}
		else if(TexasTech < Oklahoma && TexasTech < KansasSt && TexasTech < OklahomaSt && TexasTech < Texas && TexasTech < TexasAM && TexasTech < Missouri && TexasTech < Baylor && TexasTech < IowaSt && TexasTech < Kansas)
		{
			lastplaceteam=TexasTech;
		}
		else if(Missouri < Oklahoma && Missouri < KansasSt && Missouri < OklahomaSt && Missouri < Texas && Missouri < TexasTech && Missouri < TexasAM && Missouri < Baylor && Missouri < IowaSt && Missouri < Kansas)
		{
			lastplaceteam=Missouri;
		}
		else if(Baylor < Oklahoma && Baylor < KansasSt && Baylor < OklahomaSt && Baylor < Texas && Baylor < TexasTech && Baylor < Missouri && Baylor < TexasAM && Baylor < IowaSt && Baylor < Kansas)
		{
			lastplaceteam=Baylor;
		}
		else if(IowaSt < Oklahoma && IowaSt < KansasSt && IowaSt < OklahomaSt && IowaSt < Texas && IowaSt < TexasTech && IowaSt < Missouri && IowaSt < Baylor && IowaSt < TexasAM && IowaSt < Kansas)
		{
			lastplaceteam=IowaSt;
		}
		else if(Kansas < Oklahoma && Kansas < KansasSt && Kansas < OklahomaSt && Kansas < Texas && Kansas < TexasTech && Kansas < Missouri && Kansas < Baylor && Kansas < IowaSt && Kansas < TexasAM)
		{
			lastplaceteam=Kansas;
		}

}

void firstplace()
{
		if(OklahomaSt > Oklahoma && OklahomaSt > KansasSt && OklahomaSt > TexasAM && OklahomaSt > Texas && OklahomaSt > TexasTech && OklahomaSt > Missouri && OklahomaSt > Baylor && OklahomaSt > IowaSt && OklahomaSt > Kansas)
		{
			firstplaceteam=OklahomaSt;
		}
		else if(Oklahoma > OklahomaSt && Oklahoma > KansasSt && Oklahoma > TexasAM && Oklahoma > Texas && Oklahoma > TexasTech && Oklahoma > Missouri && Oklahoma > Baylor && Oklahoma > IowaSt && Oklahoma > Kansas)
		{
			firstplaceteam=Oklahoma;
		}
		else if(KansasSt > Oklahoma && KansasSt > OklahomaSt && KansasSt > TexasAM && KansasSt > Texas && KansasSt > TexasTech && KansasSt > Missouri && KansasSt > Baylor && KansasSt > IowaSt && KansasSt > Kansas)
		{
			firstplaceteam=KansasSt;
		}
		else if(TexasAM > Oklahoma && TexasAM > KansasSt && TexasAM > OklahomaSt && TexasAM > Texas && TexasAM > TexasTech && TexasAM > Missouri && TexasAM > Baylor && TexasAM > IowaSt && TexasAM > Kansas)
		{
			firstplaceteam=TexasAM;
		}
		else if(Texas > Oklahoma && Texas > KansasSt && Texas > OklahomaSt && Texas > TexasAM && Texas > TexasTech && Texas > Missouri && Texas > Baylor && Texas > IowaSt && Texas > Kansas)
		{
			firstplaceteam=Texas;
		}
		else if(TexasTech > Oklahoma && TexasTech > KansasSt && TexasTech > OklahomaSt && TexasTech > Texas && TexasTech > TexasAM && TexasTech > Missouri && TexasTech > Baylor && TexasTech > IowaSt && TexasTech > Kansas)
		{
			firstplaceteam=TexasTech;
		}
		else if(Missouri > Oklahoma && Missouri > KansasSt && Missouri > OklahomaSt && Missouri > Texas && Missouri > TexasTech && Missouri > TexasAM && Missouri > Baylor && Missouri > IowaSt && Missouri > Kansas)
		{
			firstplaceteam=Missouri;
		}
		else if(Baylor > Oklahoma && Baylor > KansasSt && Baylor > OklahomaSt && Baylor > Texas && Baylor > TexasTech && Baylor > Missouri && Baylor > TexasAM && Baylor > IowaSt && Baylor > Kansas)
		{
			firstplaceteam=Baylor;
		}
		else if(IowaSt > Oklahoma && IowaSt > KansasSt && IowaSt > OklahomaSt && IowaSt > Texas && IowaSt > TexasTech && IowaSt > Missouri && IowaSt > Baylor && IowaSt > TexasAM && IowaSt > Kansas)
		{
			firstplaceteam=IowaSt;
		}
		else if(Kansas > Oklahoma && Kansas > KansasSt && Kansas > OklahomaSt && Kansas > Texas && Kansas > TexasTech && Kansas > Missouri && Kansas > Baylor && Kansas > IowaSt && Kansas > TexasAM)
		{
			firstplaceteam=Kansas;
		}
}

void main()
{
	sort();
}

void sort() 
{
	int i;

	Oklahoma = 1+0+1+1+1+1+1-1+1;
	OklahomaSt = 1+1+1+1+1+1+1+1;
	KansasSt = 1+0+1+1+1+1+1+1-1;
	TexasAM = 1+0+1-1-1+1+1+1-1;
	Texas = 1+1+1+0+1-1-1+0+1;
	TexasTech = 1+0+1+1+1-1-1+1-1;
	Missouri = 1-1+1-1+0-1+1-1+1;
	Baylor = 1+0+1+1-1+1-1+0-1;
	IowaSt = 1+1+1+0-1-1-1-1+1;
	Kansas = 1+1-1-0-1-1-1-1-1;
	
	ofstream myfile ("example.txt");
	if (myfile.is_open())
	{
		firstplace();
		lastplace();
		firstplacedisplay();
	

		if(lastplaceteam == OklahomaSt)
		{
			myfile << "\nOklahomaSt = " << OklahomaSt;
		}
		
		if(lastplaceteam == Oklahoma)
		{
			myfile << "\nOklahoma = " << Oklahoma;
		}
		
		if(lastplaceteam == Baylor)
		{
			myfile << "\nBaylor = " << Baylor;
		}
		
		if(lastplaceteam == IowaSt)
		{
			myfile << "\nIowaSt = " << IowaSt;
		}
		
		if(lastplaceteam == KansasSt)
		{
			myfile << "\nKansasSt = " << KansasSt;
		}
		
		if(lastplaceteam == TexasAM)
		{
			myfile << "\nTexasAM = " << TexasAM;
		}
		
		if(lastplaceteam == Texas)
		{
			myfile << "\nTexas = " << Texas;
		}
		
		if(lastplaceteam == TexasTech)
		{
			myfile << "\nTexasTech = " << TexasTech;
		}
		
		if(lastplaceteam == Missouri)
		{
			myfile << "\nMissouri = " << Missouri;
		}


		myfile.close();
	}
	else cout << "Unable to open file";
}

void firstplacedisplay()
{
	ofstream myfile ("example.txt");
		if(firstplaceteam == OklahomaSt)
		{
			myfile << "\nOklahomaSt = " << OklahomaSt;
		}

		if(firstplaceteam == Oklahoma)
		{
			myfile << "\nOklahoma = " << Oklahoma;
		}
		
		if(firstplaceteam == Baylor)
		{
			myfile << "\nBaylor = " << Baylor;
		}
		
		if(firstplaceteam == IowaSt)
		{
			myfile << "\nIowaSt = " << IowaSt;
		}
		
		if(firstplaceteam == KansasSt)
		{
			myfile << "\nKansasSt = " << KansasSt;
		}
		
		if(firstplaceteam == TexasAM)
		{
			myfile << "\nTexasAM = " << TexasAM;
		}
		
		if(firstplaceteam == Texas)
		{
			myfile << "\nTexas = " << Texas;
		}
		
		if(firstplaceteam == TexasTech)
		{
			myfile << "\nTexasTech = " << TexasTech;
		}
		
		if(firstplaceteam == Missouri)
		{
			myfile << "\nMissouri = " << Missouri;
		}
}
Firstly, you could define a struct that stores the team name and score so you could replace this:

1
2
3
4
5
6
7
8
9
10
11
if(firstplaceteam == OklahomaSt)
{
    myfile << "\nOklahomaSt = " << OklahomaSt;
}

if(firstplaceteam == Oklahoma)
{
    myfile << "\nOklahoma = " << Oklahoma;
}

...

with this:

1
2
3
4
5
6
7
8
9
10
struct Team
{
    string name;
    int score;
};

...

Team lastPlaceTeam = determineLastPlace();
myfile << "\n" << lastPlaceTeam.name << " = " << lastPlaceTeam.score;


You should also look into sorting algorithms to avoid this:

if(OklahomaSt < Oklahoma && OklahomaSt < KansasSt && OklahomaSt < TexasAM && OklahomaSt < Texas && OklahomaSt < TexasTech && OklahomaSt < Missouri && OklahomaSt < Baylor && OklahomaSt < IowaSt && OklahomaSt < Kansas)

Here are descriptions of some sorting algorithms:
http://en.wikipedia.org/wiki/Bubble_sort
http://en.wikipedia.org/wiki/Insertion_sort
http://en.wikipedia.org/wiki/Merge_sort
http://en.wikipedia.org/wiki/Quicksort
Here is a start of how you could do it using a STL vector, a couple of STL algorithms and lambdas.

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
class Team
{
  public:
    string name;
    int score;
    Team( string name, int score ) : name(name), score(score) {}
};


int main( int argc, char* argv[] )
{
  vector< Team > teams;
  teams.push_back( Team( "Oklahoma",   1+0+1+1+1+1+1-1+1 ) );
  teams.push_back( Team( "OklahomaSt", 1+1+1+1+1+1+1+1 ) );
  teams.push_back( Team( "KansasSt",   1+0+1+1+1+1+1+1-1 ) );
  teams.push_back( Team( "TexasAM",    1+0+1-1-1+1+1+1-1 ) );
  teams.push_back( Team( "Texas",      1+1+1+0+1-1-1+0+1 ) );
  teams.push_back( Team( "TexasTech",  1+0+1+1+1-1-1+1-1 ) );
  teams.push_back( Team( "Missouri",   1-1+1-1+0-1+1-1+1 ) );
  teams.push_back( Team( "Baylor",     1+0+1+1-1+1-1+0-1 ) );
  teams.push_back( Team( "IowaSt",     1+1+1+0-1-1-1-1+1 ) );
  teams.push_back( Team( "Kansas",     1+1-1-0-1-1-1-1-1 ) );

  // sort teams - best to worst score
  sort( teams.begin(), teams.end(), []( const Team& t1, const Team& t2 ) { return t1.score > t2.score; } );

  // display all teams
  for_each( teams.begin(), teams.end(), []( const Team& t ) { cout << left << setw(12) << t.name << t.score << endl; } );
	
  cout << endl;
  cout << "Best  Score " << teams[0].name << " " << teams[0].score << endl;
  cout << "Worst Score " << teams[teams.size() - 1].name << " " << teams[teams.size() - 1].score << endl;
}


Output

OklahomaSt 8
Oklahoma 6
KansasSt 6
Texas 3
TexasAM 2
TexasTech 2
Baylor 1
Missouri 0
IowaSt 0
Kansas -4

Best Score OklahomaSt 8
Worst Score Kansas -4

Topic archived. No new replies allowed.