HOMEWORK HELP

Hello I can't seem to figure out why my code skips over the gymmast's name, can someone help point out what I'm doing wrong. This is a for and while subject.

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;


int main()
{ char choice;
do{
string gymnasts_name;
int gymnasts = 0, events = 0, score = 0;
double total = 0.0;
cout <<"*************************************************"<<endl;
cout <<"* This program calculates the total and average *"<<endl;
cout<<"* points for each member of a gymnastics team. *"<<endl;
cout<<"* The team's total and average points are also calculated. *"<<endl;
cout<<"*************************************************"<<endl;

cout<<"Please enter the number of gymnasts on the team: ? "<<endl;
cin >> gymnasts;
cout<<"Please enter the number of events the team competed in: ? ";
cin >> events;

for (int gym = 1; gym <= gymnasts; gym++)
{
cout << "Enter the gymnast "<< gym<<"'s name: ? ";
getline(cin, gymnasts_name);
cout << "Enter the points earned by "<<gymnasts_name<<" for each event."<<endl;
for (int numberforevents = 1 ; numberforevents <= events; numberforevents++)
{
cout << "Event "<< numberforevents<<"? ";
cin >> score;
if (score<0)
cout << "**** ERROR – INVALID SCORE – PLEASE RE-ENTER ****"<<endl;
total = total+score;
}
}
cout << gymnasts_name<<":"<<endl;
cout << "Total Points: "<<total<<endl;
cout << "Average Points: "<<total/events<<endl;

cout << "Would you like to run this program again? (Y/N)" << endl;
cin >> choice;
}while (choice=='Y'||choice=='y');
}
I placed the comments on your code to give you an idea. Please, note that there are many ways to solve this.

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
#include <iostream>
#include <string>
#include <iomanip>
#include <limits> // To use std::numeric_limits.
using namespace std;


int main()
{
	char choice;
	do {
		string gymnasts_name;
		int gymnasts = 0, events = 0, score = 0;
		double total = 0.0;
		cout << "*************************************************" << endl;
		cout << "* This program calculates the total and average *" << endl;
		cout << "* points for each member of a gymnastics team. *" << endl;
		cout << "* The team's total and average points are also calculated. *" << endl;
		cout << "*************************************************" << endl;

		cout << "Please enter the number of gymnasts on the team: ? " << endl;
		cin >> gymnasts; // Are you required to validate (i.e. no negative values) input here?
		cout << "Please enter the number of events the team competed in: ? ";
		cin >> events; // Are you required to validate (i.e. no negative values) input here?

		for (int gym = 1; gym <= gymnasts; gym++)
		{
			// Same thing as the other code, http://www.cplusplus.com/forum/general/250301/
			std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

			cout << "Enter the gymnast " << gym << "'s name: ? ";
			getline(cin, gymnasts_name);
			cout << "Enter the points earned by " << gymnasts_name << " for each event." << endl;
			for (int numberforevents = 1; numberforevents <= events; numberforevents++)
			{
				cout << "Event " << numberforevents << "? ";
				cin >> score;
				//if (score < 0)
				//	cout << "**** ERROR – INVALID SCORE – PLEASE RE-ENTER ****" << endl;
				while (score < 0) // Validate the input is not a negative integer.
				{
					cout << "**** ERROR – INVALID SCORE – PLEASE RE-ENTER ****" << endl;
					cin >> score; // Let the user try again by inputting the value.
				}
				total = total + score;
			}

			// Display the current gymnast name with the running
			// score of the team.
			cout << gymnasts_name << ":" << endl;
			cout << "Total Points: " << total << endl;
			cout << "Average Points: " << total / events << endl;
		}
		//cout << gymnasts_name << ":" << endl;
		//cout << "Total Points: " << total << endl;
		//cout << "Average Points: " << total / events << endl;

		cout << "Would you like to run this program again? (Y/N)" << endl;
		cin >> choice;

	} while (choice == 'Y' || choice == 'y');
}
Topic archived. No new replies allowed.