class code is not running

My code wont debug and i asked my professor and she couldnt find the problem

homework question: design a class that holds the following personal data: name, address, age, and phone number. write appropriate accessor and mutator functions. demonstrate the class by writing a program that creates three instances of it. one instance should hold your information, and the other two should your friend's or family member's information

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
  class personalInfo
{
private:
	string name;
	string adress;
	int age;
	int phoneNumber;
public:
	void setName(string);
	void setAdress(string);
	void setAge(int);
	void setNumber(int);
	string getName() const;
	string getAdress() const;
	int getAge() const;
	int getNumber() const;
};

#include<iostream>
#include <string>
#include"hwlab4headerfile.h"

using namespace std;

void personalInfo::setName(string n)
{
	name=n;
}

void personalInfo::setAdress(string adr)
{
	adress=adr;
}

void personalInfo::setAge(int ag)
{
	age=ag;
}

void personalInfo::setNumber(int num)
{
	phoneNumber=num;
}

string personalInfo::getName() const
{
	return name;
}

string personalInfo::getAdress() const
{
	return adress;
}

int personalInfo::getAge() const
{
	return age;
}

int personalInfo::getNumber() const
{
	return phoneNumber;
}


#include<iostream>
#include <string>
#include"hwlab4headerfile.h"

using namespace std;

int main()
{
	personalInfo myInfo; // instance of my information for personalInfo class
	personalInfo friendInfo; // instant of friend's information for personalInfo class
	personalInfo familyInfo; // instance of family's information for personal info class
	string na; // variable for name
	string addr; // variable for address
	int a; // variable for age
	int nu; // variable for phone number


	//get information from user
	cout << "This program will ask you for your information, your friend's information\n";
	cout << "and a family member's information."<< endl;
	cout << "Please enter your name" << endl;
	getline(cin,na);
	cout << "Please enter your address" << endl;
	getline(cin,addr);
	cout << "Please enter your age" << endl;
	cin >> a;
	cout << "Please enter your phone number" << endl;
	cin >> nu;
	cout << "Your information has been saved, now you will enter your friend's information." << endl;

	/*//store information from user into appropriate functions for name, address, age and phone number for their own information
	myInfo.setName(na);
	myInfo.setAdress(addr);
	myInfo.setAge(a);
	myInfo.setNumber(nu);
	cout << "Your information has been saved, now you will enter your friend's information." << endl;
	*/

	cout << "Please enter your friend's name" << endl;
	getline(cin, na);
	cout << "Please enter your friend's address" << endl;
	getline(cin, addr);
	cout << "Please enter your friend's age" << endl;
	cin >> a;
	cout << "Please enter your friend's phone number" << endl;
	cin >> nu;
	cout << "Your information has been saved, now you will enter a family member's information." << endl;

	/*//store information from user into appropriate functions for name, address, age and phone number for friend
	friendInfo.setName(na);
	friendInfo.setAdress(addr);
	friendInfo.setAge(a);
	friendInfo.setNumber(nu);
	cout << "Your information has been saved, now you will enter a family member's information." << endl;
	*/

	cout << "Please enter your family member's name" << endl;
	getline(cin,na);
	cout << "Please enter your family member's address" << endl;
	getline(cin, addr);
	cout << "Please enter your family member's age" << endl;
	cin >> a;
	cout << "Please enter your family member's phone number" << endl;
	cin >> nu;
	cout << "Your family member's information has been saved" << endl;

	//store information from user into appropriate functions for name, address, age and phone number for their own information
	myInfo.setName(na);
	myInfo.setAdress(addr);
	myInfo.setAge(a);
	myInfo.setNumber(nu);
	

	//store information from user into appropriate functions for name, address, age and phone number for friend
	friendInfo.setName(na);
	friendInfo.setAdress(addr);
	friendInfo.setAge(a);
	friendInfo.setNumber(nu);
	

	//store information from user into appropriate functions for name, address, age and phone number for family member
	familyInfo.setName(na);
	familyInfo.setAdress(addr);
	familyInfo.setAge(a);
	familyInfo.setNumber(nu);

	//Show the user their own information
	cout << "Here is your information: " << endl;
	cout << "Name: " << myInfo.getName() << endl;
	cout << "Address: " << myInfo.getAdress() << endl;
	cout << "Age: " << myInfo.getAge() << endl;
	cout << "Phone Number: " << myInfo.getNumber() << endl;

	// show the user their friend's information
	cout << "Here is your information: " << endl;
	cout << "Name: " << friendInfo.getName() << endl;
	cout << "Address: " << friendInfo.getAdress() << endl;
	cout << "Age: " << friendInfo.getAge() << endl;
	cout << "Phone Number: " << friendInfo.getNumber() << endl;

	// show the user their family member's information
	cout << "Here is your information: " << endl;
	cout << "Name: " << familyInfo.getName() << endl;
	cout << "Address: " << familyInfo.getAdress() << endl;
	cout << "Age: " << familyInfo.getAge() << endl;
	cout << "Phone Number: " << familyInfo.getNumber() << endl;

	system("Pause");
	return 0;
}


