Can anybody help me to solve this error...

I want to delete function.... can anybody help me???
case 4 is the one where got error..
im still an amateur, just started to learn coding...

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
  Put #include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;

class Car {


protected:
	int No;
	string number;  // serial number
	string model;
	int year;
	string type;
	int price;
	string status;
	
public:

	Car(){
		year = 1;
	}
	Car (int No, string number, string model, int year, string type, int price, string status){
		this -> No = No;
		this -> number = number;
		this -> model = model;
		this -> year = year;
		this -> type = type;
		this -> price = price;
		this -> status = status;
		
	}
	
	int getNo(){
		return No;
	}

	string getnumber(){
		return number;
	}	
	
	string getmodel(){
		return model;
	}
	
	int getyear(){
		return year;
	}
	
	string getType(){
		return type;	
	}
	
	int getprice(){
		return price;
	}
	
	string getstatus(){
		return status;
	}
	
	void setNo(int No){
		this->No = No;
	}
	
	void setnumber(string number){
		this->number = number;
	}
	
	void setmodel(string model){
		this->model = model;
	}
	
	void setyear(char year){
		this->year = year;
	}
	
	void setType(string type){
		this->type = type;
	}
	
	void setprice(int price){
		this->price = price;
	}
	
	void setstatus(string status){
		this->status = status;
	}
	
	void display (){
		int i;
		cout << "No.  :  " << i << endl;
		cout << "\nVehicle details :  " << getnumber() << endl;
		cout << "Serial Number : " << getnumber() << endl;
		cout << "Model : " << getmodel() << endl;
		cout << "Year : " << getyear() << endl;
		cout << "Type : " << getType() << endl;
		cout << "Price : " << getprice() << endl;
		cout << "Status : " << getstatus() << endl;
	}

};
	
	
int main(){ 
	
	Car cars[50];
	int carCount = 0;
	int getCarPosition;
	char userInput;
	string tempStringContainer = "";
	
	
	do{
		cout << "\nWelcome" << endl;

		cout<< "Menu : " << endl;

		cout << "Create Car press '1'" << endl;

		cout << "View all car details press '2'" << endl;
	
		cout << "Update car details by plate number press '3'" << endl;
		
		cout << "Delete Car details press '4'" <<endl;
		
		cout << "Exit press 'e'" << endl;
		cin >> userInput;


	switch(userInput)
	{
		case '1':
			 //cars[carCount] = new Car();
        cout << "\nInsert car details ->" << endl;
            
			
			cout << "\nCar Serial Number : ";
            cin >> tempStringContainer;
            cars[carCount].setnumber(tempStringContainer);
            //cout<< car.getnumber();
            
            cout << "\nCar Model : ";
            cin >> tempStringContainer;
            cars[carCount].setmodel(tempStringContainer);
            
            cout << "\nCar Year : ";
            cin >> tempStringContainer;
            cars[carCount].setyear(atoi(tempStringContainer.c_str()));
           
            cout << "\nCar Type : ";
            cin >> tempStringContainer;
            cars[carCount].setType(tempStringContainer);
            
           cout << "\nCar Price : ";
            cin >> tempStringContainer;
            cars[carCount].setprice(atoi(tempStringContainer.c_str()));
            
            cout << "\nCar Status : ";
            cin >> tempStringContainer;
            cars[carCount].setstatus(tempStringContainer);
            
            cars[carCount].display();
            
            carCount++;
            
        break;
        
         case '2':
        for(int i = 0 ; i < carCount ; i++ )
        {
        cars[i].display();
        }
        break;
        
        case '3':
        cout << "\nEnter Serial Number : " ;
        cin >> tempStringContainer;
        for(int i =0; i<= carCount; i++){
        if(tempStringContainer!=cars[i].getnumber()){
        //i = 0;
        }else{
        cout << "Car number found! " << endl ;
        cout << "--- Update new details ---" << endl;
        
        cout << "Model : ";
        cin >> tempStringContainer;
        cars[i].setmodel(tempStringContainer);
        
        cout << "Year : " ;
        cin >> tempStringContainer; 
        cars[i].setyear(atoi(tempStringContainer.c_str()));
        
        cout << "Type : ";
        cin >> tempStringContainer;
        cars[i].setType(tempStringContainer);
        
        cout << "Price : " ;
        cin >> tempStringContainer; 
        cars[i].setprice(atoi(tempStringContainer.c_str()));
        
        cout << "Status : ";
        cin >> tempStringContainer;
        cars[i].setstatus(tempStringContainer);
        
        cout << "New Car Details ->" << endl;
        cars[i].display();
        }
        cout << "Car Serial number not found. " << endl;
        }
        break;
        default:
        ;
        
         case '4':
        for(int i = 0 ; i < carCount ; i++ )
        {
        cars[i].display();
   		if(NULL){
			cout<<"there is no Car details";}
		else {
			cout<<"please input the number you want to delete:";
			cin>> Car.getnumber();
			delete cars[i].delete(tempStringContainer);
			}
		}
	}
        

        break;
        
        }
        
     while(userInput != 'e');
    
   return 0;
        
			
	}
