How to Populate a Vector Defined in a Class

I am trying to do an exercise. To test parts of the program, I want to populate some vectors and then read the data from these vector. The program I wrote compiles and runs, just not correctly. The program doesn't seem to be accessing the data in the populated vectors. When I run a function, the output doesn't print. The program goes directly to the end of program message (Enter a character to exit.) The error exceptions work correctly. I am least confident about the way I populated the vectors, using a Put_???? function. It was the best I could do to adapt the examples of constructors to this exercise.
What have I done wrong? Thanks.

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
class Book {
      private:
         vector<string>ISBNs;
         vector<string>Titles;
         vector<string>Authors;
         vector<int>CR_Dates;
         vector<char>Checked_In;

      public:
         void Get_author() 
         {
	 string title = " ";
	 int crdate = 1900;
         cout <<"Please enter the book's title and copyright date, separated by a space.\n";
         cin >> title >> crdate ;
		 
  	if (crdate < 1900 || crdate > 2010) throw errorcrdate;   // Copyright dates of books are after 1900 up to the current year.
	   for (int j = 0; j < title.length(); j++) {             // Verifies that the characters of the book's title are letters.
	      if (isalpha(title[j])) {
		 continue;
	      } else {
		 throw errorauthor;
	      }
	}

	for (int i = 0; i < Titles.size(); i++) {
	   if (title == Titles[i] && crdate == CR_Dates[i]) {
	      cout << "The author of the requested book is " << Authors[i] << ".\n";
	   } else {
	      cout << "There is no author in our records corresponding to your book title and copyright date.\n";
	   }
	}
     }

SNIP

     void Put_Titles() 
     {
        Titles[0] = "CatintheHat";
        Titles[1] = "CharlottesWeb";
        Titles[2] = "NineteenEightyfour";
        return;
		 }

     void Put_CR_Dates() 
     {
        CR_Dates[0] = 1957;
	CR_Dates[1] = 1952;
        CR_Dates[2] = 1949;
	return;
     }

     void Put_Authors() 
     {
	Authors[0] = "DrSeuss";
	Authors[1] = "EBWhite";
        Authors[2] = "GeorgeOrwell";
	return;

SNIP
};
SNIP

It sounds to me like you just need a constructor to populate the vectors when the object is created.
1
2
3
4
5
	Book() //Constructors have no return type
	{
		//Setup the vectors in here!
		//Constructors should not return... at all
	}


http://www.cplusplus.com/doc/tutorial/classes/ see "Constructors and Destructors"

Also, you should indent properly, it makes your code much easier to read if you use the correct indention.
Last edited on
@LB,

Thanks for your reply. I created a constructor in which the vectors are populated with the values I want.

1
2
3
4
5
6
		 Book() 
		 {
			Titles[0] = "CatintheHat";
	                Titles[1] = "CharlottesWeb";
                        Titles[2] = "NineteenEightyfour";
SNIP


The revised program compiles. However, I seem to have made another mistake which is leading to a range error upon running. I am re-checking my vectors and subscripts.

Re: Indention,

I use MS VC++ 2010 Express. It indents code automatically. When I cut and paste code into the forum, the VC++ format is completely erased. I try to re-indent the best I can.
before you can populate your vectors with such code, you must size them, for ex like this:
1
2
3
4
5
6
7
Book()
  :Titles(3)
  ,Authors(3)
{
  Titles[0]="Foo";
  Authors[3]="Goo";//out of range here
}

or you can use push_back function like this:
1
2
3
4
Book()
{
  Titles.push_back("Foo");
}
@vukki,

Thank you for your suggestions. Sizing the vectors was the problem. Both methods worked.
Topic archived. No new replies allowed.