Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
void HousingRegister::printBythePrice(string *array, float max)const {
            int index = 0;
            for (int i = 0; i <nrOfHouses; i++)
            {
                if (housing[i]->getRent() <= max)
                {
                    array[index] = housing[i]->toString();
                    index++;
                }
            }
        }

        //mainfunc

        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;
        }


What I have tried:

It is the wrong size of the array. You need to implement functionality so you do not have empty places at the end of the array.
Posted
Updated 30-May-16 14:18pm
v2
Comments
Member 12549902 30-May-16 20:09pm    
it is the wrong size of the array. You need to implement functionality so you do not have empty places at the end of the array. This is my question? Can you fix this mistake? cheek this code careful. i need an answer buy tonight
Philippe Mori 30-May-16 21:04pm    
As explained in solution 1, code design is wrong. I would suggest you to use std::vector and STL algorithm to solve that problem.

If this problem is academic and you haven't yet seen STL, then you should add a parameter for the size.

By the way, I would recommand you to use array syntax instead of pointer syntax to make it clearer that you are passing an array.
Member 12549902 31-May-16 3:36am    
Plz give me an exemple code how you wuold inplement it?
Philippe Mori 31-May-16 9:04am    
All necessary information is provided. If you don't understand, then read your course note again. If you don't make any effort, you won't learn anything.

In that case, you have to know how many items satisfy the condition so you have to return the value somehow.
Member 12549902 31-May-16 9:22am    
void printBythePrice(const HousingRegister &house){

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

house.printBythePrice1(arr, &index, max);

house.getnrOfHouses()==index;

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

void printStrings(string stringArray[], int nrOfStrings)
{
for (int i = 0; i < nrOfStrings; i++)
{
cout << endl << stringArray[i] << endl;
}
}

void HousingRegister::printBythePrice(string *array, int *index, float max)const {


for (int i = 0; i <nrofhouses; i++)="" {
="" if="" (housing[i]="">getRent() <= max)
{
array[*index] = housing[i]->toString();
index++;
}
}
}
Like this you mean? I am really tring evrything to learn doing my best, studing 8 h a day 5 days a week, some learn fast same learn slow. I am not lazzy person. I want to learn, this function works good, but i know there is a better way of fixing this. I trying i am doing my best, thats why i am asking for help. I am sorry if i look like a lazzy person, but i am just a bigginer in c++.

1 solution

The problem is: the whole idea of such code design is wrong. This way, not only you can get "empty places", but, generally, just some garbage, uninitialized memory. Worse, you can face addressing of memory beyond the memory allocated for the array, with the exception based on General Protection Fault, or, worse, writing garbage beyond that memory area, with unpredictable results.

At the same time, such problems are resolved in really simple ways. First, note that your function does not receive any information on the allocated or assumed length of the array; you receive just a pointer to the beginning of the arrays memory, if it is correct.

Now, you have to think where the knowledge on required array size come from. Let's say, it is known to the caller. Fine, the caller should allocated the array and pass the actual array length to the function.

More difficult (and less desirable) cases when the length becomes known inside the function, as a result of its internal calculations. In this case, you have to allocate the array inside the function and return it.

That's all; as simple as that.

—SA
 
Share this answer
 
Comments
Member 12549902 30-May-16 20:29pm    
Thank you, can you give me an exempel you can you inplement it?
Plz i have tried how to fix it for more than 3 weeks and i didnt succed.
Sergey Alexandrovich Kryukov 30-May-16 20:32pm    
I don't understand what else you may need. The issue is simple as hell.
And you did not tell me which of the two cases you have.
—SA
Member 12549902 31-May-16 2:47am    
Both are conected. Need to print all houses that has the rent price less than the abount i wrote.
Would you inplement such a code.. without empty plases, and not getin garbage, uninitialized memory?
I am sorry for this stupit question but i am new att programing and i dont understand all.
Sergey Alexandrovich Kryukov 31-May-16 9:13am    
Sorry, no. You know the array length either outside of the function (in the caller) or determine it inside. Two cases. What is it?
—SA
Member 12549902 31-May-16 9:28am    
you mean array[index]?

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