Constructive Feedback Needed

I did exercise 2) of chapter 9 in Stroustrup's book. That chapter is about classes, its members, enumerations, constructors, etc. This is my first attempt at writing a program with classes containing public and private members that are both data variables and functions. It compiled and ran correctly,BUT.

The exercise was a continuation of another exercise in an earlier chapter. That exercise only required writing functions, no classes. I thought I could cut and paste that program into this one and just adapt the class. I couldn't and that's where I need feedback. I was getting many compile errors saying that the subscript needed to be used with an array type. My IDE (MS VC++ Express) underlines errors and gives hints as to the solution. The solution was to write
1
2
vector<string>name;
function(vector<string>name);

instead of
1
2
vector<string>name;
function(string name);


In other words, my program seems to work correctly but I don't understand why.
This is 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
/*
This program solves an exercise.  The requirement is to first create a class holding a vector of names of individuals and a vector 
of their ages as members.  Then write a function that reads the names into the vector, a function that reads their ages into the 
vector, a function that sorts the names alphabetically and also sorts the ages to remain matched to the names, and finally a 
function that prints out the sorted names and ages.  These functions should be members of the class.
*/

#include "../../std_lib_facilities.h"

class Name_Age {
   private:
	   vector<string>Names;
           vector<string>Names2;
 	   vector<double>Ages;
	   vector<double>Ages2;
	   string name;
	   string name2;
	   double age;
	   double age2;

   public:
	   void Read_names(void)
{
	   cout <<"Enter the names of individuals.  One name per line.\n";
	   cout <<"Enter 'last' to signal the end of the list.\n";
 
	   while (cin >> name) {
		 if (name == "last") {
			Read_ages();
			break;
		 } else {
		 Names.push_back(name);
		 }
	   }
}

	   void Read_ages(void)
{
	   cout <<"Enter the ages of the individuals.  One age per line.\n";
	   for (int i = 0; i < Names.size(); ++i) {
		   cin >> age;
		   Ages.push_back(age);
	   }
	   Sort(Names, Ages);
}

	   void Sort(vector<string>Names, vector<double>Ages)  
{
	   for (int k = 0; k < Names.size(); ++k) {       // A copy of Names, called Names2, is created in order that the unsorted 
		   name2 = Names[k];                      // order of the names can be used to match the ages to the sorted names.
		   Names2.push_back(name2);
	   }

  	   sort(Names2.begin(), Names2.end());

	   for (int i = 0; i < Names2.size(); ++i) {
		   for (int j = 0; j < Names2.size(); ++j) {
			   if (Names2[i] == Names[j]) {
				   age2 = Ages[j];
				   Ages2.push_back(age2);
			   }
		   }
           }
	   Print(Names2, Ages2);
}

	   void Print(vector<string>Names2, vector<double>Ages2)
{
	   for (int i = 0; i < Names.size(); ++i) {
  	      cout << i+1 <<"  "  << Names2[i] <<"  "  << Ages2[i] <<endl;
	   }
}
};

int main () 
{
	Name_Age Test;

	Test.Read_names();

        keep_window_open();
        return 0;
}


Thanks in advance for your comments.
I've briefly skimmed your program; it's too late to properly look at that for loop on line 56. Here are some comments:

1) When working with string objects, use getline() to read data. Line 27 for example, would look like this: while( getline(cin, name) )

2) I personally think that "Read_names()" and "Read_ages()" should be in the same function as a design preference.

3) Whenever you use cin>> you need to follow that line with cin.ignore(). Ideally you would use cin.ignore(numeric_limits<streamsize>::max(), '\n'); but at the very least you the first suggestion.


In regards to your error messages, on line 44 you call the fucntion "Sort()" with parameters that are of type vector<string> and vector<double> respectivley but your function prototypes/definitions must have said you only expected a string object and a double variable.
Last edited on
Thanks for your comments, mcleano.

1) The getline() function is explained in chapter 11. I am at chapter 9. Chapters 10 and 11 are about input and output formats. So I will get there soon.

2) How would that be done? Would it be, say, "Read_variable" and the user would define the variable being read and the vector into which it is being read?

3) I haven't yet covered cin.ignore() in the Stroupstrup book. I did read in an online tutorial about problems with cin >> that are resolved with cin.ignore(). I didn't fully understand those problems, but I think I will get to it soon as well.

4) I learned of why I got those error messages from another poster. In line 44, I call the sort function and name 2 arguments. (The same for print() in line 64.) When I defined the function in line 47, I needed to included arguments as well. There was a mismatch between the argument types in line 44 and that in line 47.

I should have not included any arguments when I called sort() in line 44. Then I would not have needed to include any arguments in the function definition in line 47.
1) I completely understand. However, I would urge you to start using getline() even though you have not covered it yet (you can find a reference to it here for more information: http://www.cplusplus.com/reference/string/getline/). There are some things that beginnger books, although probably not from the creator of the language lol, teach you that aren't always best. For example, the beginner book that I used told me to use the C function gets() to get string input - even though this is an unsafe function.

2) About to eat dinner so I'll finish this later lol.

3) The same as one really. Use it and learn it at the same time, even if it i before it appears in your tutorial. You'll find that answers to it's use come up quite frequently on the forum.
Topic archived. No new replies allowed.