简体   繁体   中英

Stuck on C++ code using Structure

Currently I am stuck and not sure where to go from here...

I'm supposed to write a program that declares a struct to store the data for a player. Then declare an array of 10 components to store the data for 10 baseball players.

The program reads from a file and stores the data for ten baseball players, including player's team, name of player, number of homeruns, batting average, and runs batted in.

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)

Before the program terminates, give the user the option to store the data in an output file.

If anyone has ANY TIPS OR ADVICE I will be very grateful... I'm fairly new to coding in C++ and just stuck here... thank you in advance...

#include <iostream>
#include <fstream>
using namespace std;

struct BaseballID
{
    string teamName, playerFirstName, playerLastName;
    int homeRuns, rbi;
    double batting_average;
};

int main()
{
    BaseballID listofplayers[10];
    ifstream infile;

    infile.open("/users/AlecKleyer/Desktop/computer science term 2/BaseballStats.txt");

    if (!infile)
    {
        cout << "Error opening file!";
        return 0;
    }

    for (int j = 0; j < 10; j++) {
        infile >> listofplayers[j].teamName >> listofplayers[j].playerFirstName >> listofplayers[j].playerLastName >>listofplayers[j].homeRuns >> listofplayers[j].rbi >> listofplayers[j].batting_average;
    }
    cout << "Please Type The Following Letter: ";
    cout << "\n(A) For All Users and Stats";
    cout << "\n(B) For A Specific Player";
    cout << "\n(C) Print out for specific team";
    cout << "\n(D) Update stats for a player";

    char input = 0;
    cin >> input;

    if (input == 'A' || input == 'a') {
        printInfoAll(*listofplayers[]);
    }
    if (input == 'B' || input == 'b') {

    }
    if (input == 'C' || input == 'c') {

    }
    if (input == 'D' || input == 'd') {

    }

}

void printInfoAll(listofplayers1[])
{
    for (int i = 0; i < 10; i++) {
        cout << &listofplayers[i];
    }
}

One thing you did right was making a function of printInfoAll (even if its buggy). Note this will not compile and there might be more errors.

#include <iostream>
#include <fstream>
using namespace std;

struct BaseballID
{
    string teamName, playerFirstName, playerLastName;
    int homeRuns, rbi;
    double batting_average;
};

void printInfo(const BaseballID& id) {
  cout << id.teamName << " " << id.playerFirstName // and so on!!!!
       << "\n"; // and a newline.
}

void printInfoAll(const BaseballID listofplayers1[]) // we need a type and a paramter
{
    for (int i = 0; i < 10; i++) {
        cout << printInfo(listofplayers[i]); // you 
    }
}

void printMenu() { // factor out the components in easy reusable parts.
    cout << "Please Type The Following Letter: ";
    cout << "\n(A) For All Users and Stats";
    cout << "\n(B) For A Specific Player";
    cout << "\n(C) Print out for specific team";
    cout << "\n(D) Update stats for a player";
}


int main()
{
    BaseballID listofplayers[10]; // consider using std::vector instead


    // from here
    ifstream infile;

    infile.open("/users/AlecKleyer/Desktop/computer science term 2/BaseballStats.txt");

    if (!infile.isOpen()) // I think the isOpen is needed.
    {
        cout << "Error opening file!";
        return 0;
    }

    for (int j = 0; j < 10; j++) {
        infile >> listofplayers[j].teamName >> listofplayers[j].playerFirstName >> listofplayers[j].playerLastName >>listofplayers[j].homeRuns >> listofplayers[j].rbi >> listofplayers[j].batting_average;
        // hmm you trust your indata I don't check for errors here. 
    }
    // to here should be a function, but then you need to restructure a bit more

    printMenu();

    char input = 0;
    cin >> input;

    switch(input) {
      case 'A': case 'a':
        printInfoAll(*listofplayers[]);
        break;
      case 'B': // and so on
        // code for 'B' and 'b'
        break;
      ....
      default:
          printMenu();
          break;
    }

    // at this point you will find out you should have put it all in a loop.

    return EXIT_SUCCESS;
}

The reason for adding const to the parameters is so that the user of the functions can see it promises not to change the values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM