dating service file problem

The program i am currently writing isn't properly reading information from the file. I can add information to the file, but it doesn't display any.
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
#include <iostream>
#include <cstring>
#include <fstream>
#include <string>
using namespace std;
struct client
{
	char name [20];
	char lastname [20];
	char address [50];
	char gender [5];
	int age;
	char firstInt [20];
	char secondInt [20];
};
int main ()
{
	string searchIme,searchFI,searchSI;
	int izbor,searchAge,searchGender;
	fstream clientFile;
	client client;
	cout<<"Kakvo jelate da napravite?\n1 dobavqne na nov klient\n2 promqna na adres\n3 informaciq za klient\n4 podhodqshti klient\n5 izhod\nIzbor: ";
	cin>>izbor;
	switch (izbor)
	{
		case 1:
			clientFile.open("clientFile.txt", ios::app);
			cout<<"Ime: ";
			cin>>client.name;
			cout<<"Familiq: ";
			cin>>client.lastname;
			cout<<"Naseleno mqsto: ";
			cin>>client.address;
			cout<<"Pol: ";
			cin>>client.gender;
			cout<<"Vyzrast: ";
			cin>>client.age;
			cout<<"Interesi (vyvedete dva): ";
			cin>>client.firstInt; 
			cin>>client.secondInt;
			clientFile<<client.name<<" ";
			clientFile<<client.lastname<<" ";
			clientFile<<client.address<<" ";
			clientFile<<client.gender<<" ";
			clientFile<<client.age<<" ";
			clientFile<<client.firstInt<<" ";
			clientFile<<client.secondInt<<"\n";
			clientFile.close();
			
		case 3:
			clientFile.open("clientFile.txt", ios::app);
			cout<<"Za koi klient jelaete informaciq?";
			cin>>searchIme;
			clientFile>>client.name;
			clientFile>>client.lastname;
			clientFile>>client.address;
			clientFile>>client.gender;
			clientFile>>client.age;
			clientFile>>client.firstInt;
			clientFile>>client.secondInt;
			if (searchIme==client.name)
			{
				cout<<"Ime: "<<client.name<<endl;
				cout<<"Familiq: "<<client.lastname<<endl;
				cout<<"Naseleno mqsto: "<<client.address<<endl;
				cout<<"Pol: "<<client.gender<<endl;
				cout<<"Vyzrast: "<<client.age<<endl;
				cout<<"Interesi: "<<client.firstInt<<", "<<client.secondInt<<endl;
				clientFile.close();
			}
	}
		return 0;
}
Did it open properly for reading? Since you use ios::app. Use ios::in instead.
Tried it, didn't work. Any other ways to search for a name in a file and then print the information for that name?
First of all you should always check whether the file could be opened [and tell the user if not].

Second: This way you read only the first record. You need a loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
while(clientFile>>client.name>>client.lastname>>client.address>>...)
{
			if (searchIme==client.name)
			{
				cout<<"Ime: "<<client.name<<endl;
				cout<<"Familiq: "<<client.lastname<<endl;
				cout<<"Naseleno mqsto: "<<client.address<<endl;
				cout<<"Pol: "<<client.gender<<endl;
				cout<<"Vyzrast: "<<client.age<<endl;
				cout<<"Interesi: "<<client.firstInt<<", "<<client.secondInt<<endl;
break;
			}
}
				clientFile.close();
Are you sure you used ios::in instead of ios::app in line 51?
When you open the file for reading, are you sure the file exists?
After open, use is_open to check if you really opened the file.
Otherwise, post the code that doesn't work and tell us what line doesn't work.
However, note that your code won't work for strings containing spaces.
Furthemore, I suggest you to use std::string instead of char arrays.
Yeah, thank you so much. The loop worked.
Topic archived. No new replies allowed.