Solved

array, overloaded function  question

Posted on 2011-03-14
9
227 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 18

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 250 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 18

Assisted Solution

by:Raheman M. Abdul
Raheman M. Abdul earned 125 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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 33

Assisted Solution

by:sarabande
sarabande earned 125 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 33

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

778 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