2014-11-03 7 views
0

을 사용하는 C++ 코드에 붙어 있습니다. 현재 여기에서 어디로 가야할 지 모르겠습니다 ...구조체

플레이어에 대한 데이터를 저장할 구조체를 선언하는 프로그램을 작성해야합니다. 그런 다음 10 명의 야구 선수에 대한 데이터를 저장할 10 개의 구성 요소 배열을 선언합니다.

이 프로그램은 파일에서 읽고 플레이어의 팀, 선수의 이름, 홈런, 타율의 수 등 10 명 야구 선수에 대한 데이터를 저장하고, 실행이 타점.

프로그램 인쇄 아웃 메뉴 (이

  • 밖으로 인쇄 특정 선수
  • 밖으로 인쇄의 모든 데이터에 대한 통계를

    • 인쇄 밖으로 모든 사용자와 통계 : 루프에서, 그래서 이것은에 사용자에게 선택권을주는) 다시 다시 수행 할 수 있습니다 특정 t EAM
    • 업데이트는 특정 선수에 대한 데이터는 프로그램이 종료하기 전에,

    을 (통계 중 하나를 변경) 사용자에게 출력 파일에 데이터를 저장 할 수있는 옵션을 제공합니다. 사람이 나는 매우 감사하게 될 것입니다 어떤 조언 조언이있는 경우

    ... 난 ... 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("https://stackoverflow.com/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]; 
        } 
    } 
    
  • +0

    main()에서와 같이'listofplayers'를 정의하려면'typedef struct {} BaseballID; '와 같은'BaseballID' 구조체를 정의해야합니다. – Josh

    +0

    이 종류의 컨텍스트를 배웠습니다. C++ 지식에 대한 나의 천장이 작아서이 실험실은 저에게 어렵습니다. @Josh –

    +0

    배열을 임의의 크기로 하드 세트하는 대신 벡터 및 push_back을 사용하여 여러 요소를 처리 할 수 ​​있도록하는 것이 좋습니다. –

    답변

    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("https://stackoverflow.com/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를 추가하는 이유는 함수 사용자가 값을 변경하지 않기로 약속 한 것을 볼 수 있기 때문입니다.

    +0

    제안'void printInfoAll (const BaseballID listofplayers1 [])' –

    +0

    좋은 아이디어, const는 거의 항상 좋은 것입니다. – Surt