FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
SimpleListProgram/driver.cpp at master · lmorale4/SimpleListProgram · GitHub
lmorale4
/
SimpleListProgram
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
SimpleListProgram
/
driver.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
261 lines (229 loc) · 5.97 KB
Breadcrumbs
SimpleListProgram
/
driver.cpp
Copy path
File metadata and controls
261 lines (229 loc) · 5.97 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
257
258
259
260
261
/*
Linda Morales
Assignment #2
Section C57
*/
#
include
<
iostream
>
#
include
<
fstream
>
#
include
<
sstream
>
#
include
<
string
>
#
include
"
SimpleList.h
"
using
namespace
std
;
/*
Program makes a SimpleList of TennisPlayer and uses it
to retrive information.
*/
void
processFile
(SimpleList&
sList
);
//
input: a SimpleList pointer
//
output: none
//
side effects: opens file, processes data, and puts it in a SimpleList
TennisPlayer
createTennisPlayer
(string line,
int
lineNum);
//
input: the line(string) from the file and the number of the line (int)
//
output: a TennisPlayer
//
side effects: reads through file to get information to
//
create a TennisPlayer object
bool
checkRank
();
//
input: none
//
output: a bool
//
side effects: checks if user rank is within range 1-20
TennisPlayer
getPlayerByRank
(SimpleList
sList
,
int
rank);
//
input: a SimpleList and an int
//
output: a TennisPlayer object
//
side effects: gets the player of a given rank from a list
int
getRankFromLName
(SimpleList
sList
, string lName);
//
input: a SimpleList and a string
//
output: an int
//
side effects: gets the rank of a player given a last name
int
askForRank
();
//
input: none
//
output: an int
//
side effects: asks user for a rank and returns it
string
askForLastName
();
//
input: none
//
output: a string
//
side effects: asks user for the last name of a player
void
question1
(SimpleList
sList
);
//
input: a SimpleList object
//
output: none
//
side effects: asks user for a rank and displays
//
player with that rank
void
question2
(SimpleList
sList
);
//
input: a SimpleList
//
output: none
//
side effects: asks user for the last name of the
//
player they want to search
void
question3
(SimpleList
sList
);
//
input: a SimpleList
//
output: none
//
side effects:asks user for two ranks and displayes
//
the difference between the player's points
string
charToStr
(
char
c);
//
input: a char
//
output: a string
//
side effects: converts a char into a string
int
stringToInt
(string str);
//
input: a string
//
output: an int
//
side effects: converts a string to an int
int
main
(){
SimpleList
sList
;
processFile
(
sList
);
int
q;
cout <<
"
What would you like to do?
"
<<endl;
cout <<
"
1. Look up player by rank?
"
<< endl;
cout <<
"
2. Look up player ranking by last name?
"
<< endl;
cout <<
"
3. Find the difference in points by ranks?
"
<<endl;
cout <<
"
Choose the number of the question you wish
"
;
cout <<
"
to have answered to continue. (1-3)
"
<< endl;
cin >> q;
while
(q){
while
(q <
1
&& q >
3
){
cout <<
"
Please choose a number between 1 and 3
"
<< endl;
cin >> q;
}
if
(q ==
1
){
question1
(
sList
);
}
else
if
(q ==
2
){
question2
(
sList
);
}
else
if
(q ==
3
){
question3
(
sList
);
}
else
{
cout <<
"
We do not recognize that question
\n
"
;
}
cout <<
"
\n
Choose another number to continue.
"
<< endl;
cout <<
"
Enter any other key to quit:
"
<<endl;
cin >> q;
}
}
void
processFile
(SimpleList&
sList
){
string line;
ifstream
myFile
(
"
players.txt
"
);
if
(myFile.
is_open
()){
int
lineNum =
1
;
while
(
getline
(myFile, line)){
if
(line.
length
() >
0
){
sList
.
insert
(
createTennisPlayer
(line, lineNum),
sList
.
listSize
());
lineNum++;
}
}
}
else
cout <<
"
We were unable to open this file
"
;
myFile.
close
();
}
TennisPlayer
createTennisPlayer
(string line,
int
lineNum){
string str1;
string str2;
string
fName
;
string lName;
string points;
string country;
for
(
int
i =
0
; i< line.
length
(); i++){
str1 =
charToStr
(line[i]);
if
(str1.
compare
(
"
"
) >
0
){
str2 += str1;
}
else
{
if
(
fName
.
length
() ==
0
){
fName
+= str2;
}
else
if
(lName.
length
() ==
0
){
lName += str2;
}
else
if
(points.
length
() ==
0
){
points += str2;
}
else
{
country += str2 +
"
"
;
}
str2=
"
"
;
}
}
country += str2;
int
totPoints =
stringToInt
(points);
TennisPlayer
t
(
fName
,lName, lineNum, totPoints ,country);
lineNum++;
return
t;
}
bool
checkRank
(
int
uRank){
return
(uRank >
0
&& uRank <
21
);
}
TennisPlayer
getPlayerByRank
(SimpleList
sList
,
int
rank){
return
sList
.
retrieve
(rank -
1
);
}
int
getRankFromLName
(SimpleList
sList
, string lName){
int
rank =
21
;
for
(
int
i=
0
; i<
sList
.
listSize
(); i++){
if
(lName.
compare
(
sList
.
retrieve
(i).
getLastName
()) ==
0
){
rank =
sList
.
retrieve
(i).
getRank
();
i =
sList
.
listSize
();
}
}
return
rank;
}
int
askForRank
(){
int
uRank;
cout <<
"
Select a Rank from 1-20:
"
;
cin >> uRank;
return
uRank;
}
string
askForLastName
(){
string uStr;
cout <<
"
What is the Last Name of the Player?
"
;
cin >> uStr;
return
uStr;
}
void
question1
(SimpleList
sList
){
int
uRank =
askForRank
();
while
(!(
checkRank
(uRank))){
cout <<
"
Sorry! that player is not available.
"
<< endl;
uRank =
askForRank
();
}
TennisPlayer t =
getPlayerByRank
(
sList
, uRank);
cout << t <<endl;
}
void
question2
(SimpleList
sList
){
string uStr =
askForLastName
();
int
rank =
getRankFromLName
(
sList
, uStr);
if
(rank >
20
){
cout <<
"
Sorry! This player is not ranked in our records.
"
<< endl;
}
else
cout << uStr <<
"
is ranked number
"
<< rank << endl;
}
void
question3
(SimpleList
sList
){
int
rank1 =
askForRank
();
while
(!(
checkRank
(rank1))){
cout <<
"
Sorry! that player is not available.
"
<< endl;
rank1 =
askForRank
();
}
int
rank2 =
askForRank
();
while
(!(
checkRank
(rank2))){
cout <<
"
Sorry! that player is not available.
"
<< endl;
rank2 =
askForRank
();
}
TennisPlayer t1 =
getPlayerByRank
(
sList
, rank1);
TennisPlayer t2 =
getPlayerByRank
(
sList
, rank2);
int
points1 = t1.
getPoints
();
int
points2 = t2.
getPoints
();
int
difference = points1 - points2;
if
(difference <
0
){
difference *= -
1
;
}
cout <<
"
The players are separated by
"
<< difference;
cout <<
"
points.
"
<< endl;
}
string
charToStr
(
char
c){
stringstream ss;
string str;
ss << c;
ss >> str;
return
str;
}
int
stringToInt
(string str){
stringstream ss;
int
num;
ss << str;
ss >> num;
return
num;
}
Back
|
FazBrowse Home
|
New Git URL