array, overloaded function question

I have a homework question:
We are covering function templates and overloaded function,as well as arrays.
We haven't covered pointers yet.

We are given a list of names and ages
and we have to write 2 functions and their structure is given by our professor.:
a. display(Name, Age)//display name and age
b. display(Name)//THAT'S WHERE MY PROBLEM IS--display name of the teenagers
How can I compare the age to 13 and 19 range, if the age is not an input of my function?

Is there a way to get the age without having to create another array for age in the function's body?

Also, do you have any suggestion on how I can trim down my code or make it more professional? I am trying to learn good programming habits.

Thanks a lot.
#include<iostream>
#include<fstream>
#include<string>


using namespace std;

void Display(string a[], int b[]);
void Display(string a[]);


int main(){
	//create variables
		int Age[5];
		string Name[5];
		float AgeAve=0.0;
	//read data from file data.txt into 2 arrays
		ifstream f;
		f.open("data.txt");
		f>>Name[0]>>Age[0];
		for(int i=1;i<5;++i)
		{f>>Name[i]>>Age[i];}
		f.close();

	
	//display name and age of all students
		Display(Name, Age);
		
	//display the name of all teenager students
		Display(Name);

	//terminate the program
		return 0;
}

void Display(string a[], int b[])
{for(int i=0;i<5;++i)
	{cout<<a[i]<<' '<<b[i];
	cout<<endl;
	}
cout<<endl;
}


void Display(string a[])
{string dummy;//that's seem to be overkill--??Any SUGGESTIONS? 
int Age[5];
ifstream f;
f.open("data.txt");
for (int i= 0; i<5; ++i)
{f>>dummy>>Age[i];}
f.close();
cout<<"This is the name of all teenagers: ";
for(int i=1;i<5;++i)
		{if (Age[i]>=13 && Age[i]<=19) 
		cout<<a[i]<<' ';}
		cout<<endl;
}

Open in new window

data.txt
pgmerLAAsked:
Who is Participating?
 
käµfm³d 👽Connect With a Mentor Commented:
>>  How can I compare the age to 13 and 19 range, if the age is not an input of my function?

Given what you posted, you can't. Think about variable scope and what it means and affects with regard to the code you posted.
0
 
Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
Dont create Age[] again in 2nd function.
But use   Age[ i ] directly since it is declared in main function and it is available in display(string a[]) function

or try to use ::Age[ i ]

There is no need to get the data from the text file again in the display(string a[]) function just use the variable directly



0
 
Raheman M. AbdulConnect With a Mentor Senior Infrastructure Support Analyst & Systems DeveloperCommented:
Option:

call Display(Name,Age) from the second function ie Display(Name)

like

Display(string a[])
{
   Display(a, Age);   // here Age is globally declared
}
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
pgmerLAAuthor Commented:
When I use the Age[] in my function, I get a compile error, telling me :error C2065: 'Age' : undeclared identifier
and yet I thought I declared it in my main function.
#include<iostream>
#include<fstream>
#include<string>


using namespace std;

void Display(string a[], int b[]);
void Display(string a[]);


int main(){
	//create variables
		int Age[5];
		string Name[5];
		float AgeAve=0.0;
	//read data from file data.txt into 2 arrays
		ifstream f;
		f.open("data.txt");
		f>>Name[0]>>Age[0];
		for(int i=1;i<5;++i)
		{f>>Name[i]>>Age[i];}
		f.close();

	
	//display name and age of all students
		Display(Name, Age);
		
	//display the name of all teenager students
		Display(Name);

	//terminate the program
		return 0;
}
void Display(string a[], int b[])
{for(int i=0;i<5;++i)
	{cout<<a[i]<<' '<<b[i];
	cout<<endl;
	}
cout<<endl;
}

void Display(string a[])
{
cout<<"This is the name of all teenagers: ";
for(int i=1;i<5;++i)
		{if (Age[i]>=13 && Age[i]<=19) 
		cout<<a[i]<<' ';}
		cout<<endl;
}

Open in new window

0
 
käµfm³d 👽Commented:
You did, but again, you need to understand variable scope.
0
 
pgmerLAAuthor Commented:
I think I got it.

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


using namespace std;

void Display(string a[], int b[]);
void Display(string a[]);
//create global variable
int Age[5];

int main(){
	//create variables
		string Name[5];
		float AgeAve=0.0;
	//read data from file data.txt into 2 arrays
		ifstream f;
		f.open("data.txt");
		f>>Name[0]>>Age[0];
		for(int i=1;i<5;++i)
		{f>>Name[i]>>Age[i];}
		f.close();

	
	//display name and age of all students
		Display(Name, Age);
		
	//display the name of all teenager students
		Display(Name);

	//terminate the program
		return 0;
}
void Display(string a[], int b[])
{for(int i=0;i<5;++i)
	{cout<<a[i]<<' '<<b[i];
	cout<<endl;
	}
cout<<endl;
}

void Display(string a[])
{
cout<<"This is the name of all teenagers: ";
for(int i=1;i<5;++i)
		{if (Age[i]>=13 && Age[i]<=19) 
		cout<<a[i]<<' ';}
		cout<<endl;
}

Open in new window

0
 
sarabandeConnect With a Mentor Commented:
you better would let the Age array a local variable of main and reduce the name array to only containing teenagers before calling Display(a).

i also would suggest to using std::vector for arrays instead of plain C arrays what would make the erasing of non-teenagers much easier.

but you also can do it with the current array. for example to erase the name at position i you do:

   for (int n = i+1; n < 5; n++)
         a[n-1] = a[n];

Sara
0
 
sarabandeCommented:
after the shift loop you must clear a[4]  .

Sara
0
 
käµfm³d 👽Commented:
@sarabande

Vector would, in the professional world, be the way to go; however, since this appears to be classwork, I think the author is restricted to arrays at this point in his studies  = )


@pgmerLA

That should be correct. I would like to mention that global variables, as a matter of practice, are seen as a bad thing--they can introduce bugs if not handled properly. You generally want to introduce variables at the most restrictive scope possible. This allows you to only have what you need in memory at any given time. There will times when you may have to circumvent this principle, but there shouldn't be many. I think for what you are learning at present, you should be OK with the above.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.