Last edited on
Your code is non-sens, you should learn a bit more of C++ and completly rewrite it. There are too much things to fix.
unfortunately, that is true ^^

cars[i].display();
if(NULL){ //null == false. this never triggers.
//c++ conditions are explicit, as are most languages. if you want x== null then you say that, it does not understand implied x.
cout<<"there is no Car details";}
else { //so this always triggers.
cout<<"please input the number you want to delete:";
cin>> Car.getnumber();
delete cars[i].delete(tempStringContainer); //do you want to always execute this no matter what? Its going to misbehave because the if statements are all wrong
}
}
Last edited on
@Zaap
thanks bro, that was so helpful..

@jonnin
atleast you can teach me how to add delete function there?
I'm sorry if I'm not that helpful, but I really tried to help, however there are so much things that helping you would be the same as coding for you, and the extra thing is that you wont even understand what was wrong so you will reproduce these errors and so create a new topic, etc...

I'm just saying that you should learn C++ a bit more in details, and then rewrite your code because trust me, there is nothing hard here, it's just an accumulation. So yes, maybe you hate me right now, but if you follow my advice you will thanks me in the futur.
what pointer exactly are you trying to delete, that is the question. It isnt clear from your code what exactly you want to do. I assume its cars[i], but that isnt a pointer (you can mark it deleted, being in an array that is about all you can do there).
delete cars[i].delete(tempStringContainer);

In C++, delete means "call the destructor of the object that this pointer is pointing at, and after that I don't care what happens to the memory that object occupies".

It does NOT have anything to do with removing objects from an array.

So when you say you want to "delete" an object from the array, what do you mean? Tell us without using the word "delete".

Last edited on
Split your code into functions to find the errors.
I wasn’t so patience to test it, but maybe this could be of some help:
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include <iostream>
#include <string>


class Car {
public:
    Car() = default;
    Car ( int,
          std::string,
          std::string,
          int,
          std::string,
          int,
          std::string );

    int getNo()             const { return No; }
    std::string getnumber() const { return number; }
    std::string getmodel()  const { return model; }
    int getyear()           const { return year; }
    std::string getType()   const { return type; }
    int getprice()          const { return price; }
    std::string getstatus() const { return status; }
    
    void setNo( int No_arg )                        { No = No_arg; }
    void setnumber( const std::string& number_arg ) { number = number_arg;  }
    void setmodel( const std::string& model_arg )   { model = model_arg; }
    void setyear( char year_arg )                   { year = year_arg; }
    void setType( const std::string& type_arg )     { type = type_arg; }
    void setprice( int price_arg )                  { price = price_arg; }
    void setstatus( const std::string& status_arg ) { status = status_arg; }
    void display() const;
    void clear();

private:
    int No {};
    std::string number;                         // serial number
    std::string model;
    int year { 1 };
    std::string type;
    int price {};
    std::string status;
};


Car::Car ( int No_arg,
           std::string number_arg,
           std::string model_arg,
           int year_arg,
           std::string type_arg,
           int price_arg,
           std::string status_arg)
    : No     { No_arg }
    , number { number_arg }
    , model  { model_arg }
    , year   { year_arg }
    , type   { type_arg }
    , price  { price_arg }
    , status { status_arg }
{
}


void Car::display() const
{
    std::cout << "No.:"                << No
              << "\nVehicle details:"
                 "\nSerial Number:   " << number
              << "\nModel:           " << model
              << "\nYear:            " << year
              << "\nType:            " << type
              << "\nPrice:           " << price
              << "\nStatus:          " << status;
}


void Car::clear()
{
    number.clear();
    model.clear();
    year = 0;
    type.clear();
    price = 0;
    status.clear();
}


