Delete Function and Preset Data

Pages: 12
Hi I have written down this code but I don't get it why when I tried to delete it, it deletes the whole data.

Also my another concern is that I cant seem to store my preset data. Is there another way to do the preset data?

Please tell me if there is anything I should fix.

Here is my global variable
1
2
3
  int selection;
int n=0, i=0, x=1, y[10];
string h[10];


Here is my classes
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
class Property 
{
	private:
		string type;
		string name;
		string status;
		
	public:
		
		void set_details() ;
		void set_delprop();
		
		void get_details();
		void get_details1();
		
		Property(){}
		Property(string t, string n, string s)
		{
			type = t;
			name = n;
			status = s;
		}
		
};

//Derived Class
class Details : public Property
{
	private:
		Property e[10];
		
	public:
		Details(){}
		void AddProperty();
		void DeleteProperty();
		void ViewProperty();
		void SearchProperty();
		
		void presetdata()
		{
			Property prop1("Residential","Terrace","Rent");
			Property prop2("Residential","Bungalow","Sale");
			Property prop3("Commercial","Factory","Sale");
			Property prop4("Commercial","Malls","Sale");
			Property prop5("Commercial","Farmlands","Rent");
		}
	
};


Here is my related function
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
void Property::set_details()
{
	system("cls");
	
	cout<<n;
	cout<<"\n\n\t\t\t\tType of Property :";
	cout<<"\n\t\t\t\tPlease enter [ Residential || Commercial ]";
	cout<<"\n\t\t\t\t>>";
	cin>>type;
	
	cout<<"\n\n\t\t\t\tName of Property :";
	cout<<"\n\t\t\t\tPlease enter [ eg: Apartment || Terrace || Factory || Malls || etc.]";
	cout<<"\n\t\t\t\t>>";
	cin>>name;
	
	cout<<"\n\n\t\t\t\tStatus of Property :";
	cout<<"\n\t\t\t\tPlease enter [ Sale || Rent ]";
	cout<<"\n\t\t\t\t>>";
	cin>>status;
	
	h[i] = name;
	
}

void Details::DeleteProperty()
{
	string tempName;
	int a;
	bool lol;
	lol = false;
	a=n;
	char redo2;
	
	cout << "Name of property to remove: ";
	cin>>tempName;
  
	for(i=0;i<n;++i)
	{
		if(h[i]==tempName)
		{
			e[i]=e[i+1];
			lol = true;
			e[i].set_delprop();
   	  	    cin>>redo2;
           if(redo2=='Y'||redo2=='y'){
           int a;
           a=n;

			for(int i=0;i<a;i++)
			{
				e[i].get_details();
				a--;
				n=a;
				n++;
			}
		}
		
	}
	}
	if (lol==false)
		{
			cout<<"\nThere's no such property in the system.";
		}
		
		getch();
}

void Property::set_delprop()
{
cout<<"Do you really want to delete the property : " <<name<<"\t(y/n)?";
name = h[i];
}


Thank you for any help in advance.
Hello nomnomu,


Please tell me if there is anything I should fix.


Since you asked.

Get rid of the global variables. You could change them without realizing it and then some part of your code will be using the wrong value.

It is better to define your variables in "main" and pass them to the functions that need them.

Come up with better variable names that mean what they are for. "i", "n", "x" ,"y", "h" and "lol" may be meaningful to you, but not to anyone else. Consider this: you finish the program and think you are done, but in 6 months you are asked to make changes to this program. How much time will you waste just trying to figure out what the variables are for because you have forgotten about them.

Add some blank lines to break up your code. You want someone to read what you have done, and I know that you do, then make it as easy to read as you can. The first benefit is to you. Also watch your indenting. It is not consistent, but it should be.

In your function line 71 what happens when somewhere in you un-posted code "i" gets a new value and line 71 sets the wrong name?


Hi I have written down this code but I don't get it why when I tried to delete it, it deletes the whole data.


Which lines are you talking about?

What are you meaning? Deletes 1 element of the array or the whole array?

With the missing code I am not sure if I can come up with something to test what you have posted. It is best to provide enough of a program that can be compiled and run. It does not have to be the full code just enough to test it.

Andy
Hi thanks for reply, Andy.

In your function line 71 what happens when somewhere in you un-posted code "i" gets a new value and line 71 sets the wrong name?

That does make sense. I will fix the global variables, thank you for pointing it out.

What are you meaning? Deletes 1 element of the array or the whole array?


I wanted to delete just 1 element from the array and its details, but my code seems to delete the whole array. I will provide the whole code but note that I have yet to fix my global variable (please forgive me in that sense).

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
278
279
280
281
282
283
284
285
286
287
288
#include<iostream>
#include<string>
#include<conio.h>

using namespace std;

int main();
void Exit();
void Header();
void Transfer();

int selection;
int n=0, i=0, x=1, y[10];
string h[10];

//=========================================================================
//Base Class
class Property 
{

	private:
		string type;
		string name;
		string status;
		
	public:
		

		void set_details() ;
		void set_delprop();
		

