Question: need help with some C++ homework. I posted about this before, but the answer wasn't correct. We CAN'T use pointers for this. I'm trying to
need help with some C++ homework. I posted about this before, but the answer wasn't correct. We CAN'T use pointers for this. I'm trying to work around it. Here is the assignment:
A menu-driven program gives the user the option to find statistics about different baseball players. The program reads from a file and stores the data for ten baseball players, including players team, name of player, number of homeruns, batting average, and runs batted in. (You can make up your data, or get it online, for example: http://espn.go.com/mlb/statistics )
Write a program that declares a struct to store the data for a player. Declare an array of 10 components to store the data for 10 baseball players.
The program prints out a menu (in a loop, so this can be done again and again) giving the user a choice to:
print out all users and statistics
print out the statistics for a specific player
print out all data for a specific team
update the data for a particular player (change one of the statistics)
DO ALL WORK IN FUNCTIONS. USE A FUNCTION TO READ THE DATA, A FUNCTION TO PRINT THE MENU, and FUNCTIONS FOR EACH OF THE MENU OPTIONS.
Before the program terminates, give the user the option to store the data in an output file.
Here is my text file data:
1 Astros JoseAltuve 3 .714 5 2 Twins EduardoEscobar 2 .500 0 3 Twins BrianDozier 2 .500 1 4 Astros EvanGattis 3 .500 2 5 RedSox MookieBetts 3 .375 1 6 RedSox HanleyRamirez 2 .333 0 7 Indians JayBruce 3 .333 3 8 Yankees AaronHicks 4 .333 1 9 Twins MaxKepler 1 .333 0 10 Twins EddieRosario 1 .333 1
I am having 2 problems with my code #1 it won't printall the above data correctly. I just get a bunch of zeroes and I can't figure out how to store new data that user inputs.
here is the code:
#include
#include
#include
#include
#include
using namespace std;
const int SIZE = 10;
struct player { //structure of player
int id;
string team;
string name;
int homeruns;
int batting_avg;
int runs;
}object[SIZE]; //reference of array of structure
void loadData();
int loadMenu(int &);
void printAll();
void printSpecific(int );
void printSpecificTeam(string );
void updatePlayer(int , string );
int storeData();
int main() {
int option, choice, idPlayer, k;
string teamName;
do { //menu to be displayed
loadMenu(option);
switch (option) { //different options methods
case 1: printAll();
break;
case 2:
cout << "enter id";
cin >> idPlayer;
printSpecific(idPlayer);
break;
case 3:
cout << "enter team";
cin >> teamName;
printSpecificTeam(teamName);
break;
case 4:
cout << "enter id and team you wnat to update";
cin >> idPlayer >> teamName;
updatePlayer(idPlayer, teamName);
break;
case 5:
k = storeData();
if (k == 1)
cout << "Successfully stored";
else
cout << "error. not stored";
break;
case 6: exit(0);
break;
default: cout << "Oops something went wrong!";
}
cout << "Continue?(Y/N)";
cin >> choice;
} while (choice == 'y' || choice == 'Y');
}
void loadData() { //read data from file
ifstream in("player.txt");
int count = 0;
while (!in.eof() && count if (!in) { cout << "File can't be opened! " << endl; } in >> object[count].id >> object[count].team >> object[count].name >> object[count].homeruns >> object[count].batting_avg >> object[count].runs; ++count; } } int loadMenu(int &option) { do { cout << "Enter your choice " << "1. Print out all players and statistics " << "2. Print out the statistics for a specific player " << "3. Print out all data for specific item " << "4. Update the data for a particular player " << "5. Store data in file " << "6. Exit "; cin >> option; if (option < 0 || option > 7) { cout << "Invalid selection. Try again. "; } } while (option < 0 || option > 7); return option; }; void printAll() { //print data after reading from file loadData(); for (int i = 0; i < SIZE; i++) { cout << object[i].id << " "; cout << object[i].team << " "; cout << object[i].name << " "; cout << object[i].homeruns << " "; cout << object[i].batting_avg << " "; cout << object[i].runs << " " << endl; } } void printSpecific(int idp) { //print data of specific player for (int i = 0; i if (object[i].id == idp) { cout << object[i].id << " "; cout << object[i].team << " "; cout << object[i].name << " "; cout << object[i].homeruns << " "; cout << object[i].batting_avg << " "; cout << object[i].runs << " " << endl; break; } } } void printSpecificTeam(string teamName) { //print data of specific team for (int i = 0; i if (object[i].team == teamName) { cout << object[i].id << " "; cout << object[i].team << " "; cout << object[i].name << " "; cout << object[i].homeruns << " "; cout << object[i].batting_avg << " "; cout << object[i].runs << " " << endl; break; } } } void updatePlayer(int idp, string teamName) { //update particular player team by searching it through id for (int i = 0; i if (object[i].id == idp) { object[i].team = teamName; break; } } } int storeData() { //store data in file ofstream outFile; outFile.open("player.txt"); if (!outFile) { cout << "File can't be opened! " << endl; } return 1; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
