Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
<pre>//Main function

void printBythePrice(const HousingRegister &house){

	float max;
	string *arr = new string[house.getnrOfHouses()];
	cout << "Enter the price";
	cin >> max;

	house.printBythePrice(arr, max);

	for (int i = 0; i < house.getnrOfHouses(); i++)

	{
		cout << arr[i];
	}
	delete[] arr;
}



//handler Functinen
void HousingRegister::printBythePrice(string *arr, float max)const {

	int index = 0;

	for (int i = 0; i <nrOfHouses; i++)

	{
		if (housing[i]->getRent() >= max)

		{
                arr[index] = housing[i]->toString();

			index++;
		}
	}

}


What I have tried:

I have trie to fix this print code but, i have a problem with the array.
function expects a string * arr filling with data, but you submit a common variable instead of an Array.

How can i fix it?
Posted
Updated 27-May-16 2:19am
v2
Comments
Member 12549902 27-May-16 6:21am    
Anyone who knows what i am doing wrong?
CPallini 27-May-16 6:29am    
It just looks you are passing an array of empty strings to printBythePrice method (that is I see NO syntax errors)
Member 12549902 27-May-16 6:32am    
Thank you for the answer, How can can i fix it? Can you please, give me an exempel?
CPallini 27-May-16 6:45am    
What do you want to fix? I repeat: there isn't a syntax error in the method call (there is just a logical one: you are passing empty strings).
If you don't want to pass empty string then, well, assign them.
Member 12549902 27-May-16 6:51am    
Thank you again for the asnwer:
Am i doing wright this time?
I am sorry i am a biginer on the programing..

void printBythePrice(const HousingRegister &house){

float max;
string arr[100];

cout << "Enter the price";
cin >> max;

house.printBythePrice(arr, max);

for (int i = 0; i < house.getnrOfHouses(); i++)
{
cout << arr[i];
}
}

CPallini seems to be right: your string pointer isnt an array per se. May it is only a string and you access its chars as substring. Some reading about arrays.

Allocate an string array for the use in your code like that:

C++
string array[COUNT];


Tip: use a debugger and make some output to see what
 
Share this answer
 
Your code should work.
And, at least, it works in the fake scenario I created to make it run
C++
 #include <iostream>
 #include <vector>
 using namespace std;

class Housing
{
  float rent;
  string name;
public:
  Housing(string name, float rent):rent(rent), name(name){}
  int getRent(){return rent;}
  string toString(){return name;}
};

class HousingRegister
{
  int nrOfHouses;
  vector<Housing * > housing;

public:
  HousingRegister(int houses):nrOfHouses(houses)
  {
    for ( int n=0; n<houses; ++n)
    {
      string name = "foo";
      name.append( 1, (char)('0' + n));
      float rent = 1000.0f * (n+1);
      housing.push_back( new Housing( name, rent ));
    }
  }
  ~ HousingRegister()
  {
    for (size_t n=0; n<housing.size(); ++n)
      delete housing[n];
  }

  void printBythePrice(string *arr, float max) const;
  int getnrOfHouses() const { return nrOfHouses; };

};
void printBythePrice(const HousingRegister &house){

  float max;
  string *arr = new string[house.getnrOfHouses()];
  cout << "Enter the price";
  cin >> max;

  house.printBythePrice(arr, max);

  for (int i = 0; i < house.getnrOfHouses(); i++)

  {
    cout << arr[i];
  }
  delete[] arr;
}



//handler Functinen
void HousingRegister::printBythePrice(string *arr, float max)const {

  int index = 0;

  for (int i = 0; i <nrOfHouses; i++)

  {
    if (housing[i]->getRent() >= max)

    {
                arr[index] = housing[i]->toString();

      index++;
    }
  }
}

int main()
{
  HousingRegister hr(5);

  printBythePrice(hr);
}
 
Share this answer
 
v2
Comments
Member 12549902 27-May-16 8:26am    
Well great.. it works.
Thank you so much CPallini for the help.. you have been very patient explaining and very clear.
I wish you good weekend.
CPallini 27-May-16 8:36am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900