I've thought about whether to submit this post or not, as it may complicate things more than help. But I decided what the heck, it may be helpful, particularly if you are using an array of struct/class. So here goes.
With arrays there isn't a straightforward way to know when you have reached the end of data in the array. For example, if an array can hold up to ten Employees, but there is only eight Employees in the array at the moment, how do you know that so you can stop printing after eight rather than ten loops? Well, you keep track as you add them/delete them so you know without question how many there are and stop printing after you've reached the appropriate number. Alternatively, you use some other technique that indicates when you have reached the end of actual data. Then you can add up the elements as you print them out, rather than when you add/delete them (this means you can't access the number of elements in the array willy nilly like you could if you keep track as you add/delete them, but it works well under a number of circumstances). To do this you need to assign all of the elements of the array with some value that will never be used by an object of the type to be stored in the array (either explicitly with objects of a primitive type or via the default constructor with user defined struct/classes), and when you find the default value(s) looping through the array you know you have reached the end of the data in the array. In this case, if you are using an array of struct then restrict it so an employee number can never be negative and have the default (null) value of the Employee number be -1 or let Employee name be "" , that is an empty string, as default, which can never be for an actual Employee, either.
For example.
Code:
Here's a C++ struct called Employee
struct Employee
{
Employee();
string name;
int number;
};
Now here's a definition for the default constructor
Employee::Employee()
{
name = ""; //name is empty
number = -1; //number is -1
}
Now declare an array of 10 Employees.
Employee employees[10];
The declaration of the array of Employees will cause the compiler to call the default constructor for the Employee struct/class and intialize all elements of the array to the default values of the data members provided by the default constructor.
Then if you want to print out the Employees in the array (either before or after you enter some Employee data) you can do this:
int i = 0;
while(employee[i]l.name != "" && employee[i].number != -1)
{
cout << employee[i].name << ' ' << employe[i].number << endl;
++i;
}
cout << "the number of employees is in the array" << i << endl;
If you use null terminated char arrays instead of the STL string class then you will need to adjust for that syntax as well, but the underlying principle holds. You shouldn't need to check both data members for the null value either. Just one should do.
If you are using a list or a vector or some container other than an array, then there are other ways to detect the end of user data and/or built in methods to determine size without your having to devise a mechanism to do it. But I doubt that you are using lists or vectors, yet.