		void get_details();
		void get_details1();
		

		Property(){}
		Property(string t, string n, string s)
		{
			type = t;
			name = n;
			status = s;
		}

};

//Derived Class
class Details : public Property
{
	private:
		Property e[10];
		
	public:
		Details(){}
		void AddProperty();
		void DeleteProperty();
		void ViewProperty();
		void SearchProperty();
		
		void presetdata()
		{
			Property prop1("Residential","Terrace","Rent");
			Property prop2("Residential","Bungalow","Sale");
			Property prop3("Commercial","Factory","Sale");
			Property prop4("Commercial","Malls","Sale");
			Property prop5("Commercial","Farmlands","Rent");
		}
		
		void poly()
		{
			cout<<"\n\n\t\t Residential / Commercial";
		}
	
};

//=============================================================================================

void Details::AddProperty()
{
		n++;
		e[i].set_details();
		i++;
		
		cout<<"\n\nProperty with the following information has been successfully added into the system:"<<endl;
		
		for(int i=0; i<n; i++)
		{
			e[i].get_details();
		}


}

void Details::DeleteProperty()
{
	string tempName;
	int a;
	bool lol;
	lol = false;
	a=n;
	char redo2;
	
	cout << "Name of property to remove: ";
	cin>>tempName;
  
	for(i=0;i<n;++i)
	{
		if(h[i]==tempName)
		{
			e[i]=e[i+1];
			lol = true;
			e[i].set_delprop();
   	  	    cin>>redo2;
           if(redo2=='Y'||redo2=='y'){
           int a;
           a=n;

			for(int i=0;i<a;i++)
			{
				e[i].get_details();
				a--;
				n=a;
				n++;
			}
		}
		
	}
	}
	if (lol==false)
		{
			cout<<"\nThere's no such property in the system.";
		}
		
		getch();
}

void Details::ViewProperty()
{
	system ("cls");
	
	int i=0;
	
	system("cls");
	cout<<"\n==================================================="<<endl;
	cout<<"\n\t\t\t\t\t\t Viewing All Properties"<<endl;
	cout<<"\n==================================================="<<endl;
	cout<<"  Name of Property  ||\tType of Property\t||\tAvailability\t||";
	cout<<"\n===================================================";
	
	for(int i=0;i<n;i++)
	{
		e[i].get_details();
	}
	
	Transfer();
}

void Details::SearchProperty()
{
	int c;
	string propname;
	
		cout<<"\n\n\t\t\tPlease enter the property name."<<endl;
		cin>>propname;
		for(int i=0;i<n;i++)
		{
			if(h[i]==propname)
			{
				cout<<n<<endl;
				e[i].get_details1();
			}
		}
		
		Transfer();	
}


//=============================================================================================

void Property::set_details()
{
	system("cls");
	
	cout<<n;
	cout<<"\n\n\t\t\t\tType of Property :";
	cout<<"\n\t\t\t\tPlease enter [ Residential || Commercial ]";
	cout<<"\n\t\t\t\t>>";
	cin>>type;
	
	cout<<"\n\n\t\t\t\tName of Property :";
	cout<<"\n\t\t\t\tPlease enter [ eg: Apartment || Terrace || Factory || Malls || etc.]";
	cout<<"\n\t\t\t\t>>";
	cin>>name;
	
	cout<<"\n\n\t\t\t\tStatus of Property :";
	cout<<"\n\t\t\t\tPlease enter [ Sale || Rent ]";
	cout<<"\n\t\t\t\t>>";
	cin>>status;
	
	h[i] = name;
	
}

void Property::set_delprop()
{
cout<<"Do you really want to delete the property : " <<name<<"\t(y/n)?";
name = h[i];
}


void Property::get_details()
{
	cout<<type<<"\t\t"<<name<<"\t\t\t"<<status<<endl;
}

void Property::get_details1()
{
	cout<<"\n Type of Property : "<< "\t" << type;
	cout<<"\n Name of Property : "<< "\t" << name;
	cout<<"\n Status of Property : "<< "\t" << status;
}

//=====================================================================

void Transfer ()
{
	while (1){
		cout<<"\n\n\n\n\n\t\t\t\t\t1 : Main Menu";
		cout<<"\n\t\t\t\t\t2 : Exit the program";
		cout<<"\n\n\t\t\t\tEnter selection : ";
		cin>>selection;
		
		switch(selection)
		{
			case 1 : main (); break;
			case 2 : Exit ();	break;
		
			default : cout<<"\n=====Invalid selection====="<<endl; 
                                     cout << "Press Enter to continue."<<endl; getch(); break;
		}
		system("cls");
	}
	return;
}

void Exit ()
{
	system ("cls");
	cout<<"\n ================== Thank you ==================";
	exit(0);
}

