-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.cpp
More file actions
107 lines (78 loc) · 2.45 KB
/
1.cpp
File metadata and controls
107 lines (78 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <iostream>
#include <string>
using namespace std;
struct Car {
string brand;
string model;
int year;
double price;
bool negotiable;
string engine;
string color;
int ownerCount;
};
void inputCarInfo(Car& car);
void printCarTable(const Car* cars, int size);
int main() {
int carCount = 0;
int maxCars = 2;
Car* cars = new Car[maxCars];
char addMore = 'y';
while (addMore == 'y' || addMore == 'Y') {
if (carCount == maxCars) {
maxCars *= 2;
Car* temp = new Car[maxCars];
for (int i = 0; i < carCount; ++i) {
temp[i] = cars[i];
}
delete[] cars;
cars = temp;
}
cout << "\nEnter details for car " << (carCount + 1) << ":\n";
inputCarInfo(cars[carCount]);
carCount++;
cout << "Do you want to add another car? (y/n): ";
cin >> addMore;
cin.ignore();
}
printCarTable(cars, carCount);
// Звільняємо пам'ять
delete[] cars;
return 0;
}
void inputCarInfo(Car& car) {
cout << "Enter brand: ";
getline(cin, car.brand);
cout << "Enter model: ";
getline(cin, car.model);
cout << "Enter year: ";
cin >> car.year;
cout << "Enter price: ";
cin >> car.price;
cout << "Is the price negotiable (1 for Yes, 0 for No): ";
int negotiableInput;
cin >> negotiableInput;
car.negotiable = (negotiableInput == 1);
cin.ignore();
cout << "Enter engine: ";
getline(cin, car.engine);
cout << "Enter color: ";
getline(cin, car.color);
cout << "Enter number of owners: ";
cin >> car.ownerCount;
cin.ignore();
}
void printCarTable(const Car* cars, int size) {
cout << "Brand Model Year Price Negotiable Engine Color Owners\n";
cout << "--------------------------------------------------------------------------\n";
for (int i = 0; i < size; ++i) {
cout << cars[i].brand << " "
<< cars[i].model << " "
<< cars[i].year << " "
<< cars[i].price << " "
<< (cars[i].negotiable ? "Yes" : "No") << " "
<< cars[i].engine << " "
<< cars[i].color << " "
<< cars[i].ownerCount << endl;
}
}