void printMenu();
int getUserChoice();
void insertCarDetails( Car * const, int, int );
void listCars( const Car * const, int );
void updateCarDetails( Car * const, int );
int findCarInCstyleArray( const Car * const, int, const std::string& );
void clearCarDetails( Car * const, int, int );


int main()
{
    constexpr int Max_Cars { 50 };
    int car_count {};
    Car cars[Max_Cars];

    int user_input;
    do {
        user_input = getUserChoice();
        switch(user_input) {
        case 1:
            insertCarDetails( cars, Max_Cars, car_count++ );
            break;
        case 2:
            listCars( cars, Max_Cars );
            break;
        case 3:
            updateCarDetails( cars, Max_Cars );
            break;
        case 4:
            clearCarDetails( cars, Max_Cars, car_count );
            break;
        default:
            break;
        }
    } while (user_input != 5);
}


void printMenu()
{
    std::cout << "\nWelcome\nMenu: "
                 "\n1) Create Car"
                 "\n2) View all car details"
                 "\n3) Update car details by plate number"
                 "\n4) Delete Car details"
                 "\n5) Exit\n";
}


int getUserChoice()
{
    printMenu();
    int ans {};
    while ( ans < 1 || 5 < ans ) {
        std::cout << ">>> ";
        std::cin >> ans;
        if ( ans < 1 || 5 < ans ) {
            std::cout << "\nWrong choice. Try again.\n";
            printMenu();
        }
    }
    return ans;
}


// Data needed:
// int No --> count
// std::string number;
// std::string model;
// int year { 1 };
// std::string type;
// int price {};
// std::string status;
void insertCarDetails(Car * const cars, int size, int count)
{
    if ( size <= count ) {
        std::cout << "Cannot insert new cars.\n";
        return;
    }

    cars[count].setNo( count );

    std::cout << "\nPlease insert:\ncar Serial Number: ";
    std::string s;
    std::cin >> s;
    cars[count].setnumber(s);

    std::cout << "Car Model: ";
    std::cin >> s;
    cars[count].setmodel(s);

    std::cout << "Car year: ";
    int i;
    std::cin >> i;
    cars[count].setyear(i);

    std::cout << "Car type: ";
    std::cin >> s;
    cars[count].setType(s);

    std::cout << "Car Price: ";
    std::cin >> i;
    cars[count].setprice(i);
    
    std::cout << "Car status: ";
    std::cin >> s;
    cars[count].setstatus(s);

    cars[count].display();
}


void listCars(const Car * const cars, int size)
{
    for (int i {}; i < size; ++i) {
        cars[i].display();
        std::cout << '\n';
    }
    std::cout << '\n';
}


void updateCarDetails( Car * const cars, int size )
{
    std::cout << "\nEnter Serial Number: ";
    std::string s;
    std::cin >> s;

    int car { findCarInCstyleArray( cars, size, s ) };
    if ( -1 == car ) {
        std::cout << "Car Serial number not found.\n";
        return;
    }

    std::cout << "--- Update details ---\n";
    std::cout << "Model: ";
    std::cin >> s;
    cars[car].setmodel(s);

    std::cout << "Year: " ;
    int i;
    std::cin >> i;
    cars[car].setyear(i);

    std::cout << "Type : ";
    std::cin >> s;
    cars[car].setType(s);

    std::cout << "Price: " ;
    std::cin >> i; 
    cars[car].setprice(i);

    std::cout << "Status: ";
    std::cin >> s;
    cars[car].setstatus(s);

    std::cout << "New Car Details ->\n";
    cars[car].display();
    std::cout << '\n';
}


// Return -1 if not found
int findCarInCstyleArray( const Car * const cars,
                          int size,
                          const std::string& snum )
{
    for (int i {}; i < size; ++i) {
        if ( cars[i].getnumber() == snum ) { return i; }
    }
    return -1;
}


void clearCarDetails( Car * const cars, int size, int count )
{
    if ( !count ) {
        std::cout << "no cars inserted yet.\n";
        return;
    }

    std::cout << "Please input the number you want to clear: ";
    int num;
    std::cin >> num;
    while (count < num || size <= num) {
        std::cout << "Such car does not exist. Try again.\n>>> ";
        std::cin >> num;
    }

    cars[num].clear();
}

Topic archived. No new replies allowed.