Function call is skipping! Dont know why

When I call the member function in the main function, two functions are working fine but the third one(print()) is not. The program stops after executing the read_ages function and nothing printed on the screen. This is really strange and I could not find any problem after spending hours and had to post it here.It is a very simple program but I cant find the bug. It is a multiple file program and I am using MinGW as a compiler.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//File 1

#include<vector>
#include<string>
class Name_pairs
{
	private:
	     std::vector<std::string>names;
	     std::vector<double> ages;
	public:
         void read_names();
         void read_ages();
         void print() const;
         void sort();

};


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
//File 2

#include <iostream>
#include "Name_pairs.h"
/*{\
This function will promt the user to enter as many name as
they want. When done, emter "Quit".Store the names in the
anmes vector data member.Names must have no spaces in them.
}
*/
void Name_pairs::read_names()
{
     std::cout<<"Enter a series of names."
     "\nPress the enter key between each name. "
     "\nEnter \"Quit\"  when finished"
     "\nnames cannot have spaces in them. ";
     std::string name = "";
     std::cin>> name;
     while (name != "Quit")
     {
		 names.push_back(name);
         std::cin>> name;
     }
}

/*{\
  This function will loop through the names vector and ask the
  user to enter the age of each person.
  Store the ages in the \verb|ages| vector.
  }
*/

void Name_pairs::read_ages()

{
	double age;
	for(int i; i < names.size(); ++i)
	{
		std::cout<<"Enter the age of "<<names[i] << " : ";
		std::cin>>age;
		ages.push_back(age);
	}
}

/*{\
 This function will print out each name  and age pair in the vectors.
 e.g
 (Bob, 23)
 (Sue,56)
}
*/

void Name_pairs :: print()const
{
	for(int i; i < names.size(); ++i)
		{
		std::cout<<"("<<names[i] << ","<<ages[i]<<")"<< std::endl;
		}

}

/*{\
  This function will sort the names vector in alphabatical order
  while aslo moving the ages to keep the two vectors synchronized.
}
*/

//void Name_pairs::sort()
//{

//}


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
//File 3
/**
{
\section{main driver}
The main driver is used to
test the class\verb|Name_pairs|.
\begin{enumerate}
\item Create adefault \verb|Name-pairs| object
\item invoke the member function \verb|read_names|
\item invoke the member function \verb|read_ages|
\item sort the pairs
\item print the pairs to the console
\end{enumerate}
}
*/
#include <iostream>
using namespace std;
#include "Name_pairs.h"

int main()
{
  cout << "Lab2: test the Name_pairs class" <<endl;
  Name_pairs np;
  np.read_names();
  np.read_ages();
  np.print();
  //np.sort();


}

//I havent made the sort function yet since I am stuck with the print().Seems //like the compiler is skipping the print() function. 
1
2
3
4
5
6
7
8
void Name_pairs :: print()const
{
	for(int i; i < names.size(); ++i)
		{
		std::cout<<"("<<names[i] << ","<<ages[i]<<")"<< std::endl;
		}

}


Notice
1
2
3
for (int i; i < names.size(); ++i){

}


In C++ you must give variables a value before trying to use them.

1
2
3
4
5
6
7
8
void Name_pairs :: print()const
{
	for(int i = -1; i < names.size(); ++i)
		{
		std::cout<<"("<<names[i] << ","<<ages[i]<<")"<< std::endl;
		}

}


On a side note, why do you have your ages vector as a double? Unless you're being more precise than years std::vector<int> ages; should be fine.
Thank you, it was a great help.
for(int i = -1; i < names.size(); ++i) ¡¿ -1 ?!
Topic archived. No new replies allowed.