Problem with C vectors of class 'Team'

This is my code:

Team.h
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
//File header of class "Team".

#include <iostream>
#include <cstring>

//Prevent the multiple inclusion of the header file.
#ifndef TEAM_H
#define TEAM_H

using namespace std;

//Beginning of class "Team".
class Team {
	//Overloading the streams:
	friend ostream &operator<<(ostream &, const Team &);
	friend istream &operator>>(istream &, Team &);
	
	public:
		//Constructor:
		Team(const int = 0, const char * = "");
		Team(const Team &);
		
		//Overloading of other operators:
		const Team & operator=(const Team &);
		bool operator<(const Team &) const;
		bool operator>(const Team &) const;
		bool operator==(const Team &) const;
		bool operator!=(const Team &) const;
		bool operator<=(const Team &) const;
		bool operator>=(const Team &) const;
		
		//Set and get functions:
		const char * getTeamName() const;
		int getTeamScore() const;
		void setTeamName(const char * N);
		void setTeamScore(const int S);
		
		//Other functions:
		int AddScore(const int add) {
			return Score += add;
		}
		int SubScore(const int sub) {
			return Score -= sub;
		}
		
		//Deconstructor:
		~Team() {
			delete [] Name;
		}
		
	private:
		char * Name;
		int Score;
}; //End of class "Team".

#endif 


Team.cpp
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//File of the prototypes of class "Team".

#include <iostream>
#include <cstring>
#include "Team.h"

Team::Team(const int S, const char * N) {
	Score = S;
	Name = new char[strlen(N) + 1]; //Dynamically allocating the string.
	strcpy(Name, N);
}

Team::Team(const Team & T) {
	Score = T.Score;
	Name = new char[strlen(T.Name) + 1];
	strcpy(Name, T.Name);
}

const char * Team::getTeamName() const{
	return Name;
}

int Team::getTeamScore() const{
	return Score;
}

void Team::setTeamName(const char * N) {
	if(Name) {
		delete [] Name;
	}
	Name = new char[strlen(N) + 1];
	strcpy(Name, N);
}

void Team::setTeamScore(const int S) {
	Score = S;
}

const Team & Team::operator=(const Team & T) {
	if(this != & T) {
		if(Name) {
			delete [] Name;
		}
		Name = new char [strlen(T.Name) + 1];
		strcpy(Name, T.Name);
		Score = T.Score;
	}
	return *this;
}

bool Team::operator==(const Team & T) const {
	return (strcmp(Name, T.Name) == 0 && Score == T.Score);
}

bool Team::operator!=(const Team & T) const {
	return !(*this == T);
}

bool Team::operator<(const Team & T) const {
	return (Score < T.Score);
}

bool Team::operator>(const Team & T) const {
	return (Score > T.Score);
}

bool Team::operator<=(const Team & T) const {
	return !(*this > T);
}

bool Team::operator>=(const Team & T) const {
	return !(*this < T);
}

ostream & operator<<(ostream & os, const Team & T) {
	os << T.Name << ", " << T.Score;
	return os;
}

istream & operator>>(istream & is, Team & T){
	char buff[100];
	cout << "Insert the name of the team: ";
	is.getline(buff, 100);
	cout << "Insert the score of the team: ";
	is >> T.Score;
	T.setTeamName(buff);
	return is;
}


main.cpp
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
61
62
63
64
65
66
67
68
69
70
//File for testing.

#include <iostream>
#include <cstring>
#include "Team.h"

using namespace std;

int main(int argc, char** argv) {
	Team t1(24, "Barcelona");
	Team t2(16, "Real Madrid");
	Team t3, t4;
	Team vecTeam[5];
	
	cin >> t3;
	cout << endl;
	
	//Verifying overloaded operators:
	cout << "Team 1: " << t1 << endl;
	cout << "Team 2: " << t2 << endl;
	cout << "Team 3: " << t3 << endl;
	
	cout << "Confronting Team 1 and Team 2" << endl;
	cout << "Team 1 == Team 2: " << (t1 == t2)
	<< "\nTeam 1 != Team 2: " << ( t1 != t2 )
	<< "\nTeam 1 > Team 2: " << ( t1 > t2 )
 	<< "\nTeam 1 < Team 2: " << ( t1 < t2 )
	<< "\nTeam 1 >= Team 2: " << ( t1 >= t2 )
 	<< "\nTeam 1 <= Team 2: " << ( t1 <= t2 );
 	
 	//Verifying copy contructor:
 	Team * tptr = new Team(t1);
 	cout << "\nCopied Team: " << *tptr << endl;
 	
 	//Verifying the operator of assignment:
 	cout << "\nAssigning Team 2 to Team 3: ";
 	t3 = t2;
 	cout << t3;
 	
 	//Verifying set and get functions:
 	t4.setTeamName("Rome");
 	t4.setTeamScore(19);
 	cout << "\nTeam 4: " << t4.getTeamName() << ", " << t4.getTeamScore() << endl;
 	
 	//Verifying AddScore and SubScore functions:
 	t4.AddScore(4);
 	cout << "\nScore after adding: " << t4.getTeamScore();
 	t4.SubScore(3);
 	cout << "\nScore after subtracting: " << t4.getTeamScore();
 	
 	cout << endl;
 	cout << endl;
 	
 	//Verifying the vector of teams:
 	for (int i = 0; i < 5; i++) {
 		cin >> vecTeam[i];
	}
	
	cout << endl;
	cout << endl;
	
 	for (int i = 0; i < 5; i++) {
 		cout << vecTeam[i];
	}
 	
 	//Verifying the destructor:
 	delete tptr;
	
	return 0;
}


It all works except for the vector "vecTeam" in the first "for". When I arrive at that point it doesn't let me write the names of the teams, only the score. Why does it do that? It seems everything fine to me.
Last edited on
When you use std::cin::operator>>, you leave the newline at the end of the buffer. That means that when you try to use std::istream::getline, it stops immediately, as it immediately discovers the newline that you left in the buffer.

The easiest way to fix is to throw a std::istream::ignore immediately beforehand:
1
2
3
4
5
6
std::istream& operator>>(std::istream& is, Team& t) {
    char buf[100];
    is.ignore();
    is.getline(buf, 100);
    // ...
}


Of course, this will only work if the only thing left in the buffer is the newline character; there are ways to ignore all characters up to the first newline, but there are hundreds of examples of that out there, and this is probably enough for your purposes.

Also, normally you put the ignore directly after using std::cin >>, because if the last stream operation happened to be another getline you'll just end up ignoring the first character of your input. I didn't do it like that here for conciseness.
Last edited on
Oh, I got it.
Thanks for the help!
Topic archived. No new replies allowed.