Classes and Soccer :)

hello everyone :)

My proffesor gave me this exercise to build a class called Team which represent a soccer team in some Soccer Championship :P with attributes like TeamName (20 letters max), GoalsScored, GoalsTaken, GamesPlayed, GamePoints and methods like NewGame(which should set GoalsScored and GolasTaken to some values), Points(for every goal a team gets 3 points,for every tie 1 point), TeamName (which should return a pointer on the first letter in the name of the team) etc..
You will see the rest int the code below(its not complicated:) )..

I'm having troubles with the team name. I dont know should it be a pointer on a char attribute or a char set. Because it can be 20 letters max long, i probably should write a method which determines the name length?! And what about the TeamName method, how to implement that? I tried compiling this, but i get an error message for the line 42 saying "passing `const Tim' as `this' argument of `int Tim::DuzinaImena(char*)' discards qualifiers ".

Help! This isn't some homework assignment or something like that, take your time :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <conio.h>

using namespace std;

class Team{
      char *name; // I tried using a pointer but it doesnt work

      int games_played, wins, ties, goals_scored, goals_taken,   loses,total_points;

   
   public:
      Team(){
        games_played=0; wins=0; ties=0; 
goals_scored=0; goals_taken=0; loses=0; total_points=0;
        };
      void NewGame(int goals_scored, int goals_taken);
      int NameLength(char *name);
      const char *TeamName() const;
      int Points() const {return total_points;}
      int GoalDifference() const {return goals_scored-goals_taken;}
      void PrintData() const ;

};

int Team::NameLength(char *name){ // Is this right ?
    int counter(0);
    while(name[counter]!=0 && counter<20) counter++;
    return counter;
};

void Team::NewGame(int goals_scored, int goals_taken){
     games_played++;
     if(goals_scored>goals_taken){ wins++; total_points+=3;}
     if(goals_scored<goals_taken) loses++;
     if(goals_scored==goals_taken) ties++;
     };
     
void Team::PrintData() const{
     cout<<"Info on the team: ";

     for(int i = 0 ; i < NameLength(name) ; i++) cout<<name[i];

     cout<< "\n--------------------------------\n";
     cout<< "Games played: " << games_played << " \n ";
     cout<< "Goals scored: " << goals_scored << "\n" ;
     cout<< "Goals taken: " << goals_taken << "\n";
     cout<< "Wins: " << wins << "\n" ;
     cout<< "Loses: " << loses << "\n" ;
     cout<< "--------------------------------\n";
     cout<< "Points: " << total_points << "\n" ;
     };

     
int main(){

    getch();
    return 0;
}


This would have been a lot easier if I could use strings, but no, my proffesor wants me to do it the hard way :D I implemented some methods in the class interface..I dont speak english very well,so sorry if made some mistakes..I'm using BloodShed Dev-C++ builder which uses MinGw compiler..

Last edited on
Name length can be determined by strlen function call. As for storing the team names, you can use either char * or
char name[20]; . If you use char * then make sure you allocate enough memory before copying into it.

Ex.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 

int main() { 

char firstName[20]; 
strcpy(firstName, "David"); 

char *lastName; 
lastName = new char[20]; 

strcpy(lastName, "Beckham") ; 

delete [] lastName; 
return 0; 
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
bool Team::NameLength(char *name){ 
    if(strlen(name)<=20) 
       return true;
    else 
       return false;
};

void Team::setName() {
     char *str;
     cout << "Please enter a team naem(max 20 chars): ";
     cin >> str;
     cin.ignore();
     if(NameLength(str))
        strcpy(name, str);
     else ;
        // get input again...
}; 


An idea for your member functions ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bool Team::NameLength(char *name){ 
    if(strlen(name)<=20) 
       return true;
    else 
       return false;
};

void Team::setName() {
     char *str;
     cout << "Please enter a team naem(max 20 chars): ";
     cin >> str;
     cin.ignore();
     if(NameLength(str))
        strcpy(name, str);
     else ;
        // get input again...
}; 


An idea for your member functions ^


Except for the fact that it will give Segmentation fault since you did not allocated memory for str to store user input value.
You have to allocate memory for name and str or declare them as arrays.
Last edited on
i have made some progress myself..since i am new to programing i am trying to do all the work using loops, not using functions like strlen or strcpy ..

the name length function i wrote works just fine..


printing of name:

1
2
3
4
char *Team::TeamName()const{
     for(int i=0 ; i < NameLength() ; i++) cout<<name[i];   
};


my NameLength function will print the name if its under 20 letters long, but i had to figure out what to do if its not, and kevin solved this problem for me..thnx and thnx for the tip on memory alocation ;)
Except for the fact that it will give Segmentation fault since you did not allocated memory for str to store user input value.
You have to allocate memory for name and str or declare them as arrays.

Sorry in my head I set name as an array char name[20];

[Edit]
You don't have to allocate memory for str
Last edited on
any ideas how to implement function Name(const char name[]) which should return a pointer
on the first letter of the word stored in name[] ??
Last edited on
Could you not get that input in main, then add that argument to the constructor?
@mcleano:

[Edit]
You don't have to allocate memory for str


str is a pointer. And while taking user input, which could be string of any length, you have to allocate memory to store that string somewhere. Now when you say cin>>str; , it means that store the user input in the memory pointed by str (since str is a pointer). However, str is not pointing to large enough memory to store the user input. Therefore, command will terminate. Well, technically, str will be pointing to some random memory, but that memory could very well be the read-only memory. Who knows!
So, in short, you HAVE to allocate memory for str.

Unless, if you could please explain me why we don't have to allocate memory for str. :)



Ahhhh what was I thinking! I stand corrected by kevinchkin!
@kevin
i did have to allocate memory for the pointer, since i was getting a "bug report" error :s

anyway, i finished the class, its working perfectly..thnx guys ;)
Topic archived. No new replies allowed.