int main()
{
	Details info;
	system ("cls");

	while(1){
		
		cout<<"\n\t\t\t\t\t1 : Add details of item"<< endl;
		cout<<"\t\t\t\t\t2 : Delete item details"<< endl;
		cout<<"\t\t\t\t\t3 : View all category of item"<< endl;
		cout<<"\t\t\t\t\t4 : View a property"<< endl;
		cout<<"\t\t\t\t\t0 : Exit" << endl;
		
		cout << endl << "\t\t\tEnter selection : ";
		cin>>selection;

	switch(selection)	{

			case 1 : info.AddProperty(); getch(); break;
			case 2 : info.DeleteProperty(); break;
			case 3 : info.ViewProperty(); break;
			case 4 : info.SearchProperty(); break;
			case 0 : Exit(); break;
			
			default: 
			cout <<"\nInvalid selection"<<endl;
			cout << "Press Enter to continue" <<endl;
			getch();
			break;
			
		}
		system("cls");
	}
	return 0;
	
}


Thank you.
Last edited on
Take a step back here.

Why do you need h at all? Isn't all the property details stored in the e array in Details? Shouldn't Details maintain a counter of how many properties details are held?

presetdata() doesn't do what you think it does. It creates a bunch of local variables which aren't used and are then discarded when the function ends.

Before you get into deletion etc, get Property class to obtain data and to display/return this data. Then you want Details to obtain details of a number of propetries and to be able to display them.

Only once you can add/display data for Details do you then think about deletion.

Code one step at a time, get that step to compile OK, then test to make sure work. Then code the next step, get that to work OK, then progress....

You should also have a program design before you start coding so that you know the required classes, class members, class functions, inter-relation between the classes, what input is needed, output required etc etc.

Hello nomnomu,

Thank you for the code. I was stumped on how to make it compile and run. I will give it at test.

"conio.h" is not a standard C++ header file. Not everyone has it or can use it. If this is for you use or for school that is fine, but still can be avoided.

I like to use this where "getch()" is needed:
1
2
3
4
5
// <--- Keeps console window open when running in debug mode on Visual Studio. Or a good way to pause the program.
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();

It is the last 2 or 3 lines that are important. Since C++ does not have anything to match "getch()" this is as close as I have found to match. Also your compiler must be old because I have always had to use "_getch()" so my compiler would not complain or I have to add "#define _CRT_SECURE_NO_WARNINGS" as the very first line in the file.

The function void presetdata() in the "Details" class create 5 objects as part of the class. Is this what you want or should it be separate from the class?

Andy

Edit: P.S. forgot to ask if you know about vectors yet?
Last edited on
Hello seeplus, thank you for replying

Why do you need h at all? Isn't all the property details stored in the e array in Details? Shouldn't Details maintain a counter of how many properties details are held?


I created h because I supposed that e can't be accessed since it is in private class.

presetdata() doesn't do what you think it does. It creates a bunch of local variables which aren't used and are then discarded when the function ends.


Is that so, but I needed preset data. Is there any other way to make it work?

Before you get into deletion etc, get Property class to obtain data and to display/return this data. Then you want Details to obtain details of a number of propetries and to be able to display them.


Im sure it is able to display the details input by user. I'll add the number after.

You should also have a program design before you start coding so that you know the required classes, class members, class functions, inter-relation between the classes, what input is needed, output required etc etc.


Okay, I'll jot it down for my next code.

Thank you so much, seeplus.

Hello, Andy,

It is the last 2 or 3 lines that are important. Since C++ does not have anything to match "getch()" this is as close as I have found to match. Also your compiler must be old because I have always had to use "_getch()" so my compiler would not complain or I have to add "#define _CRT_SECURE_NO_WARNINGS" as the very first line in the file.


We were told to use Dev C++ to do our coding.

The function void presetdata() in the "Details" class create 5 objects as part of the class. Is this what you want or should it be separate from the class?


Yes, we need to have 5 objects that are already in system.

forgot to ask if you know about vectors yet?


No, we haven't learned vectors yet, thus, we cannot use vectors.

Thank you so much, Andy.
Hello nomnomu,

I do agree with seeplus that you need to step back an rethink the program.

What you have can be mad to work, but it is not easy.

One of the biggest problem I had was the single letter variable names. Some of which I still do not know what to call them like bool lol; which should be written as: bool lol{};. This with the empty {}s the compiler will set the variable to the correct form of (0)zero based on the variables type. You do not need 2 lines to do this. Please come up with a better that describes what it is for so I do not end up ROFL every time I see it.

One part I have not worked on yet is in the "Add" function. Before you add to the array in "Details" you need to check if there is room left, so you do not end up trying to add past the end of the array. If you can use a "std::vector" that would eliminate the problem.

As you will see I have changed some of the variable names and reworked the "presetdata" function to make use of it. As seeplus mentioned your original function did nothing.

I have not moved the global variables yet, but they still need to be moved. In general any non constant variable should be avoided.

In your programs prefer to use the newline, (\n), over the function "endl". These days the (\n) works much the same as "endl".

I do not know where you picked up using all those (\t)s, but most of the time forget that you ever learned it. In some places you use to many and it places the output to far to the right on the screen. Any (\t) not at the beginning of a string does not always line up the way you are thinking.

An example:
==========================================================================
                         Viewing All Properties
