Storing user input in an array of objects

I've always had a hard time with arrays of objects when it comes to storing user input for each objects members and then outputting them. I created a class called VideoGame and an array of objects called Games[5]. What I'm trying to do here is get user input for each data member in an object, store that user input in its corresponding data member, and then in the end, output the result of the object in question such as Games[0]. I know how to create a normal array using user input for each element, and then outputting a given element from the array, but the problem I'm having here is I'm not sure how to do that same thing for array of objects.

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
 #include <iostream>
#include <string>
using namespace std;

class VideoGame
{
public:
	VideoGame(string t, string g, char r, string d): //constructor
		title(t), genre(g), rating(r), developer(d)
	{
	}

	VideoGame() //default constructor
	{
		title;
		genre;
		rating;
		developer;
	}

	 void SetName(string &t) 
	{
		 title = t;
	}

	 VideoGame DisplayName(VideoGame &game )
	 {
		 cout << game.SetName();
	 }



private:            
	string title;
	string genre;
	char rating;
	string developer;

};

int main()
{

	VideoGame Games[5];

	cout << "Enter a game name: " << endl;
	Games[0].SetName(cin);
	Games[0].DisplayName(Games[0])




}
Will a for loop help you ?

And some parts of your code has issues :

the body of the default c'tor doesn't make sense :
1
2
3
4
5
6
7
VideoGame() //default constructor
{
	title;
	genre;
	rating;
	developer;
}


void SetName(string &t)
it would be better if you pass it as const :
void setName( const string& str )

1
2
3
4
VideoGame DisplayName(VideoGame &game )
{
	cout << game.SetName();
}
I think this code should be a non-member instead ?
The return type should be void,
And cout will not work.

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
class VideoGame
{
public :
        // constructors

        void setTitle( const string& title ) { this->title = title; }
        string getTitle() { return title; }

        // other members

private :
        string title;
        // other members
};

int main ()
{
    VideoGame games[ 5 ];
    string foo;
    
    for( size_t i = 0; i < 5; i++ ) {
        cin >> foo;
        games[ i ].setTitle( foo ); // set title
        cout << games[ i ].getTitle(); // print title
        // ...
    }
}
Last edited on
Once you fix your code problems, an array of objects is just the same as any other array. You reference objects by the correct index and treat that single object as you would treat a single instance of the object.
1
2
3
4
5
6
7
8
9
10
11
12
class car
{
    int cost; 
}; 

car mycars[5]; //Declare an array of 5 objects. 

int i;
for(i = 0; i < 5; ++i)
{
    std::cin >> mycards[i].cost; //Set the cost of each car in the array. 
}
Last edited on
Thanks you guys for your help. I modified my program and got it to build without any error, but it's not entirely executing the way that it should. Everything seems to be working normally, except for when it gets to the part about asking for input on the games dev. It just kind of skips over that part and jumps right to the end of the program. I can't see anything that looks wrong with that part of the program.

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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <string>
using namespace std;

class VideoGame
{
public:

	VideoGame(){}

	explicit VideoGame(string t, string g, char r, string d): //constructor
		title(t), genre(g), rating(r), developer(d)
	{
	}


	void setitle(const string& title) 
	{
		this->title = title; 
	}

	string getTitle()
	{
		return title;
	}
	
	void setGenre(const string& genre)
	{
		this->genre = genre;
	}
		
	string getGenre()
	{
		return genre;
	}

	void setRating(const char& rating)
	{
		this->rating = rating;
	}

	char getRating()
	{
		return rating;
	}

	void setDev(const string& dev)
	{
		this->developer = dev;
	}

	string getDev()
	{
		return developer;
	}
	

	
private:            
	string title;
	string genre;
	char rating;
	string developer;

}; //End VideoGame class

int main()
{

	VideoGame Games[5];
	string info;
	char rate;

	for (size_t i = 0; i < 5; i++)
	{
		cout << "Enter a game name: ";
		getline(cin, info);
		Games[i].setitle(info);
		cout << "Entered game is " << Games[i].getTitle() <<".";
		cout << endl;

		cout << "Enter the game's genre: ";
		getline(cin, info);
		Games[i].setGenre(info);
		cout << Games[i].getTitle() << "'s" << " genre is " << Games[i].getGenre() << ".";
		cout << endl;

		cout << "Enter the games rating: ";
		cin >> rate;
		Games[i].setRating(rate);
		cout << Games[i].getTitle() << "'s" << " rating is " << Games[i].getRating() << ".";
		cout << endl;

		cout << "Enter the games developer: ";
		getline(cin, info);
		Games[i].setDev(info);
		cout << Games[i].getTitle() << "'s" << " developer is " << Games[i].getDev() << ".";
		cout << endl;

		cout << "Title: " << Games[i].getTitle() << endl;
		cout << "Genre: " << Games[i].getGenre() << endl;
		cout << "Rating: " << Games[i].getRating() << endl;
		cout << "Developer: " << Games[i].getDev() << endl;
		cout << endl;

	} //End for

return 0;

} //End main

the problem is w/ cin >> and getline()
getline() eat's the newline left by operator>>

To fix this you just have to put cin.ignore() before using getline()
1
2
3
cin >> foo;

getline( cin, bar ); // getline will eat the newline the >> left and continue unto the next statement 


1
2
3
cin >> foo;
cin.ignore(); //  discard characters
getline( cin, bar ) // okay ! 
Not to be off topic, but does anyone know of any good C++ books. I've been practicing with C++ now for about a month and am looking for something that I can read that is tailored more towards C++ beginners. I was looking at possibly getting:

http://www.amazon.com/Jumping-into-C-Alex-Allain/dp/0988927802/ref=sr_1_4?ie=UTF8&qid=1388024721&sr=8-4&keywords=C%2B%2B
Topic archived. No new replies allowed.