0

So I was doing a program where i need to find the total area of all floors, where the floors are determined by the user. I think I'm correct on the part to use the pointers, so to check if the code was correct I tried it doing it with basic addition. but even with basic addition it seems to have already some problems. I tried looking for similar questions in here and I can't find anything that might help me, so I hope you guys can help me. Thank you in advance.

float *Length=NULL, *Width=NULL, *Area=NULL, TotalArea, templ, tempw;
int floors, count;
    cout << "Input the number of floors to proceed\n";
    cout << ":";
    cin >> floors;
    
    Length = new float[floors];
    Width = new float[floors];
    Area = new float[floors];
   for (int loop = 0; loop < floors; loop++)
    {
        cout << "\n\nFloor " << loop + 1 << endl;
        cout << "Input the Length: \n";
        cin >> templ;
        cout << "Input the Width: \n";
        cin >> tempw;
        *(Length + loop) = templ;

        *(Width + loop) = tempw;
        *(Area + loop) = (*Length + loop) + (*Width + loop);

        count = loop;
        for (int count = 0; count < floors; count++)
        {
            TotalArea = TotalArea + *Area+count;
        }
    }
    cout << TotalArea  << endl;

I tried inputting the following: floor:2

floor 1 length: 1 width: 1 floor 2 length: 1 width: 1

The answer should be 4, but the output ends up with 10.

DeWard
  • 15
  • 4
  • I think the loop that calculates `TotalArea` should be outside the other loop. You also need to initialize that variable before you use it. Your code would be much easier to read if you used `Length[x]` instead of `*(Length + x)`. – Retired Ninja Sep 08 '20 at 20:30
  • Why are you using "Area = Length + Width"? Normally, that would be "Area = Length * Width". There are many other errors, though. But, before I even begin to address them, please post the rest of your code - so that we have a MRE (that can be compiled and run). – Adrian Mole Sep 08 '20 at 20:44
  • Looks like in here: `*Area+count` you wanted `*(Area+count)` – Vlad Feinstein Sep 08 '20 at 20:44
  • @VladFeinstein And similar errors in a number of other places. Lots of errors! – Adrian Mole Sep 08 '20 at 20:45
  • @AdrianMole I used "Area = Length + Width", because as I said on the description to check if my pointers and arrays are correct I tried it first with basic addition. – DeWard Sep 09 '20 at 03:32
  • @RetiredNinja, Vlad Feinstein, Adrian Mole Thank you for your time with helping me, much appreciated. I did post my what I came up with as an answer, if you'd like to look into it more I would be glad to receive your criticism. – DeWard Sep 09 '20 at 04:57

2 Answers2

1

You don't need arrays and pointers. Since you are looping through the floors and summing the areas of each floor, you can "forget" about previously encountered lengths, widths and areas, and only remember the total area encountered so far.

float length;
float width;
float area;
float totalArea;

int floors;
std::cout << "Input the number of floors to proceed\n";
std::cout << ":";
std::cin >> floors;

totalArea = 0;
for (int loop = 0; loop < floors; loop++)
{
    std::cout << "\n\nFloor " << loop + 1 << std::endl;
    std::cout << "Input the Length: " << std::endl;
    std::cin >> length;
    std::cout << "Input the Width: " << std::endl;
    std::cin >> width;
    area = length * width;
    totalArea += area;
}
std::cout << totalArea  << std::endl;

Notice how I added these annoying std:: everywhere? If you wonder why, see this other question: Why is “using namespace std;” considered bad practice?

Stef
  • 13,242
  • 2
  • 17
  • 28
  • Re: `You don't need arrays and pointers.` - unless your goal is to use arrays and pointers, as suggested by tags. – Vlad Feinstein Sep 08 '20 at 22:37
  • 1
    @VladFeinstein The question opens with "I think I'm correct on the part to use the pointers," which might or might not mean "I made the decision to use pointers because I thought they were appropriate for this problem, please correct me if they're not" – Stef Sep 08 '20 at 22:41
  • Yes, my goal was to use arrays but not necessarily using pointers. Thank you for your help, although you did not use arrays, the part where you did not use another for loop helps me. – DeWard Sep 09 '20 at 04:28
0

I tried making some changes based on you guys comments and here is what I did. There are some warnings I don't really know why but It seems to be working

float* Length = NULL, * Width = NULL, TotalArea, tempw, templ, * Area = NULL;
int floors, loop = 0;

    cout << "Input the number of floors to proceed\n";
    cout << ":";
    cin >> floors;


    for (int loop = 0; loop < floors; loop++)
    {
        Length = new float[floors];
        Width = new float[floors];
        Area = new float[floors];
        cout << "\n\nFloor " << loop + 1 << endl;
        cout << "Input the Length: \n";
        cin >> templ;
        cout << "Input the Width: \n";
        cin >> tempw;
        *(Length +loop) = l;
        *(Width+loop) = w;
        Area[floors] = (*(Length+loop)) * (*(Width+loop));
        TotalArea += a[floors];
    }

    cout << TotalArea  << endl;

This is what I came u with, it seems to be working, the output is also correct now.

DeWard
  • 15
  • 4
  • Can you tell us what the warnings are? Here are a few suggestions: 1) Write `Length[loop] = l;` instead of `*(Length +loop) = l;`. 2) I think the last line should be `TotalArea += Area[floors];` and not `TotalArea += a[floors];`. 3) If you have some `float` variables and some `float *` variables, it's more readable and less error prone **not** to declare them on the same line. It is too easy to forget a `*` in your first line `float* Length = NULL, * Width = NULL, TotalArea, tempw, templ, * Area = NULL;`, and when someone reads it, it's not obvious at a glance how many`float` and `float *`. – Stef Sep 09 '20 at 08:42
  • 4) Use flags `-Wall -Wextra -Werror` on the compiler when compiling. This will help you develop good habits, and in many occasions will help you find mistakes. A compiler warning often means "what you wrote is technically correct c++, but it probably doesn't do what you think it does", so it's very important not to ignore warnings. – Stef Sep 09 '20 at 08:44
  • @stef I can't really tell the errors anymore, because I asked my friend to help me and there are no more warnings now, but thank you for your suggestions it is also what my friend told me to do. also thank you for recommending to use '-wall -wextra -werror' i will search about it and hopefully learn how to use it properly. – DeWard Sep 09 '20 at 14:18