==========================================================================
  Name of Property  ||  Type of Property        ||      Availability    ||
==========================================================================
Residential             Terrace                 Rent
Residential             Bungalow                        Sale
Commercial              Factory                 Sale
Commercial              Malls                   Sale
Commercial              Farmlands                       Rent



Instead of trying to explain everything as it is late for me this code and the comments should help.

Here is part of the code:
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
#include<iostream>
#include<string>
#include<conio.h>

#include <chrono>
#include <thread>

using namespace std;

constexpr int MAXSIZE{ 10 };

//int main();  // <--- This should not needed here.
void Exit(bool& done);
//void Header();  // <--- Not in this code.
void Transfer(bool& done);

int selection;
int numOfProperitiesUsed{}, i{}, x{ 1 }, y[MAXSIZE]{};  // <--- ALWAYS initialize all your variables.
string h[MAXSIZE]{ "Residential", "Residential", "Commercial", "Commercial", "Commercial" };

//=========================================================================
//Base Class
class Property
{
    private:  // <--- A class is "private" by default. This is not needed, but OK if you leave.
        string type;
        string name;
        string status;

    public:
        void set_details();
        void set_delprop();
        void set_details(std::string inType, std::string inName, std::string inStatus);  // <--- Added.

        void get_details();
        void get_details1();

        Property() {}
        Property(string inType, string inName, string inStatus)  // <--- Changed.
        {
            type = inType;
            name = inName;
            status = inStatus;
        }
};

//Derived Class
class Details : public Property
{
    private:
        Property properties[MAXSIZE];

    public:
        Details() {}
        void AddProperty();
        void DeleteProperty();
        void ViewProperty(bool& done);
        void SearchProperty(bool& done);
        //void setDetails(std::string inType, std::string inName, std::string inStatus);

        void presetdata()
        {
            properties[0].set_details("Residential", "Terrace", "Rent");
            properties[1].set_details("Residential", "Bungalow", "Sale");
            properties[2].set_details("Commercial", "Factory", "Sale");
            properties[3].set_details("Commercial", "Malls", "Sale");
            properties[4].set_details("Commercial", "Farmlands", "Rent");

            numOfProperitiesUsed = 5;
        }

        void poly()  // <--- Not sure what this is for. I do not recall seeing it used anywhere.
        {
            cout << "\n\n\t\t Residential / Commercial";
        }
};

//=============================================================================================

void Details::AddProperty()
{
    properties[numOfProperitiesUsed].set_details();
    //i++;

    cout << "\n\nProperty with the following information has been successfully added into the system:\n";

    numOfProperitiesUsed++;  // <--- Moved.

    for (int i = 0; i < numOfProperitiesUsed; i++)
    {
        properties.get_details();
    }
}

void Details::DeleteProperty()
{
    string tempName;
    int a;  // <--- Not sure why this is needed. you could just use "numOfProperitiesUsed".
    bool lol;
    lol = false;
    char redo2;

    a = numOfProperitiesUsed;

    cout << "Name of property to remove: ";
    cin >> tempName;

    for (i = 0; i < numOfProperitiesUsed; ++i)
    {
        if (h[i] == tempName)
        {
            properties[i] = properties[i + 1];

            lol = true;

            properties[i].set_delprop();

            // <--- The "cin" [b][i]ALWAYS needs a prompt.[/b]
            cin >> redo2;

            if (redo2 == 'Y' || redo2 == 'y')
            {
                int a;  // <--- This is a local variable. it overshadows the 1st definition of "a".

                a = numOfProperitiesUsed;

                for (int i = 0; i < a; i++)
                {
                    properties[i].get_details();

                    a--;

                    numOfProperitiesUsed = a;  // <--- Subtract 1 from "numOfProperitiesUsed"'

                    numOfProperitiesUsed++;  // <--- Add 1. Is there a point to this? Should this be here?
                }
            }
        }
    }

    if (lol == false)
    {
        cout << "\nThere's no such property in the system.";
    }

    _getch();
}

void Details::ViewProperty(bool& done)
{
    system("cls");

    int i = 0;
    std::string equalLine(74, '=');

    system("cls");

    cout
        << equalLine << '\n'
        << std::string((74 - 23) / 2, ' ') << "Viewing All Properties\n"
        << equalLine << '\n'
        << "  Name of Property  ||\tType of Property\t||\tAvailability\t||\n"
        << equalLine << '\n';

    for (int i = 0; i < numOfProperitiesUsed; i++)
    {
        properties[i].get_details();
    }

    Transfer(done);
}

void Details::SearchProperty(bool& done)
{
    //int c;  // <--- Defined, but never used.
    string propname;

    cout << "\n\n\tPlease enter the property name: ";
    cin >> propname;

    for (int i = 0; i < numOfProperitiesUsed; i++)
    {
        if (h[i] == propname)
        {
            cout << /*numOfProperitiesUsed <<*/ '\n';  //<--- The commented part is OK for testing, but screws up normal output.
            properties[i].get_details1();
        }
    }

    Transfer(done);
}
And the rest of the code:
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


