Sorting? Or Searching

Pages: 1... 4567
Urm basically its only letting me input for lastname and not first and same again with postcode . I know it doesn't make sense but its skipping some details
so it skips first name and lets me input in 2nd name
We're working with this code, right?
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
void SaveData(Details Data[],int size){
ofstream DataSave("test.txt");
	for(int i=0;i<size;i++){
	DataSave <<"\n";
	cout << "First Name: ";
    getline(cin, Data[i].FirstName);
	DataSave <<  Data[i].FirstName<<"\t";

    cout << "Last Name: ";
    getline(cin,  Data[i].LastName);
	DataSave << Data[i].LastName<<"\t";

	cout << "Age(Please Enter an Integer(Number)): ";
	DataSave <<  Data[i].Age<<"\t";

	cout << "Email: ";
	getline(cin,  Data[i].Email);
	DataSave <<  Data[i].Email<<"\t";

    cout << "Door Number(Please Enter an Integer(Number)): ";
    cin >>  Data[i].DoorNumber;
	DataSave <<  Data[i].DoorNumber<<"\t";

    cout << "Road Name: ";
    getline(cin,  Data[i].RoadName);
	DataSave <<  Data[i].RoadName<<"\t";

    cout << "Post Code(NOTE:Please use Lower Case): ";
	getline(cin,  Data[i].PostCode);
	DataSave <<  Data[i].PostCode<<"\t";
	DataSave <<"\n";
	}
	DataSave.close();

}

I see an error. You forgot to read in the age from the user. This would throw off whats being entered for what, which could easily cause cin to fail.
Data entry must be correct.
fixed it with this
out << "Age(Please Enter an Integer(Number)): ";
cin >> Data[i].Age;
DataSave << Data[i].Age<<"\t";

still not working its skipping first name
and its definetly a get line problem


it skips email and goes to door number

then skips road name and does the postcode
Yeah, I think you are encountering the nasty problems that occur when you mix cin >> and getline(cin) calls.
It has something to do with a newline getting stuck in the stream.

My file I/O skills are limited because I don't do it much.

You can try stuff like adding this after any line where you use cin >> ( your newly added line and line 21 above). cin.ignore(256,'\n'); which may remove the stray newline (if that's what's causing the problem).

Or, try using getline for everything.

Someone else may have to help you with this part.
Last edited on
Thanks for all your help. Also do you have any idea why Data[i]!=NULL doesn't work?
You're welcome for the help. Did that cin.ignore(256,'\n'); fix anything?
Also do you have any idea why Data[i]!=NULL doesn't work?

Data[i] is not a pointer, so that comparison is illegal.

EDIT: On second thought, Data[i] is a Details object, so Data[i] != not-a-Details object will always be illegal.
Last edited on
and no it didn't I might go back to just using cin without get line

so if that comparison to null doesn't work how do I check if array is full if so to skip :S
and no it didn't I might go back to just using cin without get line

OK, but that will land you back onto problems with blank spaces in strings.

so if that comparison to null doesn't work how do I check if array is full if so to skip :S

Not sure what you mean by "array is full". The code above is done when i==size in the for loop.
I mean to check if details have already been entered into data[I]
Just check Data[i] for valid data. Your default constructor for a Details object assigns age = 0, if I recall.
I was going to suggest you make that -1 instead. If Data[i].age == -1 then details have not been entered.
yes it is age=0; and I only need to check age right not the others such as name strings?
im about to go crazy now its started to wipe the previous data in the file again...
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
for(int j=0;j<x;j++){
				if ( Data[i].Age == 0){
				cout << "First Name: ";
				getline(cin, Data[i].FirstName);
				DataSave <<  Data[i].FirstName<<"\t";	
				cin.ignore(256,'\n'); 
				cout << "Last Name: ";
				getline(cin,Data[i].LastName);
				DataSave << Data[i].LastName<<"\t";
		
				cout << "Age(Please Enter an Integer(Number)): ";
				cin >>  Data[i].Age;
				DataSave <<  Data[i].Age<<"\t";
				cin.ignore(256,'\n'); 
				cout << "Email: ";
				getline(cin,  Data[i].Email);
				DataSave <<  Data[i].Email<<"\t";
			
				cout << "Door Number(Please Enter an Integer(Number)): ";
				cin >>  Data[i].DoorNumber;
				DataSave <<  Data[i].DoorNumber<<"\t";
				cin.ignore(256,'\n'); 
				cout << "Road Name: ";
				getline(cin,Data[i].RoadName);
				DataSave <<  Data[i].RoadName<<"\t";
			
				cout << "Post Code(NOTE:Please use Lower Case): ";
				getline(cin,Data[i].PostCode);
				DataSave <<  Data[i].PostCode<<"\t";
				DataSave <<"\n";
				
			}
			else{
				i++;
				x--;
				cout << "FULL";
			}
		}
	
	DataSave.close();

}


every time I re run the program it deletes old and puts in new
Last edited on
every time I re run the program it deletes old and puts in new

Open the file with a mode of ios::app, in order to append the new data at the end of the existing file.
ofstream DataSave("test.txt", ios::out | ios::app);

That means the file will grow larger each time. I've lost track of the other parts of the code, if you are reading from the file into your array, then the size of the array may need to be increased. I did mention a vector at one point, and that may be useful, depending on the overall design and what it is you are trying to achieve.

I don't know whether this is a student project, where you must adhere to a strict specification, or your own where you can choose the requirements yourself.
Last edited on
its a student project we have to use arrays

also I don't understand this bit of code

ofstream DataSave("test.txt", ios::out | ios::app);
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
	#include <iostream> 
	#include <string>
	#include <math.h> 
	#include <fstream>
	#include <sstream>
	using namespace std;

class Details
{
public:
    Details();
	string FirstName;
    string LastName;
    int Age ;
    string Email;
    int DoorNumber;
    string RoadName;
    string PostCode;
	void SearchByAge(int);
	
};

	 Details::Details() : Age(0), DoorNumber(0) {}
//void DataEntry(Details &member){
//	ofstream DataSave("test.txt");
//	
//	cout << "First Name: ";
//    cin >> member.FirstName;
//	DataSave << member.FirstName<<"\t";
//
//    cout << "Last Name: ";
//    cin >> member.LastName;
//	DataSave << member.LastName<<"\t";
//
//	cout << "Age(Please Enter an Integer(Number)): ";
//	cin >> member.Age;
//	DataSave << member.Age<<"\t";
//
//	cout << "Email: ";
//	cin >> member.Email;
//	DataSave << member.Email<<"\t";
//
//    cout << "Door Number(Please Enter an Integer(Number)): ";
//    cin >> member.DoorNumber;
//	DataSave << member.DoorNumber<<"\t";
//
//    cout << "Road Name: ";
//    cin >> member.RoadName;
//	DataSave << member.RoadName<<"\t";
//
//    cout << "Post Code(NOTE:Please use Lower Case): ";
//	cin >> member.PostCode;
//	DataSave << member.PostCode<<"\t";
//	DataSave <<"\n";
//	
//	
//
//}
void GetData(Details &member){
	cout << "\nFirst Name: "<< member.FirstName;
	cout << "\nLast Name: " << member.LastName;
	cout << "\nAge: " << member.Age;
	cout << "\nEmail: " << member.Email;
	cout << "\nDoor Number: " <<  member.DoorNumber;
	cout << "\nRoad Name: " << member.RoadName;
	cout << "\nPost Code: " <<  member.PostCode<<"\n";
}
void Age_Search(Details info[], int size){
    int AgeSearch = 0;
	cin >> AgeSearch;
	for(int i=0; i < size; i++){
		if( info[i].Age == AgeSearch ){
		cout << info[i].FirstName << " "  << info[i].LastName << " is" << info[i].Age << " years old.\nHere are his/her Details:";
		GetData(info[i]);
		}
	}
}
void LastName_Search(Details info[],int size){
	string LastNameSearch;
	cin >> LastNameSearch;
	for(int i=0; i < size; i++){
		if( info[i].LastName == LastNameSearch ){
		cout << "Here are Details of People who have the Surname "<< LastNameSearch<<":";
		GetData(info[i]);
		}
	}

}
void PostCode_Search(Details info[],int size){
	string PostCodeSearch;
	cin >> PostCodeSearch;
	for(int i=0; i < size; ++i){
		if( info[i].PostCode == PostCodeSearch ){
		cout << "Here are Details of People Who Live in the '"<< PostCodeSearch<<"' Postal Area:";
		GetData(info[i]);
		}
	}
}
void Email_Search(Details info[],int size){
	string EmailSearch;
	cin >> EmailSearch;
	for(int i=0; i < 10; ++i){
		if( info[i].Email == EmailSearch ){
		cout << "This Person Has the email address '"<< EmailSearch<<"':";
		GetData(info[i]);
		}
	}
}
// define operator >> for a Details object

   istream& operator>>( istream& is, Details& detail ){
    string line;
    string word;
    char delim = '\t';
    getline(is, line);

    istringstream iss(line);

    getline(iss, word, delim);
    detail.FirstName = word;

    getline(iss, word, delim);
    detail.LastName = word;

    getline(iss, word, delim);
    detail.Age = atoi(word.c_str());          // convert string to integer

    getline(iss, word, delim);
    detail.Email = word;

    getline(iss, word, delim);
    detail.DoorNumber = atoi(word.c_str());   // convert string to integer

    getline(iss, word, delim);
    detail.RoadName = word;

    getline(iss, word, delim);
    detail.PostCode = word;

    return is;
}
void LoadData(Details Data[], int size){
    ifstream File_in("test.txt");// attempt to open file for reading
    if( File_in.is_open() )
    {
        for(int i=0; i<size;i++)
            File_in >> Data[i];
	        File_in.close();// details!
    }
    return;
}
	//void SaveData(Details Data[], int size){
