Using structs

I am creating a simple address book program. I am having trouble properly implementing the header file. I know I need to change line 8 to cin>>Person.firstname. The problem I am having is comparing what the user enters to what is stored in the file. Any suggestions on how I could fix my code would be helpful.

Header File
1
2
3
4
5
6
7
8
9
10

  struct Person
{
    string firstname;
    string lastname;
    string phone;
    string address;
};



Program

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
  void searchFirst(ifstream& inData)
{
string searchName, firstName, lastName, phone, address,
upperSearch, upperFirstName;

cout << "Enter the first name of the person: ";
cin >> searchName;
cout << endl;

upperSearch = uppercaseString(searchName);

while (readFile(inData, firstName, lastName, phone, address))
{
upperFirstName = uppercaseString(firstName);

if (upperFirstName == upperSearch)
break;
}

if (inData)
{
cout << "Person found: " << endl;
printInfo(firstName, lastName, phone, address);
}
else
{
cout << searchName << " not found!" << endl << endl;
}

inData.clear();
inData.seekg(0);
}

bool readFile(ifstream& inData, string& firstName, string& lastName, string& phone, string& address)
{
getline(inData, firstName, ',');
extraSpaces(firstName);

getline(inData, lastName, ',');
extraSpaces(lastName);

getline(inData, phone, '\n');
extraSpaces(phone);

getline(inData, address, '\n');
extraSpaces(address);

return inData;
}

in order to use struct element you need to need to create the object of your struct name

Person p
to access use p.firstname;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct data
{
	string name;
	string last_name;
};

void person()
{
	data d;
	cout<<"\nEnter The First Name :"
	cin>>d.name;
	cout<<"\nEnter The Second Name : ";
	cin>>d.last_name;
}

For Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct data
{
	string name;
	string last_name;
	// if you make function in your struct no need to use
	// object but you need to use in main()
	void person();
};

void data::person()// :: scope resolution operator
{
	cout<<"\nEnter The First Name :"
	cin>>name;
	cout<<"\nEnter The Second Name : ";
	cin>>last_name;
}
Topic archived. No new replies allowed.