How do I get an age to not display from file if it doesn't meet a range?

Jan 27, 2017 at 8:31pm
So I have a code here that is suppose to sort out all the names in order by last name, as well as keep the age with them. I have all that. However, I seem to be having trouble with keeping certain names and ages out of the display based on an age range.

The description says to display all names and ages in order, but discard names if they are below the age of 18, and above the age of 65. I have the first in last name in two dimensional arrays of string, and the age in a basic int array. I found out how to keep the age with the name, but I can't seem to keep the name with the age when I tell it not to display.

I've tried a few if/else statements on my file function and my display function, but I just get wonky results

Here is the code, Pay no mind to the excessive amount of functions I use. I was having trouble with them when I was learning, so now I just like to create many, for learning purposes. I understand the inefficiency

[code]

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

int getFile(string, string[][2], int[]);
void getDisplay(string[][2], int, int[]);
void selectionSort(string[][2], int, int[]);
void switching(string&, string&);
int bSearch(const string[][2], int, string);
void swap(int&, int&);

#define ARRAYSIZE 50

int main()
{

;
string filename, names[ARRAYSIZE][2];
int age[ARRAYSIZE];
int localcounter = 0;
int file = 0;
string search;


file = getFile(filename, names, age);
selectionSort(names, file, age);
getDisplay(names, file, age);



return 0;

}
int getFile(string filename, string names[][2], int age[])
{
ifstream infile;
string firstname, lastname;
int localcounter = 0;
int ages;
int underages[ARRAYSIZE];

cout << "Please enter the name of the file " << endl;
cin >> filename;

infile.open(filename.c_str());
if (infile)
{
while (infile >> firstname >> lastname >> ages && localcounter < ARRAYSIZE)

{
names[localcounter][0] = firstname;
names[localcounter][1] = lastname;
age[localcounter] = ages;

localcounter++;
}
}
else
{
cout << "Couldn't find file" << endl;
exit(1);
}
return localcounter;


}
void getDisplay(string names[][2], int num, int age[])
{
string name;
string search;
int blank[ARRAYSIZE];
for (int count = 0; count < num; count++)
{

cout << names[count][0] << " " << names[count][1] << " " << age[count] << endl;

}

char again;
do
{
cout << "Please enter a Last name to search for: ";
cin >> search;

int results = bSearch(names, num, search);

if (results == -1)
cout << "That name couldn't be found, Please try again" << endl;
else
{

cout << "That name was found here: " << results << endl;
}

cout << "Would you like to search again? :(Y/N)";
cin >> again;
} while (again == 'Y' || again == 'y');




}
void selectionSort(string names[][2], int count, int age[])
{
for (int TopOfList = 0; TopOfList < count - 1; TopOfList++)
{
string smallest = names[TopOfList][1];

int locationOfSmallest = TopOfList;

for (int current = TopOfList + 1; current < count; current++)
{
if (names[current][1] < smallest)
{
smallest = names[current][1];
locationOfSmallest = current;

}

}


switching(names[TopOfList][1], names[locationOfSmallest][1]);
switching(names[TopOfList][0], names[locationOfSmallest][0]);
swap(age[TopOfList], age[locationOfSmallest]);

}

}
int bSearch(const string SIZE[][2], int numElems, string value)
{
int first = 0,
last = numElems - 1,
middle,
position = -1;
bool found = false;

while (!found && first <= last)
{
middle = (first + last) / 2;
if (SIZE[middle][1] == value)
{
found = true;
position = middle;
}
else if (SIZE[middle][1] > value)
last = middle - 1;
else
first = middle + 1;
}
return position;
}


void switching(string& alpha, string& beta)
{
string temp;

temp = alpha;
alpha = beta;
beta = temp;

}
void swap(int& alpha, int& beta)
{
int temp;
temp = alpha;
alpha = beta;
beta = temp;


}
[code]
Last edited on Jan 27, 2017 at 8:32pm
Jan 27, 2017 at 9:47pm
Consider creating a class or a struct called Person to store the first name, last name and age of a person. Then you could just have a single array of Persons. Better still would to use a vector<Person>

To filter out the people who are too young or too old, just add code in getDisplay() so it only prints the ones whose age is acceptable.
Topic archived. No new replies allowed.