URGENT!!Could you please do it in C++ vim terminal and use only one return at th
ID: 3754148 • Letter: U
Question
URGENT!!Could you please do it in C++ vim terminal and use only one return at the end of the program instead of two!!!
Array should be [21]!!
And make it as simple as possible!
Assignment:You will be writing a program to score bowling for several players. Your job will be to ask for players and their scores, then add up their score. When all players have been entered, the user will type "done". Then, your program will tally up the worst and best player and output who won and who lost the game.
Restrictions / Hints
1. You must use a vector to store all of the player names. All player names will be a single name.
2. You must use another vector to store the total score for each player. You must calculate the total score for each player after all rolls have been entered.
3. You must use an array to store each roll's value.
4. The number of rolls is variable depending on whether the player threw a strike (knocked down 10 pins in one roll), spare, or neither. Please see "Scoring Bowling" below.
5. You must use a for loop to input the roll values into the array.
6. If the user types "done" before entering any names, output "No players were entered."
7. In the event of a tie, the first player (lowest vector index) to obtain the best or worst score is printed.
8. You can assume that the user will input valid scores. You do not need to perform error checking for invalid input.
Enter player's name (done for no more players): John Enter score for frame 1, roLL 1: 10 Enter score for frame 2, roll 1: 10 Enter score for frame 3, roll 1: 10 Enter score for frame 4, roll 1: 10 Enter score for frame 5, roll 1: 10 Enter score for frame 6, roll 1: 10 Enter score for frame 7, roll 1: 10 Enter score for frame 8, roll 1: 10 Enter score for frame 9, roll 1: 10 Enter score for frame 10, roll 1: 10 Enter score for frame 10, roll 2: 10 Enter score for frame 10, roll 3: 10 Enter player's name (done for no more players): Cheryl Enter score for frame 1, roll 1: 8 Enter score for frame 1, roll 2: 1 Enter score for frame 2, roll 1: 0 Enter score for frame 2, roll 2: 9 Enter score for frame 3, roll 1: 2 Enter score for frame 3, roll 2: 8 Enter score for frame 4, roll 1: 10 Enter score for frame 5, roll 1: 6 Enter score for frame 5, roll 2: 3 Enter score for frame 6, roll 1: 7 Enter score for frame 6, roll 2: 0 Enter score for frame 7, roll 1: 5 Enter score for frame 7, roll 2: 2 Enter score for frame 8, roll 1: 10 Enter score for frame 9, roll 1: 0 Enter score for frame 9, roll 2: 6 Enter score for frame 10, roll 1: 2 Enter score for frame 10, roll 2: 8 Enter score for frame 10, roll 3: 10 Enter player's name (done for no more players): done John scored 300 Cheryl scored 122 Cheryl did the worst by scoring 122. John won the game by scoring 300 Enter player's name (done for no more players): done No players were enteredExplanation / Answer
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int main(){
vector<string> players; // vector used to store players
string player; // string variable to store player's name
vector<int> scores; // vector scores to store score for each player
while(true){
cout <<"Enter player's name (done for no more players): ";
cin >> player;
if(player == "done"){
if (players.empty()){
cout << "No players were entered.";
return 0;
}
break; //if input is "done", no need to continue, no need to insert to an array
}
players.push_back(player);
int rolls[22]={0}; // one-dimensional int array used to store score for each roll, the possible maximum rolls is 21, defined size as 22, because not using rolls[0]; we can do two-dimensional int array
int roll=1; // initiate the value of roll as 1
int frame=1; // initiate the value of frame as 1
int score; // score for each roll
for (int i=1; i<21; ++i){ // a for loop as required to take all input
cout <<"Enter score for frame "<<frame<<", roll "<<roll<<": ";
cin >> score;
while(!cin){ // check whether the input is an integer
cout << "Please input an integer: ";
cin.clear(); // clear the input stream, basically delete the fetched value
cin.ignore(256,' '); // Maximum number of characters to extract 256, stops extracting until new line ' '
cin >> score; // store the value in score
}
while(score < 0 || score > 10){// There are only 10 pins
cout << "Please input an integer between 1~10: ";
cin.clear(); // clear the input stream, basically delete the fetched value
cin >> score; // store the value in score
}
rolls[i] = score; // insert the score to the array
if(score == 10){ // based on the rule of game, before frame 10, when the first hit is 10, bump the frame
if (frame<10){ // frame 10 is different than frame 1 to 9
if (i % 2 == 1) {
++i; //since it is a one-dimensional array, every two units is a frame, since it hits 10 in roll 1, move to next frame
}
++frame;
roll = 1;
} else { // in frame 10, even the roll 1 is 10 pins, still have to play roll 2
roll = 2; // bump the roll to roll 2
}
}else{
if(i % 2 == 1){
roll = 2; // if current roll is one, then the coming roll is 2
}else{
++frame; // if it is roll 2, bump up frame,
roll = 1;// reset roll to 1
}
}
}
if (rolls[19]+rolls[20] >= 10){//if the sum of the score of frame 10's roll 1 and roll 2, is larger than 10, roll 3!
frame = 10, roll = 3;
cout <<"Enter score for frame "<<frame<<", roll "<<roll<<": ";
cin >> score;
rolls[21] = score;
}
vector<int> frame_scores; // used to store the calculated score of each frame
// the score calculation is defined here, better to define a function
for(int i = 1; i< 19; i += 2){ // i += 2, set up the step as 2, make sure each i will be the roll 1
int frame_score=0; //for each frame, initiate the calculated as 0
if (rolls[i]==10){ // Strike
frame_score+=10; // if the roll 1 of each frame is 10, then add 10 at first
if(rolls[i+2] == 10){ // if the score of the following roll 1 is 10, then add 10
frame_score += 10;
if(i<17){ //boundary check, if frame still within the 8 rounds, the i+2+2 works
frame_score += rolls[i+2+2];
} else{
frame_score += rolls[20];//for the 10th frame, even the score of roll 1 is 10, still need to do the roll 2, rolls[20] is the score of roll 2
}
} else {
frame_score += rolls[i+2] + rolls[i+3];// if the following score of roll 1 is not 10, then these are following two shots
}
} else if(rolls[i]+rolls[i+1] == 10){ // Spare
frame_score += 10;
frame_score += rolls[i+2];
} else {
frame_score = rolls[i]+rolls[i+1];
}
frame_scores.push_back(frame_score);
}
// the last round
if (rolls[19] == 10){
frame_scores.push_back(10+rolls[20]+rolls[21]);
} else if(rolls[19]+rolls[20] == 10){
frame_scores.push_back(10+rolls[21]);
}
else {
frame_scores.push_back(rolls[19]+rolls[20]);
}
int sum=0;
for(int i: frame_scores){
sum+=i;
}
scores.push_back(sum);
}
//Better to define a function here to print those records
unsigned int j=0;
for(string plr: players){
cout<<plr<< " scored "<<scores.at(j)<<endl;
++j; // j represents the location of the plr(player)
}
//Better to define a function to get the min index, max index, min value, and max value
int min = INT_MAX; // The built in max integer in c++
int max =INT_MIN; // The built in min integer in c++
unsigned int index_min=0;
unsigned int index_max=0;
unsigned int i =0;
for(int score: scores){
if (score < min){
min=score;
index_min = i;
}
if (score > max){
max=score;
index_max = i;
}
++i;
}
cout<<players.at(index_min)<<" did the worst by scoring "<<min<<endl;
cout<<players.at(index_max)<<" won the game by scoring "<<max<<endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.