Hello all,
I am in the process of teaching myself c++. I wrote one program successfully and I'm trying to write my second. I have run into a problem that I can not seem to figure out at all. I understand that this seems trivial, but I really need to grasp why this isn't working. The idea behind the program is that the user enters 5 baseball players' names and batting averages. The user is then prompted to enter a player's name to receive their average. Here is the code:
#include <iostream>
#include <cstring>
using namespace std;
void displayInstructions(void)
{
cout << "This program is created to allow you to enter \n"
<< "up to 5 baseball players' names and averages and then \n"
<< "enter a player's name to receive their average. \n"
<< endl;
cout << "To stop entering names, enter the word 'end' when asked \n"
<< "for the player's name."
<< endl;
}
int main()
{
class ballPlayer{
public:
char name[30];
int avg;
};
displayInstructions();
int i=0;
int players=0;
int totalPlayers=0;
ballPlayer player[5];
char quit[]="end";
for(i=0;i<=4;i++)
{
cout << "Please enter a player's name:"
<< endl;
cin.getline(player[players].name,30);
if(player[0].name == "end")
{
cout << "Please enter at least one batter's name."
<< endl;
cin.getline(player[players].name,30);
}
else if(player[players].name == quit && i != 0)
{
break;
}
cout << "Please enter this player's average:"
<< endl;
cin >> player[players].avg;
if(player[players].avg > 1000)
{
cout << "This is not a valid batter's average. \n"
<< endl;
cout << "Please enter a valid bater's average:"
<< endl;
cin >> player[players].avg;
players++;
totalPlayers++;
}
else
{
players++;
totalPlayers++;
}
}
char endBatter[30];
enterName:
cout << "Enter the player's name to get their average:"
<< endl;
cin >> endBatter[0];
players=0;
for(i=0;i<=totalPlayers;i++)
{
int isMatch = strcmp(player[players].name,endBatter);
if(isMatch == 0)
{
cout << player[players].name << "'s average is: "
<< player[players].avg << endl;
continueDecision:
cout << "Would you like to enter another player? Y/N"
<< endl;
char yesNo[4];
cin.getline(yesNo,4);
if(yesNo == "y" || yesNo == "Y")
{
goto enterName;
}
else if(yesNo == "n" || yesNo == "N")
{
break;
}
else
{
cout << "You did not enter a valid answer."
<< endl;
goto continueDecision;
}
if(isMatch != 0 && players == totalPlayers)
{
cout << "You did not enter a valid player's name. \n"
<< endl;
goto enterName;
}
}
}
return 0;
}
As of right now, everything goes smoothly up until after the user enters the first player's batting average. It then prompts the following:
"Please enter a player's name:
Please enter this player's average:"
Here are the problems I am experiencing:
1. It doesn't allow users to enter the batter names of the final four players, only their averages. Because of this, I can not test the portion of code that has the user enter the word "end" to stop entering players.
2. When I enter the word "end" for the first batter, the program should prompt that the user needs to enter at least one batter, but instead it just accepts "end" as the player's name.
3. After the user enters the name of the batter that they want to receive the average for, the program quits instead of running the loop to display the correct average.
I understand that this code is most likely severely flawed and poorly written, but any help would be much appreciated and would greatly aid me in better understanding this language.
-D