-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab5.cpp
More file actions
162 lines (130 loc) · 3.74 KB
/
lab5.cpp
File metadata and controls
162 lines (130 loc) · 3.74 KB
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
//Name: Autumn Henderson
//Date: September 26th, 2018
//Title: Lab 5
//Description: This program takes scores from players from a bowling game and
//prints out all players and their scores and who did the best and worst in the
//game.
#include <iostream>
#include <vector>
using namespace std;
const int ARRAY_SIZE = 21;
int main() {
//Array and Vector Variables
vector<string> names;
vector<double> totalScores;
int scores[ARRAY_SIZE];
//Tracker Variables
bool actualPlayers = false;
int frameIterator;
int i;
int j;
double addedScores;
//CIN Variables
string playerName;
double firstRoll, secondRoll, thirdRoll;
//Min and Max Variables
string maxName, minName;
double maxScore, minScore;
do {
cout << "Enter player's name (done for no more players): ";
cin >> playerName;
//If statement explicitly breaks the loop when player enters "done".
if (playerName == "done") {
break;
}
//If we get to this statement, there is at least one player.
actualPlayers = true;
//Adds in name to vector
names.push_back(playerName);
//Resets trackers
frameIterator = 1;
addedScores = 0;
//For loop initializes/clears scores array.
for (i = 0; i < ARRAY_SIZE; ++i) {
scores[i] = 0;
}
//For loop to enter basic rolls into array.
for (i = 0; i < ARRAY_SIZE; ++i) {
cout << "Enter score for frame " << frameIterator << ", roll 1: ";
cin >> firstRoll;
scores[i] = firstRoll;
++i;
//if statement checks to see if there is a second roll
if (firstRoll < 10 || frameIterator == 10) {
cout << "Enter score for frame " << frameIterator << ", roll 2: ";
cin >> secondRoll;
scores[i] = secondRoll;
}
else {
scores[i] = 0;
}
//if statement checks for third roll on 10th frame
if (frameIterator == 10 && ((firstRoll == 10) || (firstRoll + secondRoll == 10))){
cout << "Enter score for frame " << frameIterator << ", roll 3: ";
cin >> thirdRoll;
scores[21] = thirdRoll;
break;
}
++frameIterator;
}
//Adds scores together
for (i = 0; i < (ARRAY_SIZE - 1); ++i) {
//if score was a strike, adds that plus next two actual rolls
if (scores[i] == 10) {
addedScores += scores[i];
if (scores[i+2] == 10){
addedScores += (scores[i+2] + scores[i+4]);
}
else {
addedScores += (scores[i+2] + scores[i+3]);
}
}
//if score was a spare, adds that plus next roll
else if ((scores[i] + scores[i + 1] == 10)) {
addedScores += (scores[i] + scores[i+1] + scores[i+2]);
}
//else the score was neither a spare or strike and just adds the two rolls
else {
addedScores += (scores[i] + scores[i+1]);
}
++i;
}
//Adds in last frame + 10 if perfect bowl
if (addedScores == 280)
addedScores += (scores[21] + 10);
else
addedScores += (scores[21]);
//Inserts total score into vector
totalScores.push_back(addedScores);
}while (true);
//If statement for whether there were players and, if so, prints
//scores and best and worst.
if (actualPlayers == false) {
cout << "No players were entered.\n";
}
else {
cout << "\n";
//For loop prints names and scores
for (i = 0; i < names.size(); ++i) {
cout << names.at(i) << " scored " << totalScores.at(i) << ".\n";
}
//For loop compares scores
for (i = 0; i < names.size(); ++i) {
if (i == 0) {
maxName = minName = names.at(i);
maxScore = minScore = totalScores.at(i);
}
else if (totalScores.at(i) < minScore) {
minScore = totalScores.at(i);
minName = names.at(i);
}
else if (totalScores.at(i) > maxScore) {
maxScore = totalScores.at(i);
maxName = names.at(i);
}
}
cout << minName << " did the worst by scoring " << minScore << ".\n";
cout << maxName << " won the game by scoring " << maxScore << ".\n";
}
return 0;
}