will post errors on next post
closed account (DEUX92yv)
I'd guess a few of them are at the very top, because at the point you define the class, you haven't included <string> yet. Also, you should put all of your included headers and namespace at the top of a file. It makes an easy reference point. Furthermore, you need not include the same headers multiple times in the same file.

hwlab4headerfile.h(8): error C2146: syntax error : missing ';' before identifier 'name'
hwlab4headerfile.h(8): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
hwlab4headerfile.h(8): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
hwlab4headerfile.h(9): error C2146: syntax error : missing ';' before identifier 'adress'
hwlab4headerfile.h(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
hwlab4headerfile.h(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
hwlab4headerfile.h(13): error C2061: syntax error : identifier 'string'
hwlab4headerfile.h(14): error C2061: syntax error : identifier 'string'
hwlab4headerfile.h(17): error C2146: syntax error : missing ';' before identifier 'getName'
hwlab4headerfile.h(17): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
hwlab4headerfile.h(17): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
hwlab4headerfile.h(17): warning C4183: 'getName': missing return type; assumed to be a member function returning 'int'
hwlab4headerfile.h(18): error C2146: syntax error : missing ';' before identifier 'getAdress'
hwlab4headerfile.h(18): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
hwlab4headerfile.h(18): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
hwlab4headerfile.h(18): warning C4183: 'getAdress': missing return type; assumed to be a member function returning 'int'
main function.cpp(68): error C2660: 'personalInfo::setName' : function does not take 1 arguments
main function.cpp(69): error C2660: 'personalInfo::setAdress' : function does not take 1 arguments
main function.cpp(75): error C2660: 'personalInfo::setName' : function does not take 1 arguments
main function.cpp(76): error C2660: 'personalInfo::setAdress' : function does not take 1 arguments
main function.cpp(82): error C2660: 'personalInfo::setName' : function does not take 1 arguments
main function.cpp(83): error C2660: 'personalInfo::setAdress' : function does not take 1 arguments
hwlab4headerfile.h(8): error C2146: syntax error : missing ';' before identifier 'name'
hwlab4headerfile.h(8): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
hwlab4headerfile.h(8): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
hwlab4headerfile.h(9): error C2146: syntax error : missing ';' before identifier 'adress'
\hwlab4headerfile.h(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
hwlab4headerfile.h(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
hwlab4headerfile.h(13): error C2061: syntax error : identifier 'string'
hwlab4headerfile.h(14): error C2061: syntax error : identifier 'string'
hwlab4headerfile.h(17): error C2146: syntax error : missing ';' before identifier 'getName'
hwlab4headerfile.h(17): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
hwlab4headerfile.h(17): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
hwlab4headerfile.h(17): warning C4183: 'getName': missing return type; assumed to be a member function returning 'int'
hwlab4headerfile.h(18): error C2146: syntax error : missing ';' before identifier 'getAdress'
hwlab4headerfile.h(18): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
hwlab4headerfile.h(18): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
hwlab4headerfile.h(18): warning C4183: 'getAdress': missing return type; assumed to be a member function returning 'int'
hw lab 4.cpp(11): error C2511: 'void personalInfo::setName(std::string)' : overloaded member function not found in 'personalInfo'
hwlab4headerfile.h(6) : see declaration of 'personalInfo'
hw lab 4.cpp(16): error C2511: 'void personalInfo::setAdress(std::string)' : overloaded member function not found in 'personalInfo'
hwlab4headerfile.h(6) : see declaration of 'personalInfo'
hw lab 4.cpp(31): error C2556: 'std::string personalInfo::getName(void) const' : overloaded function differs only by return type from 'int personalInfo::getName(void) const'
hwlab4headerfile.h(17) : see declaration of 'personalInfo::getName'
hw lab 4.cpp(31): error C2371: 'personalInfo::getName' : redefinition; different basic types
hwlab4headerfile.h(17) : see declaration of 'personalInfo::getName'
hw lab 4.cpp(32): error C2065: 'name' : undeclared identifier
hw lab 4.cpp(36): error C2556: 'std::string personalInfo::getAdress(void) const' : overloaded function differs only by return type from 'int personalInfo::getAdress(void) const'
hwlab4headerfile.h(18) : see declaration of 'personalInfo::getAdress'
hw lab 4.cpp(36): error C2371: 'personalInfo::getAdress' : redefinition; different basic types
hwlab4headerfile.h(18) : see declaration of 'personalInfo::getAdress'
hw lab 4.cpp(37): error C2065: 'adress' : undeclared identifier

its three seperate codes the class definition is in a header file and then the function definitions are in another file and then int main is in another file
closed account (DEUX92yv)
From the errors, it seems like all the functions in your class are already defined in hwlab4headerfile.h

Since you've already defined them, try commenting out the line where you include that file, and you should see significantly fewer errors.

Edit: from what you've just said, you should only be including the files, and don't need to define the class or functions at all. That defeats the purpose of including them.
Last edited on
so i got it to work in hwlab4headerfile.h i needed to add include iostream and string . but now in my main function it asks for my information but when it gets to asking for my friends information it skips the code where it asks for their name and goes to their address and same thing for asking for the family member's name it skips that and asks for their address
heres 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
#include<iostream>
#include <string>
#include"hwlab4headerfile.h"

using namespace std;

int main()
{
	personalInfo myInfo; // instance of my information for personalInfo class
	personalInfo friendInfo; // instant of friend's information for personalInfo class
	personalInfo familyInfo; // instance of family's information for personal info class
	string na; // variable for name
	string addr; // variable for address
	int a; // variable for age
	int nu; // variable for phone number


	//get information from user
	cout << "This program will ask you for your information, your friend's information\n";
	cout << "and a family member's information."<< endl;
	cout << "Please enter your name" << endl;
	getline(cin,na);
	cout << "Please enter your address" << endl;
	getline(cin,addr);
	cout << "Please enter your age" << endl;
	cin >> a;
	cout << "Please enter your phone number" << endl;
	cin >> nu;
	cout << "Your information has been saved, now you will enter your friend's information." << endl;

	//store information from user into appropriate functions for name, address, age and phone number for their own information
	myInfo.setName(na);
	myInfo.setAdress(addr);
	myInfo.setAge(a);
	myInfo.setNumber(nu);
	
	cout << "Please enter your friend's name" << endl;
	getline(cin,na);
	cout << "Please enter your friend's address" << endl;
	getline(cin,addr);
	cout << "Please enter your friend's age" << endl;
	cin >> a;
	cout << "Please enter your friend's phone number" << endl;
	cin >> nu;
	cout << "Your information has been saved, now you will enter a family member's information." << endl;

	//store information from user into appropriate functions for name, address, age and phone number for friend
	friendInfo.setName(na);
	friendInfo.setAdress(addr);
	friendInfo.setAge(a);
	friendInfo.setNumber(nu);
	
	

	cout << "Please enter your family member's name" << endl;
	getline(cin,na);
	cout << "Please enter your family member's address" << endl;
	getline(cin, addr);
	cout << "Please enter your family member's age" << endl;
	cin >> a;
	cout << "Please enter your family member's phone number" << endl;
	cin >> nu;
	cout << "Your family member's information has been saved" << endl;

	//store information from user into appropriate functions for name, address, age and phone number for family member
	familyInfo.setName(na);
	familyInfo.setAdress(addr);
	familyInfo.setAge(a);
	familyInfo.setNumber(nu);

	//Show the user their own information
	cout << "Here is your information: " << endl;
	cout << "Name: " << myInfo.getName() << endl;
	cout << "Address: " << myInfo.getAdress() << endl;
	cout << "Age: " << myInfo.getAge() << endl;
	cout << "Phone Number: " << myInfo.getNumber() << endl;

	// show the user their friend's information
	cout << "Here is your information: " << endl;
	cout << "Name: " << friendInfo.getName() << endl;
	cout << "Address: " << friendInfo.getAdress() << endl;
	cout << "Age: " << friendInfo.getAge() << endl;
	cout << "Phone Number: " << friendInfo.getNumber() << endl;

	// show the user their family member's information
	cout << "Here is your information: " << endl;
	cout << "Name: " << familyInfo.getName() << endl;
	cout << "Address: " << familyInfo.getAdress() << endl;
	cout << "Age: " << familyInfo.getAge() << endl;
	cout << "Phone Number: " << familyInfo.getNumber() << endl;

	system("Pause");
	return 0;
}
closed account (DEUX92yv)
It skips that code because you use getline(), which returns the text of the line, but leaves the newline character '\n' in the cin stream. To fix this, each time after you use getline(cin, someVariable), place this line: cin >> ws;. "ws" stands for whitespace, and since a newline character counts as whitespace, the stream will place the newline into that whitespace placeholder, and should continue working as normal.

Edit: I forgot to mention a reminder: don't place the whitespace line after lines where you use cin >> someVariable;. Only after those in which you use getline(). It's not necessary after every line.

Edit 2: depending on if you expect incorrect user input, you may also want to include cin.clear(); cin.ignore(200 /* is usually enough characters to ignore */, '\n'); before the getline.
Last edited on
Ive made the changes but still it works but then skips the rest of the questions like it will go to my friends information ask me for their address then skip everything else completely

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
#include<iostream>
#include <string>
#include"hwlab4headerfile.h"

using namespace std;

int main()
{
	personalInfo myInfo; // instance of my information for personalInfo class
	personalInfo friendInfo; // instant of friend's information for personalInfo class
	personalInfo familyInfo; // instance of family's information for personal info class
	string na; // variable for name
	string addr; // variable for address
	int a; // variable for age
	int nu; // variable for phone number


	//get information from user
	cout << "This program will ask you for your information, your friend's information\n";
	cout << "and a family member's information."<< endl;
	cout << "Please enter your name" << endl;
	getline(cin,na);
	cout << "Please enter your address" << endl;
	getline(cin,addr);
	cout << "Please enter your age" << endl;
	cin >> a;
	cout << "Please enter your phone number" << endl;
	cin >> nu;
	cout << "Your information has been saved, now you will enter your friend's information." << endl;
	
	cout << "Please enter your friend's name" << endl;
	getline(cin,na);
	cin >> ws;
	cout << "Please enter your friend's address" << endl;
	getline(cin,addr);
	cin >> ws;
	cout << "Please enter your friend's age" << endl;
	cin >> a;
	cout << "Please enter your friend's phone number" << endl;
	cin >> nu;
	cout << "Your information has been saved, now you will enter a family member's information." << endl;

	cout << "Please enter your family member's name" << endl;
	getline(cin,na);
	cin >> ws;
	cout << "Please enter your family member's address" << endl;
	getline(cin, addr);
	cin >> ws;
	cout << "Please enter your family member's age" << endl;
	cin >> a;
	cout << "Please enter your family member's phone number" << endl;
	cin >> nu;
	cout << "Your family member's information has been saved" << endl;

	//store information from user into appropriate functions for name, address, age and phone number for their own information
	myInfo.setName(na);
	myInfo.setAdress(addr);
	myInfo.setAge(a);
	myInfo.setNumber(nu);

	//store information from user into appropriate functions for name, address, age and phone number for friend
	friendInfo.setName(na);
	friendInfo.setAdress(addr);
	friendInfo.setAge(a);
	friendInfo.setNumber(nu);
	

	//store information from user into appropriate functions for name, address, age and phone number for family member
	familyInfo.setName(na);
	familyInfo.setAdress(addr);
	familyInfo.setAge(a);
	familyInfo.setNumber(nu);

	//Show the user their own information
	cout << "Here is your information: " << endl;
	cout << "Name: " << myInfo.getName() << endl;
	cout << "Address: " << myInfo.getAdress() << endl;
	cout << "Age: " << myInfo.getAge() << endl;
	cout << "Phone Number: " << myInfo.getNumber() << endl;

	// show the user their friend's information
	cout << "Here is your information: " << endl;
	cout << "Name: " << friendInfo.getName() << endl;
	cout << "Address: " << friendInfo.getAdress() << endl;
	cout << "Age: " << friendInfo.getAge() << endl;
	cout << "Phone Number: " << friendInfo.getNumber() << endl;

	// show the user their family member's information
	cout << "Here is your information: " << endl;
	cout << "Name: " << familyInfo.getName() << endl;
	cout << "Address: " << familyInfo.getAdress() << endl;
	cout << "Age: " << familyInfo.getAge() << endl;
	cout << "Phone Number: " << familyInfo.getNumber() << endl;

	system("Pause");
	return 0;
}
Topic archived. No new replies allowed.