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');
}
#include <iostream>
#include <string>
#include <iomanip>
#include <limits> // To use std::numeric_limits.
usingnamespace 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');
}