Dear all I have a function and withing I want to call a member funtion.

More specifically I have a menu inplemented the code below.
please note the commented line

case '1':
	//flightcode();
	break;
	case '2':
	cout<<"Enter the Customers National Number ID"<<endl;
	cin>>ni;
//	CustArray.showCustomers(ni);//given the customers ID
	break;
	case '3':
	//membershiptype();

I have a member funtion into my classes declaration. I have 150 objects because I declare them in the main

Customers *CustArray=new Customers[150];
void Customers::showCustomers(int ni)
		{
		for (int i=0; i>150;i++) 
		{
			if (ni==customer_id) 
			{
				cout <<"Customer Information"<<endl;
				cout <<"*********************"<<endl;
				cout<<"Customer ID:"<< customer_id <<endl;
				cout<<"ID:"<< id <<endl;
				cout<<"Name:"<< name <<endl;
				cout<<"Address:"<< address <<endl;
				cout<<"Membership:"<< membership <<endl;
				cout<<"----------------------------"<<endl;
			}
		}

How I am going to call this function given that the user will enter his/her id?
Then I will search thgout all the created objects and show user info.?

Recommended Answers

All 8 Replies

Your going about this the wrong way. Customers member function 'showCustomers' shouldn't loop through the Customers objects, it should only display its values.

You should have a loop like

for (int i = 0; i < 150; ++i)
{
CustArray[i].showCustomers();
}

Edit your code as:

case 2:
cout<<"Enter the Customers National Number ID"<<endl;
cin>>ni;
for (int i=0; i<150;i++)
{
 if (ni==CustArray[i].customer_id)
 {
  CustArray[i].showCustomers(ni);
 }
}
break;
// Here loop is removed
void Customers::showCustomers(int ni)
{
  if (ni==customer_id)
  {
   cout <<"Customer Information"<<endl;
   cout <<"*********************"<<endl;
   cout<<"Customer ID:"<< customer_id <<endl;
   cout<<"ID:"<< id <<endl;
   cout<<"Name:"<< name <<endl;
   cout<<"Address:"<< address <<endl;
   cout<<"Membership:"<< membership <<endl;
   cout<<"----------------------------"<<endl;
   }
............

Be careful

Edit your code as:

case 2:
cout<<"Enter the Customers National Number ID"<<endl;
cin>>ni;
for (int i=0; i<150;i++)
{
 if (ni==CustArray[i].customer_id)
 {
  CustArray[i].showCustomers(ni);
 }
}
break;
// Here loop is removed
void Customers::showCustomers(int ni)
{
  if (ni==customer_id)
  {
   cout <<"Customer Information"<<endl;
   cout <<"*********************"<<endl;
   cout<<"Customer ID:"<< customer_id <<endl;
   cout<<"ID:"<< id <<endl;
   cout<<"Name:"<< name <<endl;
   cout<<"Address:"<< address <<endl;
   cout<<"Membership:"<< membership <<endl;
   cout<<"----------------------------"<<endl;
   }
............

Be careful

Your code looks good. But, allow me to comment.
showCustomers() function should not receive an int parameter. There is no need for it. In the loop function, there is already an 'if' statement to check for the customer_id. Hence, when the showCustomers() function is called, all information about the customer is displayed. It's simple as that.

case 2:
cout<<"Enter the Customers National Number ID"<<endl;
cin>>ni;
for (int i=0; i<150;i++)
{
 if (ni==CustArray[i].customer_id)
 {
  CustArray[i].showCustomers();
 }
}
break;
// Here loop is removed
// int parameter is removed
void Customers::showCustomers()
{
   cout <<"Customer Information"<<endl;
   cout <<"*********************"<<endl;
   cout<<"Customer ID:"<< customer_id <<endl;
   cout<<"ID:"<< id <<endl;
   cout<<"Name:"<< name <<endl;
   cout<<"Address:"<< address <<endl;
   cout<<"Membership:"<< membership <<endl;
   cout<<"----------------------------"<<endl;
............
}

Your code looks good. But, allow me to comment.
showCustomers() function should not receive an int parameter. There is no need for it. In the loop function, there is already an 'if' statement to check for the customer_id. Hence, when the showCustomers() function is called, all information about the customer is displayed. It's simple as that.

case 2:
cout<<"Enter the Customers National Number ID"<<endl;
cin>>ni;
for (int i=0; i<150;i++)
{
 if (ni==CustArray[i].customer_id)
 {
  CustArray[i].showCustomers();
 }
}
break;
// Here loop is removed
// int parameter is removed
void Customers::showCustomers()
{
   cout <<"Customer Information"<<endl;
   cout <<"*********************"<<endl;
   cout<<"Customer ID:"<< customer_id <<endl;
   cout<<"ID:"<< id <<endl;
   cout<<"Name:"<< name <<endl;
   cout<<"Address:"<< address <<endl;
   cout<<"Membership:"<< membership <<endl;
   cout<<"----------------------------"<<endl;
............
}

The code given by the author, has a function showCustomers(int). It receives an integer parameter, but actually it is not required.

I want to thank you guys.
You showed me the right direction :)

Hi guys again,
I have tried the code and it doesnt seem to work.

In the other hand I have manipulate the code so that I have a function to display all customers and this is working!!!
:(

Hi guys again,
I have tried the code and it doesnt seem to work.

In the other hand I have manipulate the code so that I have a function to display all customers and this is working!!!
:(

What code? You'll have to post it if you want us to look at it.

See it here:This code does show any results

case 2:
    cout<<"Please enter Systems Customer ID"<<endl;
    cin>>searchedi;
	for (int i=0; i<ncustomers_in_system;i++)
    {
	if (searchedi==CustArray[i].id) //TO DO globally declare
	{
	CustArray[i].showCustomers(); //TO DO same declaration here
	}
	}
    break;
void Customers::showCustomers()
		{
				cout <<"Customer Information"<<endl;
				cout <<"*********************"<<endl;
				cout<<"Customer ID:"<<customer_id <<endl;
				cout<<"ID:"<<id<<endl;
				cout<<"Name:"<<name<<endl;
				cout<<"Address:"<<address<<endl;
				cout<<"Membership:"<<membership<<endl;
				cout<<"----------------------------"<<endl;
		}

But the code below shows:

case '2':
	cout<<"Customers Information"<<endl;cout<<"**************************************\n\n"<<endl;
	for (int c=0; c<ncustomers_in_system;c++)
    CustArray[c].showAllCustomers();
	break;
void Customers::showAllCustomers()
		{
				cout<<"Cust-ID:"<< customer_id <<" ";
				cout<<"ID:"<< id <<" ";
				cout<<"Name:"<< name <<" ";
				cout<<"Address:"<< address <<" ";
				cout<<"Membership:"<<membership <<" ";
				cout<<"----------------------------"<<endl;
		}

Hope that helps

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.