?
Solved

array, overloaded function  question

Posted on 2011-03-14
9
Medium Priority
?
235 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:pgmerLA
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 19

Expert Comment

by:Raheman M. Abdul
ID: 35131511
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
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1000 total points
ID: 35131531
>>  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
 
LVL 19

Assisted Solution

by:Raheman M. Abdul
Raheman M. Abdul earned 500 total points
ID: 35131551
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:pgmerLA
ID: 35131602
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35131974
You did, but again, you need to understand variable scope.
0
 

Author Comment

by:pgmerLA
ID: 35133101
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
 
LVL 35

Assisted Solution

by:sarabande
sarabande earned 500 total points
ID: 35135674
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
 
LVL 35

Expert Comment

by:sarabande
ID: 35135681
after the shift loop you must clear a[4]  .

Sara
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35137335
@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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question