struct data type

i am trying to get ahead in the C++ class and i came across with the "struct" instruction and i'm trying to fallow the book example but for some reason after i compiled, the code return with a lot of error. I need to say this book is not what i expected for a beginner but is what will uses in class. Can i get some help please with this code. to be more specific how to use a struct within a struct.

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
// Struct chapter 9 dys 
# include <iostream>
# include <fstream>
# include <iomanip>
# include <string>
using namespace std;
const int NO_OF_SALES_PERSON = 6;
struct nameType
{
	string firstName; 
	string middleName;
	string lastName;
};
struct addressType
{	string address1;
	string address2;
	string city;
	string state;
	string zipcode;
};
struct contactType
{
	
	string cellPhone;
	string homePhone;
	string phone;
	string email;
};

struct employeeType // struct with a struct.
{ 
	nameType name;
	addressType address;
	contactType contact;
		
};

int main ()
{
cout<< "Enter employee name"<<endl; // the code is ok until this point
// How i could store the employee name using struct?
cin>>  nameType name.firstName>>employeeType.name.middleName>>employeeType.name.lastName;
cout<< employeeType nameType.name.lastName<<endl;
cout<< employeeType nameType.name.firstName<<endl;


}
Last edited on
A struct just defines a datatype. Once the struct is defined, you use just as you would any other data type. You need to first declare it, then you can use it.

1
2
3
4
5
6
int main()
{
    employeeType employee;
    employee.name.firstName = "Bob";
    //And so on...
}
Thanks a lot!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct employeeType 
{ 
	nameType name[10];
	addressType address;
	contactType contact;
	double salary; // SalesPerson yearly sales amount
	double saleByQuarter [4]; //store the totalsale by quarter 
};

int main ()
{
   employeeType employee ;  
cout<< "Enter employee name"<<endl;
cin>>   employee.name[0].firstName>> employee.name[0].middleName>> employee.name[0].lastName;
cout<< employee.name[0].lastName<<endl;
cout<< employee.name[0].firstName<<endl;
}
Topic archived. No new replies allowed.