Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

COPY AND PASTE THIS CODE AND HELP ME WITH MY ERROR! So I\'m having touble for a

ID: 3754344 • Letter: C

Question

COPY AND PASTE THIS CODE AND HELP ME WITH MY ERROR!

So I'm having touble for a statement. When the user puts in the word "done" when asked at the beginning of the program cout<<"Enter player's name (done for no more players): ";

It prints out "No players were entered' and then the following terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check: __n (which is 0) >= this->size() (which is 0)

I just want it to print out No players were entered without the terminate sentence!!

#include <iostream>

#include <vector>

#include <string>

using namespace std;


int main()

{


vector<string> players;


vector<int> score;

int x=0, y=0, z=0;

int bpos;

int wpos;

int worst;

int frames[21];

int wors, best, v=0;


string nam;


while (true)

{

cout<<"Enter player's name (done for no more players): ";

cin>>nam;

if(nam=="done")

{

if(v==0)

{

cout<<"No players were entered"<<endl;


}

break;

}

v=1;


players.push_back(nam);


for(int i=0; i<9; i++)

{

cout<<"Enter score for frame "<<i+1<<", roll 1:";

cin>>x;

frames[i*2]=x;

if(x<10)

{

cout<<"Enter score for frame "<<i+1<<", roll 2:";

cin>>y;

frames[(i*2)+1]=y;

}

else

{

frames[(i*2)+1]=0;

}

}

cout<<"Enter score for frame 10, roll 1:";

cin>>x;

frames[18]=x;

int d=x;

cout<<"Enter score for frame 10, roll 2:";

cin>>y;

frames[19]=y;

d=x+y;

if(d>=10)

{

cout<<"Enter score for frame 10, roll 3:";

cin>>z;

frames[20]=z;

}

else

frames[20]=0;

int finalSc=0;


for(int j=0; j<18; j=j+2)

{

if(frames[j]==10)

{

if(frames[j+2]!=10)

finalSc=finalSc+10+frames[j+2]+frames[j+3];

else

finalSc=finalSc+10+frames[j+2]+frames[j+4];

}

else if(frames[j]+frames[j+1]==10)

{

finalSc=finalSc+10+frames[j+2];

}

else

{

finalSc=finalSc+frames[j]+frames[j+1];

}

}

finalSc=finalSc+frames[18]+frames[19]+frames[20];

score.push_back(finalSc);

}

worst = score.at(0);

wpos=0;

best = score.at(0);

bpos=0;

cout<<endl;

for(int i=0;i<players.size();i++)

{

if(worst>score.at(i))

{

worst = score.at(i);

wpos = i;

}

if(best<score.at(i))

{

best = score.at(i);

bpos = i;

}

cout<<players.at(i)<<" Scored "<<score.at(i)<<"."<<endl;

}

cout<<players.at(wpos)<<" did the worst by scoring "<<score.at(wpos)<<"."<<endl;

cout<<players.at(bpos)<<" won the game by scoring "<<score.at(bpos)<<".";

return 0;

}

Explanation / Answer

#include <iostream>

#include <vector>

#include <string>

using namespace std;

int main()

{

vector<string> players;

vector<int> score;

int x = 0, y = 0, z = 0;

int bpos;

int wpos;

int worst;

int frames[21];

int wors, best, v = 0;

string nam;

while (true)

{

cout << "Enter player's name (done for no more players): ";

cin >> nam;

if (nam == "done")

{

break;

}

v = 1;

players.push_back(nam);

for (int i = 0; i < 9; i++)

{

cout << "Enter score for frame " << i + 1 << ", roll 1:";

cin >> x;

frames[i * 2] = x;

if (x < 10)

{

cout << "Enter score for frame " << i + 1 << ", roll 2:";

cin >> y;

frames[(i * 2) + 1] = y;

}

else

{

frames[(i * 2) + 1] = 0;

}

}

cout << "Enter score for frame 10, roll 1:";

cin >> x;

frames[18] = x;

int d = x;

cout << "Enter score for frame 10, roll 2:";

cin >> y;

frames[19] = y;

d = x + y;

if (d >= 10)

{

cout << "Enter score for frame 10, roll 3:";

cin >> z;

frames[20] = z;

}

else

frames[20] = 0;

int finalSc = 0;

for (int j = 0; j < 18; j = j + 2)

{

if (frames[j] == 10)

{

if (frames[j + 2] != 10)

finalSc = finalSc + 10 + frames[j + 2] + frames[j + 3];

else

finalSc = finalSc + 10 + frames[j + 2] + frames[j + 4];

}

else if (frames[j] + frames[j + 1] == 10)

{

finalSc = finalSc + 10 + frames[j + 2];

}

else

{

finalSc = finalSc + frames[j] + frames[j + 1];

}

}

finalSc = finalSc + frames[18] + frames[19] + frames[20];

score.push_back(finalSc);

}

if (v == 0)

{

cout << "No players were entered" << endl;

}

else{

worst = score.at(0);

wpos = 0;

best = score.at(0);

bpos = 0;

cout << endl;

for (int i = 0; i < players.size(); i++)

{

if (worst > score.at(i))

{

worst = score.at(i);

wpos = i;

}

if (best < score.at(i))

{

best = score.at(i);

bpos = i;

}

cout << players.at(i) << " Scored " << score.at(i) << "." << endl;

}

cout << players.at(wpos) << " did the worst by scoring " << score.at(wpos) << "." << endl;

cout << players.at(bpos) << " won the game by scoring " << score.at(bpos) << "."<<endl;

}

return 0;

}

If "No players entered" then we can terminate the program, so that I used return.

Please check with bolded text...

Let me know if you have any clarifications. Thank you...