使用Structure粘贴在C ++代码上

时间:2014-11-03 01:08:34

标签: c++ xcode

目前我被困住了,不知道从哪里开始......

我应该编写一个程序,声明一个结构来存储玩家的数据。然后声明一个包含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];
    }
}

1 个答案:

答案 0 :(得分:0)

你做得对的一件事就是制作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添加到参数的原因是,函数的用户可以看到它承诺不更改值。