2011-03-21 73 views
0

我一直在使用类来创建一个C++生成的幻想足球草案的斗争。我觉得我对课程有了一个很好的理解,但是在这种情况下实施它们时遇到了麻烦。我想上传我写的代码,并且在代码中主要针对实施“团队”类和“玩家”类有一些具体的评论。Fantasy Football - Implementing Classes Trouble

任何和所有帮助赞赏 此外,如果我没有正确上传,请让我知道。

// main.cpp - Where the winners are crowned 
// Written by Mike Green 

#include <iostream> 
#include <string> 

using namespace std; 

void add_name_at_end(); 
void traverse(); 


// Declaration of name, and the -> next pointer 
struct Names 
{ 
    string name; 
    string name2; 
    struct Names *next; 

}; 
Names *start_ptr = NULL; 
int option = 0; 


// ---------------------------Adding of players to Draft----------------------------- 
void add_name_at_end() 
{ 
    // Temporary pointers 
    Names *temp, *temp2; 

    // Reserve space for a new node 
    temp = new Names; 
    cout << "Please enter YOUR chosen team name:"; 
    cin >> temp->name; 


    temp->next = NULL; 


    // Sets the pointer from this node to the next NULL 
    // If list empty at beginning just set start pointer to this node 
    if (start_ptr == NULL) 
     start_ptr = temp; 
    else 
    { 
     temp2 = start_ptr; 
     while (temp2->next != NULL) 
     { 
      temp2 = temp2->next; 
     } 

      temp2->next = temp; 
    } 

} 

// --------------------------Traversing the List------------------------------ 
void traverse() 
{ 
Names *temp; 
temp = start_ptr; 

do 
{ 
if(temp == NULL) 
cout << "These are the teams: " << endl; 
else 
{ 
    cout << "Team Name: " << temp->name << endl; 
    temp = temp->next; 

    } 
}while (temp != NULL); 
} 

// -------------------------Team Class---------------------------- 
class team 
{ 
    // -------------String "Name" or "Names" here?---------------- 
    //---------------How to recall what they enter as their team name? Or how does this work?---------------- 
    //---------------Really stuck here..------------------ 
    string Name; 
    int points; 
    team *myPlayers; 
    int insert; 

}; 


//------------------------Players Class------------------------- 
class players 
{ 
    // ------------Used to store the list of players that are draftable--------------- 
    //----------------Read in a file, Or create an array? 
    string Name; 
    int points; 
    bool taken; 

}; 



//-----------------Main--------------------------- 
int main() 
{ 
    start_ptr = NULL; 
    cout << "Welcome to Fantasy Football" << endl; 


    //int numberofPlayers; 
    //cout << "How many players will be playing today? (1-8)"; 
    //cin >> numberofPlayers; 
    //cout << "There will be " << numberofPlayers << " players playing today." << endl; 

    //if (numberofPlayers < 0 || numberofPlayers > 8) 
    // cout << "You did not enter a number between 1 and 8" << endl; 

    do 
    { 
     cout << endl; 
     cout << "What would you like to do? " << endl; 
     cout << "1 - Insert Another Player For The Draft" << endl; 
     cout << "2 - Finished Adding Players " << endl; 
     cout << endl << " >> "; 
     cin >> option; 

     switch(option) 
     { 
     case 1: add_name_at_end(); 
      break; 
     case 2: break; 
     } 

    }while (option != 2); 

    if (option == 2) 
    { 
     cout << "These are the teams that will be drafting " << endl; 
     traverse(); 
    } 


    // ------------------This Loop does not work-------------- 
    // - Can't use team because it is a class? 
    // - Name is unidentified? 
    // - Still need to write the insert function.. But Where?? 
    for (int i = 1 || 2 || 3 || 4; i < 5; i++) 
    { 
     cout << team[i] << "chooses: " << endl; 
     cin >> Name; 
     team[i].insert(Name); 
    } 

    system ("pause"); 
} 

回答

0

这是不对的:

for (int i = 1 || 2 || 3 || 4; i < 5; i++) 

我不知道你想在这里做什么。

team是一个类,但是您将其作为数组引用。你的意图是什么?

我建议使用标准容器(向量,双向变换等)而不是滚动自己的链表(除非它是实现列表的要点)。

放弃全局变量的习惯。

失去system("pause");system("pause"); - Why is it wrong?

+0

那么我想说我= NumberofTeamsDrafting;我<5;我++ - 因为最高队的起草将是4 ..不知道该怎么说。 – Mike 2011-03-21 22:20:05

+0

@Mike:然后你需要计算队伍的数量。初始化'i'为零并且计数到该数字。你可能想强制执行“团队”填充的限制。 – 2011-03-21 22:23:20

+0

好的,现在我想和团队一起做的就是为第一个起草者即迈克的团队说话,然后他起草一个球员,并且我想把它发给他的球队[我],基本上说,迈克的球队选择:“球员” ,然后将玩家插入Mike的团队。接下来,这是我对最终项目的想法,没有给出任何标准,所以我可以使用任何最简单的方法,但很明显,我不是最熟练的程序员。至于全局变量,我很困惑。 感谢您的帮助。 – Mike 2011-03-21 22:32:14