//=============================================================================================

void Property::set_details(std::string inType, std::string inName, std::string inStatus)
{
    type = inType;
    name = inName;
    status = inStatus;
}

void Property::set_details()
{
    system("cls");

    cout << numOfProperitiesUsed;

    cout << 
        "\n\n\tType of Property :"
        "\n\tPlease enter [ Residential || Commercial ]"
        "\n\t>> ";
    cin >> type;

    cout << 
        "\n\n\tName of Property :"
        "\n\tPlease enter [ eg: Apartment || Terrace || Factory || Malls || etc.]"
        "\n\t>> ";
    cin >> name;

    cout << 
        "\n\n\tStatus of Property :"
        "\\tPlease enter [ Sale || Rent ]"
        "\n\t>> ";
    cin >> status;

    h[i] = name;

}

void Property::set_delprop()
{
    cout << "Do you really want to delete the property : " << name << "\t(y/n)? ";
    name = h[i];
}

void Property::get_details()
{
    cout << type << "\t\t" << name << "\t\t\t" << status << '\n';
}

void Property::get_details1()
{
    cout << "\n Type of Property : " << "\t" << type;
    cout << "\n Name of Property : " << "\t" << name;
    cout << "\n Status of Property : " << "\t" << status;
}

//=====================================================================

void Transfer(bool& done)
{
    bool isDone{};

    while (1)
    {
        cout << "\n\n\n\n\n\t\t1 : Main Menu";
        cout << "\n\t\t2 : Exit the program";
        cout << "\n\n\t\tEnter selection : ";
        cin >> selection;

        switch (selection)
        {
            //case 1: main(); break;
            case 1:
                return;
            case 2:
                Exit(done);

                isDone = true;
                break;

            default:
                cout <<
                    "\n\t\t=====Invalid selection=====\n"
                    "\t\tPress Enter to continue.\n";

                _getch();
                break;
        }

        system("cls");

        if (isDone)
            break;
    }

    return;  // <--- This is not required or necessary.
}

void Exit(bool& done)
{
    system("cls");
    cout << "\n\n     ================== Thank you ==================";
    std::this_thread::sleep_for(std::chrono::seconds(2));  // Requires header files "chrono" and "thread". Optional as is the header files.

    done = true;
    //exit(0);  // <--- Do not use "exit". Return to main and exit there at the closing }.
}

int main()
{
    Details info;

    info.presetdata();
    
    system("cls");

    bool done{};

    while (1)
    {
        //Header();

        cout <<
            "\n"
            "\t\t          MAIN MENU\n"
            "\t\t-----------------------------\n"
            "\t\t1 : Add details of item\n"
            "\t\t2 : Delete item details\n"
            "\t\t3 : View all category of item\n"
            "\t\t4 : View a property\n"
            "\t\t0 : Exit\n\n"
            "\t\t Enter selection : ";
        cin >> selection;

        switch (selection)
        {
            case 1:
                info.AddProperty();
                // <--- The "getch" needs a prompt.
                _getch();
                break;
            case 2: info.DeleteProperty(); break;
            case 3: info.ViewProperty(done); break;
            case 4: info.SearchProperty(done); break;
            case 0: 
                Exit(done);
                done = true;
                break;

            default:
                cout << "\nInvalid selection\n";
                cout << "Press Enter to continue\n";

                _getch();
                break;
        }

        if (done)
            break;

        system("cls");
    }

    return 0;
}


Andy
Hi Andy,

Alright, I will take note for future references.
Thank you for the code but my compiler can't compile it, sadly.
Hello nomnomu,

What IDE/compiler are you using?

Andy
The compile issue is L91 in the first section

1
2
3
4
for (int i = 0; i < numOfProperitiesUsed; i++)
{
    properties.get_details();    // HERE
}


As a minimum it should be:

 
properties[i].get_details();


which compiles. But get in the add function?????

And having global variables i, x, y in L18 is not good at all. If you really, really want to have global variables, at least give them meaningful names (and preference by say g_ to make them stand out). As the program is coded, there are scope issues with the global and local varibles.....

When you have a non-POD type as a function argument, pass by ref and not by value (unless a copy is required).

L17 selection isn't initialised, despite the comment on L18.

This design still needs a re-think!
Last edited on
Hi Andy,
What IDE/compiler are you using?

I am using Dev C++ .

Hi seeplus,
I re-do my whole code and removed my global variable as well but now I seem to encounter different problems. Should I edit this topic or should I just close this topic and post another topic?
Hello nomnomu,

I would keep going with this 1. Just post your new code.

There is a good chance that your IDE is not set for the C++11 standards. If not this may help:

The DEV C++ that I have is version 5.11 with a build year of 2015.

To adjust the settings:
    •	Under the Tools menu choose “Compiler Options”.
    •	In the window that comes up you will see tabs for “General”, “Settings”, “Directories” and “Programs”.
    •	Choose the settings tab.
    •	In the next set of tabs that come up choose “Code Generation”.
    •	The last line should say “Language Standard (-std).
    •	On the right side of that line click on the down arrow.
    •	In the list box that comes up choose “ISO C++ 11”.
    •	Press “OK”.

