How can you implement storage and subsequent writing / reading from a data file of the type:

Small house-> 1 door, 5 windows Big house-> 15 doors, 600 windows

The number of houses is unlimited to enter, the data - only the doors and windows.

Managed C ++. Use structure? But if you create a structure of this type, how then to declare it, if you do not know what the house will be?

    5 answers 5

    Dynamic memory allocation for structures and other data types can be performed in C ++ using the malloc () or calloc () functions or the new operator (objects are just them). They can also create arrays of arbitrary size. See more books on C ++ and Google.

    • Good. A good example, my last question. You answered it. About buildings and offices: hashcode.ru/questions/51458 /... I commented below from here, I commented: "I remembered where I had a hitch, why I didn’t take structures right away. Data: office, factory dynamic. I don’t know how many places will be. How to be? " - Maxim Tsybanov 4:19
    • one
      So I write about it here. Those. how to allocate memory dynamically for structures and their arrays (i.e., to create them, in a simple way). I gave the direction to dig. Then do yourself a little work. There is nothing complicated. - skegg
    • malloc and callloc is a C-style - andrybak
    • Thanks, @Andrey , I will know - skegg

    and who prevents to make doors and windows arrays? In this case, no problems with storage.

    struct House { vector<Door> doors; vector<Window> windows; }; 

    (I don’t know Managed c ++ so well, so I can’t say how to precisely make arrays or something like that).

    • Did not quite understand your idea. My problem is that with this data it is necessary to operate easily for writing and reading from a file. I do not know what the number of houses will be? In your case (with structure), I still need to declare a struct House Big; Struct House Little; etc. I cannot dynamically assign names to structures. - Maxim Tsybanov 1:01
    • @Maxim Droy decide on what you are given at the entrance, and what you want to keep / with what you want to operate PS in general, the question is not clearly formulated - andrybak

    If I understand correctly what is required, then I need to use a structure like this:

     struct House { BOOL IsBig; int DoorsCount; int WindowCount; } 

    and, accordingly, to fill the IsBig property = TRUE for large houses, FALSE for small ones. Write to a file (and read from a file) you need an array of such structures.

      You can use inheritance:

       struct house { protected: int Doors, int Windows; /* или массивы/вектора структур Door, Window, если надо хранить данные о дверях и окнах */ } struct small_house : house {} struct big_house : house {} 

      Note struct {...} in C ++ is the same as class {public: ...}

      • You do not understand me. What house the user enters: it can be not only big or small. It may still be red, ugly, etc. I do not know what it may be in the future. This will be taken from the textbox thanks to the user. And then, having entered the name of the house, he can already enter the number of windows and doors to him. - Maxim Tsybanov
      • @Maxim Droy what prevents you from first realizing what the house will be like and then declaring a variable? And @ganouver, in my opinion, is right - the size of the house can quietly be its property (like color, beauty, etc.) - andrybak

      Maxim, I understood from your comments (I probably understood) that you actually have several (this is about offices, factories ...) objects and each has a list of user-defined properties (possibly from a set of predefined).

      I am afraid that the data structures (“hard”) offered to you are not very suitable here. Apparently you need to operate with lists of objects, each of which has (one or more) property lists of the form name-property: value.

      I can assume that you will have to put a certain description in front of such an object in the file, which you should use when reading.

      If this proposal seems interesting to you, then you can try to design such a structure.

      • Yes, this is his training task. He wrote it to me in a discussion of the previous question. - skegg