繁体   English   中英

使用Structure卡在C ++代码上

[英]Stuck on C++ code using Structure

目前,我被困住了,不知道从这里去哪里...

我应该编写一个程序,该程序声明一个用于存储播放器数据的结构。 然后声明一个由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的功能(即使它有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的原因是使函数的用户可以看到它保证不会更改值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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