//	ofstream DataSave("test.txt");
 //   if( Datasave.is_open() )
  //  {
   //     for(int i=0; i<size;i++){
    //       DataSave << Data[i];
	//	}
	 // DataSave.close();      // details!
  //  }

  //  return;
//}






    




void SaveData(Details Data[],int size){
	ofstream DataSave("test.txt");
	int x;
	int i;
	i=0;
		
	cout << "How Many Entries Would You Like to add";
	cin >> x;

	
		
	for(int j=0;j<x;j++){
				if ( Data[i].Age == 0){
				cout << "First Name: ";
				getline(cin, Data[i].FirstName);
				DataSave <<  Data[i].FirstName<<"\t";	
				cin.ignore(256,'\n'); 
				cout << "Last Name: ";
				getline(cin,Data[i].LastName);
				DataSave << Data[i].LastName<<"\t";
		
				cout << "Age(Please Enter an Integer(Number)): ";
				cin >>  Data[i].Age;
				DataSave <<  Data[i].Age<<"\t";
				cin.ignore(256,'\n'); 
				cout << "Email: ";
				getline(cin,  Data[i].Email);
				DataSave <<  Data[i].Email<<"\t";
			
				cout << "Door Number(Please Enter an Integer(Number)): ";
				cin >>  Data[i].DoorNumber;
				DataSave <<  Data[i].DoorNumber<<"\t";
				cin.ignore(256,'\n'); 
				cout << "Road Name: ";
				getline(cin,Data[i].RoadName);
				DataSave <<  Data[i].RoadName<<"\t";
			
				cout << "Post Code(NOTE:Please use Lower Case): ";
				getline(cin,Data[i].PostCode);
				DataSave <<  Data[i].PostCode<<"\t";
				DataSave <<"\n";
				
			}
			else{
				i++;
				j--;
				cout << "FULL";
			}
		}
	
	DataSave.close();

}
int main(){	
	
	int a=0;
	int b=0;
	int i = 0;
	string temp;
	Details	Data[10];
	LoadData(Data,10);
	
	
	do{	
		cout << "\n1) Input \n2) View \n3) Search\n4) Delete\nExit \n" ;
		cin >> a;
		switch(a){
			case 1:	
				//Input;
			SaveData(Data,10);
			break;
			case 2: 
				//View;
				cout << "Would You like to view the data?";
				cin >> i;
				GetData(Data[i]);	
			break;
			case 3: 
				//Search
				system("CLS");
				cout << "\n1) Input \n2) View";
				cout << "\n3Search\n   3.1)Search Using Age\n   3.2)Search Using Last Name\n   3.3)Search Using Post Code\n   3.4)Search Using Email";
				cout <<"\n4) Delete\nExit \n" ;
				cin >> b;
				switch(b){
				case 1:
					//Search With Age
					cout << "Enter the age of the person you are looking for: ";Age_Search(Data,10);break;
				case 2:
					//Search Using Last Name
					cout << "Enter the Last Name or Surname of the person you are looking for: ";LastName_Search(Data,10);break;
				case 3:
					//Search Using Post Code
					cout << "Enter the Post Code of the person/people you are searching for: ";PostCode_Search(Data,10);break;
				case 4:
					//Search Using Email Adress
					cout << "Enter the Email Address of the person you are searching for: ";Email_Search(Data,10);break;default:break;
				system("pause");					
				}//end of switch b(search)
			break;
			case 4: 
				//Delete
			break;
			case 5:
				//Exit
			break;default: break;       
		   }//end of switch a(main menu)
	}//end of do while loop if exit is chosen.
	while(a!=5);


return 0;}


and this is what I have atm
Thanks for sharing your code. I haven't had time to look in detail, but I think it will be useful to all of us who try to help.

Opening a file using different mode flags such as append is described in the tutorial here: http://www.cplusplus.com/doc/tutorial/files/
Also thanks for all the help so far from everyone in this topic. Don't know where id be without the help. I suppose it would be much more basic than it is now and I would've given up trying to add some features. Still have a long way to go but thanks. :D Really Appreciated.
Pages: 1... 4567