Solved

array, overloaded function  question

Posted on 2011-03-14
9
225 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
Comment Utility
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 74

Accepted Solution

by:
käµfm³d   👽 earned 250 total points
Comment Utility
>>  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
Comment Utility
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
 

Author Comment

by:pgmerLA
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 74

Expert Comment

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

Author Comment

by:pgmerLA
Comment Utility
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 32

Assisted Solution

by:sarabande
sarabande earned 125 total points
Comment Utility
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 32

Expert Comment

by:sarabande
Comment Utility
after the shift loop you must clear a[4]  .

Sara
0
 
LVL 74

Expert Comment

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 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.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now