使用Structure卡在C++代码上

Stuck on C++ code using Structure

本文关键字:代码 C++ 卡在 Structure 使用      更新时间:2023-10-16

目前我被卡住了,不确定从这里到哪里。。。

我应该编写一个程序,声明一个结构来存储玩家的数据。然后声明一个由10个组件组成的数组来存储10名棒球运动员的数据。

该程序从文件中读取并存储十名棒球运动员的数据,包括球员所在的球队、球员姓名、全垒打数、打击率和打击率。

该程序打印出一个菜单(在一个循环中,这样就可以一次又一次地完成),让用户可以选择:

  • 打印出所有用户和统计信息
  • 打印出特定玩家的统计数据
  • 打印出特定团队的所有数据
  • 更新特定玩家的数据(更改其中一个统计数据)

在程序终止之前,为用户提供将数据存储在输出文件中的选项。

如果有人有任何提示或建议,我将不胜感激。。。我对C++编程还很陌生,只是停留在这里。。。提前感谢。。。

#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];
    }
}

您做得对的一件事是生成printInfoAll的函数(即使它有缺陷)。请注意,这将不会编译,并且可能会有更多错误。

#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;
}

在参数中添加const的原因是,函数的用户可以看到它承诺不更改值。