+ 17

[SOLUTION] C++ I need help

There are several (N) cars in the repair shop. We have the following information about them: number, brand, owner's name, date of last repair (day, month, year), date of repair (day, month, year). Develop a sort program using the direct addition method (optional): 1. Arrange the names of car owners in alphabetical order and print information about their cars accordingly. https://code.sololearn.com/cb1Xfhx1zi7Y/?ref=app

19th Nov 2020, 5:46 PM
Sanjar Egamberdiyev
Sanjar Egamberdiyev - avatar
11 Respostas
+ 9
Thank you very much Flash and Davide
20th Nov 2020, 3:42 AM
Sanjar Egamberdiyev
Sanjar Egamberdiyev - avatar
+ 5
Flash yes I used free instead of delete because I don't know C++. I corrected it. Thanks.
20th Nov 2020, 2:40 AM
Davide
Davide - avatar
+ 4
All right I checked on the web and yes, even in C++ you need to allocate memory for your pointers. So after you declare: mashine *car[5]; you need this: for(int i = 0; i < 5; ++i ) { car[i] = new mashina; } Also you need to correct the function call in line 69 like that: saralash(car, 5); Because by dereferencing car (i.e. writing *car) you would pass a pointer to mashina rather than an array of pointers to mashina.
19th Nov 2020, 7:09 PM
Davide
Davide - avatar
+ 3
⚔ Sanjar ⚔ maybe no need of raw pointers. Try to use references if applicable. here is a solution using OOP of task 1. I didn’t get “direct addition method” part. https://code.sololearn.com/c8rEUBoH4G7F/?ref=app
19th Nov 2020, 8:23 PM
Flash
+ 2
I don't know C++ but the problem may be that you use the pointers to mashina without allocating memory for them.
19th Nov 2020, 6:46 PM
Davide
Davide - avatar
+ 2
I also noticed that saralash() was giving a segmentation fault. I fixed it. Pointers are your main problem. Look for more information about them, the web is full of resources. https://code.sololearn.com/cUeQhcFcokw0/?ref=app
19th Nov 2020, 8:45 PM
Davide
Davide - avatar
+ 2
Flash yes, I added a line at the end. Thanks for the heads up
19th Nov 2020, 9:13 PM
Davide
Davide - avatar
+ 2
⚔ Sanjar ⚔ you are welcome đŸ€—
20th Nov 2020, 3:52 AM
Davide
Davide - avatar
+ 1
shouldn’t you also address memory leaks? Davide
19th Nov 2020, 9:02 PM
Flash
0
Davide didn’t check it before. your code leaks memory. don’t call c deallocator to deallocate c++ objects. you had 5 pointers. free deleted the first and one destructor was called. rest is lost hence leaks memory. pay attention to line 20, 86, 87. malloc()-free();new-delete;new[]-delete[]; I think you wrote you didn’t know C++. anyway, hope this helps. here is the code: https://code.sololearn.com/czuNaX7onK0r/?ref=app
20th Nov 2020, 2:33 AM
Flash