This will let the IDE and compiler use the C++11 standards.


You should change your settings to use the 2011 standards at the very least. A better option would be to get a newer IDE/compiler. The MSVS IDEs are good and Code::Blocks is also a good IDE. Today the 2011 standards are 10 years old making an IDE using the 2014 or 2017 standards a better choice.

Andy

Edit: format
Last edited on
Hi Andy,

Thank you for the guide. I have set up my IDE with the C++11 standards.

It's just been 3 months since I've been studying this but I hope my code this time is better.

My new problem is that (I have 5 preset data) when I tried adding new details for the second time (Meaning I'm adding to my number 7), the new details overwrites my previous details (number 6). My next concern is I have tried the search function again but it doesn't work this time around.

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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include <iostream>
#include <cstring>

using namespace std;

int main();
void Header();

//Classes================================================================
//Base Class
class Property
{
	//Private attribute (Encapsulation)
	private:
		int no;
		string type;
		string name;
		string status;
		
		public:
			
			//Setter (Encapsulation)
			void set_type(string t){type = t;}
			void set_name(string n){name = n;}
			void set_status(string s){status = s;}
			void set_newproperty();
						
			void set_delete()
			{
				type = " ";
				name = " ";
				status = " ";
			}
			
			void set_preset(int o, string y, string a, string u)
			{
				no = o;
				type = y;
				name = a;
				status = u;
			}
			
			//Getter (Encapsulation)
			string get_type(){return type;}	
			string get_name(){return name;}
			string get_status(){return status;}
			void get_property();
			
			//Constructor
			Property(){	}
			
			//Polymorphism
			void statement(){cout<<"Property Management";}
			
};

//Derived Class
class Details : public Property
{
	//Private attribute (Encapsulation)
	private :
		//Array
		Property properties[10];
		
	public :
		//Functions
		void addDetails();
		void deleteDetails();
		void viewAll();
		void viewOne();
		
		void presetData()
		{
			properties[0].set_preset(1, "Residential", "Apartment", "Sale");
			properties[1].set_preset(2, "Residential", "Terrace", "Rent");
			properties[2].set_preset(3, "Residential", "SemiD", " Sale");
			properties[3].set_preset(4, "Commercial", "Factory", "Sale");
			properties[4].set_preset(5, "Commercial", "Malls", "Sale");
			properties[5].set_preset(6, " ", " ", " ");
			properties[6].set_preset(7, " ", " ", " ");
			properties[7].set_preset(8, " ", " ", " ");
			properties[8].set_preset(9, " ", " ", " ");
			properties[9].set_preset(10, " ", " ", " ");
		}
		
		//Polymorphism
		void statement(){cout<<"by me";}
		
};

//Base Class Part)===================================

void Property::set_newproperty()
{
	//Local variable
	int countafterpreset = 6;
	no = countafterpreset;
	
	cout
		<< endl
		<< "Type of property [ Residential || Commercial ] : ";
	cin >> type;

	cout
		<<endl<<endl
		<< "Name of Property [ Terrace || Bungalow || Farmlands || etc. ] : ";
	cin >> name;
	
	cout
		<<endl<<endl
		<< "Status of Property [ Sale || Rent ] : ";
	cin >> status;
	
}

void Property::get_property()
{
	cout << "   " << no << "\t   " << type << "\t\t   " << name << "\t    " << status << " "
		 <<endl;
}

//Derived Class Part======================================

void Details::addDetails()
{
	//Local variable
	int counter = 5;
	
	//Clear screen
	system("cls");
	
	properties[counter].set_newproperty();
	
	cout <<endl
		 << endl << endl
		 <<"The following property has been added into the system"
		 <<endl << endl
		 << "|| No. || Type of Property || Name of Property || Status ||"
		 <<endl
		 << "-----------------------------------------------------------"
		 <<endl;
		 
	properties[counter].get_property();
	
	counter++;
}

void Details::deleteDetails()
{
	//Local variables
	int todelete, counter;
	string temp_type , temp_name, temp_status;
	
	//Clear Screen
	system("cls");
	
	viewAll();
	
	cout << endl
		 << "Input the number of the property that you want to delete : " << endl;
	cin >> todelete;
	
	for (counter=todelete;counter<10;counter++){
		
	properties[counter-1].set_delete();
	
	temp_type = properties[counter].get_type();
	temp_name = properties[counter].get_name();
	temp_status = properties[counter].get_status();
	
	properties[counter-1].set_type(temp_type);
	properties[counter-1].set_name(temp_name);
	properties[counter-1].set_status(temp_status);
	}
	
	cout <<endl
		 << "The property has been removed from the system"
		 <<endl << endl
		 << "The following is the new property list."
		 <<endl
		 << "\t\tPress ENTER to view";
	
	_getch();
	
	system ("cls");
	viewAll();
}

void Details::viewAll()
{
	//Local variable
	int counter;
	
	//Clear screen
	system("cls");
	
	Header();
	
	cout << endl<<endl
			 << "-----------------------------------------------------------"
			 <<endl
			 << "|| No. || Type of Property || Name of Property || Status ||"
			 <<endl
			 << "-----------------------------------------------------------"
			 <<endl;
			 
	for(counter = 0; counter < 10; counter++ )
	{	 
		properties[counter].get_property();
	}
}

void Details::viewOne()
{
	//Local variable
	string tempname;
	
	system("cls");
	
	cout << endl
		 << "Input the name of the property : ";
	
	cin >> tempname;
	
	for(int i = 0; i < 10; i++)
	{
		if(properties[i] == tempname)
		{
			cout<<properties[i].get_property();
			break;
		}
	}
	if(i==10)
	{
		cout << "Property Does Not Exist";
	}
	return;
	

}

//===============================================

void Header ()
{
	Property call;
	Details call1;
	
	cout << endl
		 << "=========================================================================================================="
	 	 << endl << endl
		 << "\t\t\t\t\t  ";
	
	call.statement();
	
	cout << endl
		 << "\t\t\t\t\t      ";
	
	call1.statement();
	
	cout << endl << endl
	   	 <<"==========================================================================================================="
		 << endl;
}

//================================================

int main()
{
	//Local Variable
	int selection;
	
	//Constructor calls
	Details info;
	info.presetData();
	
	//Clear screen
	system ("cls");

	while(1){
			
		Header();
		
		cout << endl
			 <<"\t\t\t\t\t1 : Add details of item"<< endl
			 <<"\t\t\t\t\t2 : Delete item details"<< endl
			 <<"\t\t\t\t\t3 : View all category of item"<< endl
			 <<"\t\t\t\t\t4 : View a property"<< endl
			 <<"\t\t\t\t\t0 : Exit " 
			 << endl << endl 
			 << "\t\t\tEnter selection : ";
			 
		cin>>selection;

	switch(selection)	
	{

			case 1 : info.addDetails(); _getch(); break;
			case 2 : info.deleteDetails(); _getch(); break;
			case 3 : info.viewAll(); _getch(); break;
			case 4 : info.viewOne(); break;
			case 0 : return (0); break;
			
			default: 
			cout <<"\nInvalid selection"<<endl
				 << "Press Enter to continue" <<endl;
				 
			_getch();
			break;
			
		}
		system("cls");
	}
	return 0;
	
}


I think I have some problem with my increment for the Add new details but I cant seem to find out how to fix it.

Thank you.
Last edited on
Hello nomnomu,

This morning I took a look at the "Delete" function. What you have does not work. When you have a match you only move 1 element to the right left, but miss the rest. When keeping track of "numOfProperitiesUsed" you have really over worked what you need. The variable "a" is not needed. Only when you find a match and shift everything to the right left 1 do you subtract 1 from "numOfProperitiesUsed".

I reworked your code to give you something to start with:
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
void Details::DeleteProperty()
{
    string nameToRemove;  // <--- Changed.
    bool found{};         // <--- Changed.
    char redo2{};

    system("cls");        // <--- Added

    cout <<               // <--- Changed.
        "\n\n"
        "\t         DELETE A PROPERTY\n\t" << std::string(35, '-') << "\n\n"
        "\tName of property to remove: ";
    cin >> nameToRemove;

    properties[i].set_delprop(nameToRemove);    // <--- Changed function.

    cin >> redo2;

    for (i = 0; i < numOfProperitiesUsed; ++i)
    {
        if (properties[i].GetName() == nameToRemove)
        {
            for (int j = i; j < numOfProperitiesUsed - 1; j++)  // <--- The - 1 keeps you from going past the end of the array.
            {
                properties[j] = properties[j + 1];
            }

            found = true;
            
            numOfProperitiesUsed--;

            if (redo2 == 'Y' || redo2 == 'y')
            {
                std::cout << '\n';

                for (int i = 0; i < numOfProperitiesUsed; i++)
                {
                    properties[i].get_details();
                }
            }
        }
    }

    if (!found)  // <--- Does the same as your original code.
    {
        cout << "\nThere's no such property in the system.";
    }

    std::cout << "\n\n  Press Enter to continue.";  // <--- Added.

    _getch();
}

I am not saying this is the best way, but it does work.

I asked once before if you know about vectors? This would work much better especially with the "Delete" function. Much less work.

Also do you know about the "<iomanip>" header file along with "std::setw()"? In the "Property::get_details" function using "std::setw()" gives an output of:

==========================================================================
                         Viewing All Properties
==========================================================================
  Type of Property  ||  Name of Property        ||      Availability    ||
==========================================================================
Residential             Terrace                            Rent
Residential             Bungalow                           Sale
Commercial              Factory                            Sale
Commercial              Malls                              Sale
Commercial              Farmlands                          Rent





                1 : Main Menu
                2 : Exit the program

                Enter selection :


You can see how everything is nicely lined up. It works better than the (\t)s. Another thought I just had is you might be able to resize a string up to a larger size to space things differently. Not sure as I have never tried to resize up.

See what you think and what you can do with it.

Andy
Hello nomnomu,

I will check out your new code and see what I find.

Andy
Hi Andy,

I reworked your code to give you something to start with:

Thank you so much, I will work on my old code again based on the code you reworked.


I asked once before if you know about vectors? This would work much better especially with the "Delete" function. Much less work.
Also do you know about the "<iomanip>" header file along with "std::setw()"? In the "Property::get_details" function using "std::setw()" gives an output of:

No, we have yet to study vectors and I did ask my lecturer if we were able to use vector but was rejected. For the <iomanip>, I have never heard of it but I think it should be fine to use it to have the output nicely arranged just like what u have shown me. However, I have no idea how to use <iomanip>.

Another thought I just had is you might be able to resize a string up to a larger size to space things differently. Not sure as I have never tried to resize up.

For the size, we are also restricted with the maximum of 10.

Thank you.
Last edited on
Why is class Details derived from class Property? This doesn't seem right. Details has the array of Property which is as expected.

The code mentions polymorphism. There's no polymorphism here. Details::statement() will execute the Details function and Property::statement() will execute the Property statement.

There is no requirement for no in Property. But Details should maintain a variable for the number of properties elements used. Should no be in Details and not Property?

There are other code issues, but you need to get the design right before we get into the coding issues.

Hello nomnomu,

"<iomanip>" as the name tends to imply works with "<iostream>" to manipulate what is in a "cout" statement. You are likely to first learn about it when you start using "doubles" to store numbers and "setprecision" would be the first part you learn. The "std::setw()" is not that hard to understand. It creates a block to put something in. Varying the width of that block is used to space columns across the screen.

I do not know where you came up with needing "<cstring>", but this is the C++ version of the C header file "<string.h>". "string.h" and the C++ "string" are not the same nor can they be used the same. "string" contains the string class and all the functions that go with the class. "string.h" knows nothing about classes.

You have changed to using "_getch()". I am not sure if Dev C++ needs this, but I know my MSVS 2017 does. Whichever works for you the header file "conio.h" is still needed.

"int main()" should never need to be forward declared, or prototyped, if you have to then there is something else wrong somewhere.

In the "Property" class your 1st variable is "int no;". Is the word no, the opposit of yes, or short for number. And what is it doing here when it should be in the "Details" class where it is needed to keep track of the array. Still need a better name and do not be afraid of longer variable names.

A suggestion:
1
2
3
4
5
6
7
8
class Property
{
    //Private attribute (Encapsulation)
    private:
        int no;
        string mType;
        string mName;
        string mStatus;

I have also seen:
1
2
3
4
5
6
7
8
class Property
{
    //Private attribute (Encapsulation)
    private:
        int no;
        string m_type;
        string m_name;
        string m_status;

Whether you use the single letter "m" or the "m_" it helps to set off the member variables of a class from a regular variable. So in a line like:void set_preset(int o, string y, string a, string u) you could writhe the function as:
1
2
3
4
5
6
7
void set_preset(int no, string type, string name, string status)
{
    mNo = no;
    mType = type;
    mName = name;
    mStatus = status;
}

And your code is much easier to follow and understand.

I am not the expert on classes, but I do not believe that "void statement()" is "Polymorphism", but I could be wrong.

In the "Details" class in the private section this is where you need a variable to keep track of how much of the array is used. Having to call a "get" or "set" function just to use a private variable from another class is wrong.

In the "presetData" function you are good from "[0] to [4]", but "[5] - [9]" are not necessary. I see now that the "no" in "Property" is a counting number. If you are using this number in the "Add" function that is why you are off.

In "Details" when you create the array it contains 10 elements of the object "Property". The "std::string"s are empty and have no value, but the "no" is an uninitialized variable and has a garbage value until you give it something.

The "6 - 10" is OK, but you need to remove the space from the double quotes.

This function:
1
2
3
4
5
void Property::set_newproperty()
{
    //Local variable
    int countafterpreset = 6;  // <--- You only preset 5 usable elements not 6.
    no = countafterpreset;     // <--- "no" is the wrong variable to be using here. 

Technically you have preset all 10 elements of the array, but only the first 5 have usable information.

As you say "countafterpreset" is a local variable defined when the function and destroyed when the closing } is reached and the function looses scope only to be remade the next time the function is called. This should not be a local variable, but retrieved from the "Details" class. You will have to pass that variable from the "Details::addDetails" function, (by reference), so the proper variable can be updated.

In the "Details::addDetails" function you define the local variable "counter" you even give it the correct value. Add the new information and the last line adds 1 to counter just before the variable is destroyed and the new value is lost.

As I happen to notice in "void Details::addDetails()", that would be the 2nd one, and why the compiler did not find this an error I do not know. Maybe it is saving it for later when the other errors are fixed.

Either I did something wrong in the copy and paste or you changed you code after you posted it. If you changed the code do not do that. It confuses people and makes the thread hard to follow.

There is something to work on for awhile while I get the code to compile and run.

Andy
Pages: 12