Beginner C++ Question

How would I ask the user to input user name instead of setting it to Dave?

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
#include <iostream>
#include <string>
using namespace std;

struct date
{
int day;
int month;
int year;
};
struct address
{
string house_street;
string city;
string state;
string zip_code;
};
class StudentDetails
{
string name;
int id;
struct date dob;
struct address address;
int totalCreditEarned;
double GPA;
public:
StudentDetails(string n, int i = -1)
{
name = n;
id = i;
}

void setName(string n);
void setId(int i);
void setDate(int d, int m, int y);
void setAddress(string h, string c, string s, string z);
void setTotalCreditEarned(int t);
void setGPA(double g);
string getName();
int getId();
struct date getDate();
struct address getAddress();
int getTotalCreditEarned();
double getGPA();
};

void StudentDetails::setName(string n)
{
name = n;
}
void StudentDetails::setId(int i)
{
id = i;
}
void StudentDetails::setDate(int d, int m, int y)
{
dob.day = d;
dob.month = m;
dob.year = y;
}
void StudentDetails::setAddress(string h, string c, string s, string z)
{
address.house_street = h;
address.city = c;
address.state = s;
address.zip_code = z;
}
void StudentDetails::setTotalCreditEarned(int t)
{

totalCreditEarned = t;
}
void StudentDetails::setGPA(double g)
{
GPA = g;
}
string StudentDetails::getName()
{
return name;
}
int StudentDetails::getId()
{
return id;
}
struct date StudentDetails::getDate()
{
return dob;
}
struct address StudentDetails::getAddress()
{
return address;
}
int StudentDetails::getTotalCreditEarned()
{
return totalCreditEarned;
}
double StudentDetails::getGPA()
{
return GPA;
}
int main()
{
StudentDetails student("Dave");
string h, c, s, z;
int d, m, y;
int id;
int credit;
double gpa;
char temp;
cout << "Enter the Student ID: "<<endl;
cin >> id;
cout << "*****Enter the Birthday Details:*****"<<endl;
cout << "Day: ";
cin >> d;
cout << "Month: ";
cin >> m;
cout << "Year: ";
cin >> y;
getchar();
cout << "******Enter the Address Detials:*****"<<endl;
cout << "House # & Street: "<<endl;
getline(cin, h); 
cout << "City: "<<endl;
getline(cin, c); 
cout << "State: "<<endl;
getline(cin, s); 
cout << "Zip Code: "<<endl;
getline(cin, z);
cout << "Enter the Total Credits Earned: "<<endl;
cin >> credit;
cout << "Enter the GPA: "<<endl;
cin >> gpa;
student.setId(id);
student.setDate(d, m, y);
student.setAddress(h, c, s, z);
student.setTotalCreditEarned(credit);
student.setGPA(gpa);
cout <<"**********VALIDATION**********:"<<endl<<endl;
if (student.getAddress().zip_code == "11235") 
cout <<"Yes, The student " << student.getName() << "'s lives in the zip code is 11235"<<endl;
else
cout <<"No, The student " << student.getName() << "'s lives in the zip code is not 11235"<<endl;
if (student.getDate().month == 11)
cout << "Yes, The student " << student.getName() << "'s birthday is in November"<<endl;
else
cout << "No, The student " << student.getName() << "'s birthday is not in November"<<endl;
}
Hello tetsuuuu,
Your code is very hard to read.
Proper indenting and some blank lines would help.
In "main" ask for the name before you create and object of the class.
Then you can use the variable in place of ("Dave").
You need some better variable names. string h, c, s, z;. I have no idea
what these are or what they mean. Six month from now you will have no idea either.

Andy
Sorry about that, the formatting messed up while copy + pasting somehow.

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
#include <iostream>
#include <string>
using namespace std;

struct date
{
	int day;
	int month;
	int year;
};
struct address
{
	string house_street;
	string city;
	string state;
	string zip_code;
};
class StudentDetails
{
	string name;
	int id;
	struct date dob;
	struct address address;
	int totalCreditEarned;
	double GPA;
public:
	StudentDetails(string n, int i = -1)
	{
		name = n;
		id = i;
	}

	void setName(string n);
	void setId(int i);
	void setDate(int d, int m, int y);
	void setAddress(string h, string c, string s, string z);
	void setTotalCreditEarned(int t);
	void setGPA(double g);
	string getName();
	int getId();
	struct date getDate();
	struct address getAddress();
	int getTotalCreditEarned();
	double getGPA();
};

void StudentDetails::setName(string n)
{
	name = n;
}
void StudentDetails::setId(int i)
{
	id = i;
}
void StudentDetails::setDate(int d, int m, int y)
{
	dob.day = d;
	dob.month = m;
	dob.year = y;
}
void StudentDetails::setAddress(string h, string c, string s, string z)
{
	address.house_street = h;
	address.city = c;
	address.state = s;
	address.zip_code = z;
}
void StudentDetails::setTotalCreditEarned(int t)
{
	totalCreditEarned = t;
}
void StudentDetails::setGPA(double g)
{
	GPA = g;
}
string StudentDetails::getName()
{
	return name;
}
int StudentDetails::getId()
{
	return id;
}
struct date StudentDetails::getDate()
{
	return dob;
}
struct address StudentDetails::getAddress()
{
	return address;
}
int StudentDetails::getTotalCreditEarned()
{
	return totalCreditEarned;
}
double StudentDetails::getGPA()
{
	return GPA;
}
int main()
{
	StudentDetails student("Dave");
	string h, c, s, z;
	int d, m, y;
	int id;
	int credit;
	double gpa;
	char temp;
	
	cout << "Enter the Student ID: "<<endl;
	cin >> id;
	cout << "*****Enter the Birthday Details:*****"<<endl;
	cout << "Day: ";
	cin >> d;
	cout << "Month: ";
	cin >> m;
	cout << "Year: ";
	cin >> y;
	getchar();
	cout << "******Enter the Address Detials:*****"<<endl;
	cout << "House # & Street: "<<endl;
	getline(cin, h); 
	cout << "City: "<<endl;
	getline(cin, c); 
	cout << "State: "<<endl;
	getline(cin, s); 
	cout << "Zip Code: "<<endl;
	getline(cin, z);
	cout << "Enter the Total Credits Earned: "<<endl;
	cin >> credit;
	cout << "Enter the GPA: "<<endl;
	cin >> gpa;
	student.setId(id);
	student.setDate(d, m, y);
	student.setAddress(h, c, s, z);
	student.setTotalCreditEarned(credit);
	student.setGPA(gpa);
	cout <<"**********VALIDATION**********:"<<endl<<endl;
	if (student.getAddress().zip_code == "11235") 
		cout <<"Yes, The student " << student.getName() << "'s lives in the zip code is 11235"<<endl;
	else
		cout <<"No, The student " << student.getName() << "'s lives in the zip code is not 11235"<<endl;
	if (student.getDate().month == 11)
		cout << "Yes, The student " << student.getName() << "'s birthday is in November"<<endl;
	else
		cout << "No, The student " << student.getName() << "'s birthday is not in November"<<endl;
}


Instead of setting the name to Dave I want the user to input a name instead. But I am confused about what to do.
I got it, thanks for the hint.
Hello tetsuuuu,

Just to give you an idea of how blank lines and a good variable name can make a difference:
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
int main()
{
    std::string name;

    cout << "Enter the Student name: ";
    std::getline(cin, name);

    StudentDetails student(name);
    string address, city, state, zip;
    int day, month, year;
    int id;
    int credit;
    double gpa;
    char temp;  // <--- Does not appear to be used.

    cout << "Enter the Student ID: ";
    cin >> id;

    cout << "*****Enter the Birthday Details:*****" << '\n';

    cout << "Day: ";
    cin >> day;

    cout << "Month: ";
    cin >> month;

    cout << "Year: ";
    cin >> year;

    getchar();

    cout << "******Enter the Address Detials:*****" << '\n';

    cout << "House # & Street: ";
    getline(cin, address);

    cout << "City: ";
    getline(cin, city);

    cout << "State: ";
    getline(cin, state);

    cout << "Zip Code: ";
    getline(cin, zip);

    cout << "Enter the Total Credits Earned: ";
    cin >> credit;

    cout << "Enter the GPA: ";
    cin >> gpa;

    student.setId(id);
    student.setDate(day, month, year);
    student.setAddress(address, city, state, zip);
    student.setTotalCreditEarned(credit);
    student.setGPA(gpa);

    cout << "**********VALIDATION**********:" << '\n' << '\n';

    if (student.getAddress().zip_code == "11235")
        cout << "Yes, The student " << student.getName() << "'state lives in the zip code is 11235" << '\n';
    else
        cout << "No, The student " << student.getName() << "'state lives in the zip code is not 11235" << '\n';

    if (student.getDate().month == 11)
        cout << "Yes, The student " << student.getName() << "'state birthday is in November" << '\n';
    else
        cout << "No, The student " << student.getName() << "'state birthday is not in November" << '\n';

    return 0;  // <--- Not required, but makes a good break point.
}


Andy
@tetsuuuu

It's usually preferable to pass string by ref so that a copy isn't made (const ref so that the contents can't be changed).

In C++, you don't need to specify struct when passing/returning a structure.

If a method doesn't change the class data (or changed data is mutable), then the method should be defined as const so that the method will work with a const class instance.

Rather than have all the setters, an alternative is :

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
#include <iostream>
#include <string>
using namespace std;

struct Date
{
	int day {};
	int month {};
	int year {};

	Date() {
		cout << "Day: ";
		cin >> day;

		cout << "Month: ";
		cin >> month;

		cout << "Year: ";
		cin >> year;

		cin.ignore(1000, '\n');
	}
};

struct Address
{
	string house_street;
	string city;
	string state;
	string zip_code;

	Address() {
		cout << "House # & Street: ";
		getline(cin, house_street);

		cout << "City: ";
		getline(cin, city);

		cout << "State: ";
		getline(cin, state);

		cout << "Zip Code: ";
		getline(cin, zip_code);
	}
};

class StudentDetails
{
	string name;
	int id {};
	Date dob;
	Address address;
	int totalCreditEarned {};
	double GPA {};

public:
	StudentDetails(const string& n, int i, const Date& d, const Address& a, int ce, double g) : name(n), id(i), dob(d), address(a), totalCreditEarned(ce), GPA(g) {}

	string getName() const { return name; }
	int getId() const { return id; }
	Date getDate() const { return dob; }
	Address getAddress() const { return address; }
	int getTotalCreditEarned() const { return totalCreditEarned; }
	double getGPA() const { return GPA; }
};

int main()
{
	string name;
	int id;
	int credit;
	double gpa;

	cout << "Enter the Student name: ";
	getline(cin, name);

	cout << "Enter the Student ID: ";
	cin >> id;

	cout << "*****Enter the Birthday Details:*****" << '\n';
	Date dob;

	cout << "******Enter the Address Details:*****" << '\n';
	Address address;

	cout << "Enter the Total Credits Earned: ";
	cin >> credit;

	cout << "Enter the GPA: ";
	cin >> gpa;

	const StudentDetails student(name, id, dob, address, credit, gpa);

	cout << "**********VALIDATION**********:" << '\n' << '\n';

	if (student.getAddress().zip_code == "11235")
		cout << "Yes, The student " << student.getName() << "'state lives in the zip code is 11235" << '\n';
	else
		cout << "No, The student " << student.getName() << "'state lives in the zip code is not 11235" << '\n';

	if (student.getDate().month == 11)
		cout << "Yes, The student " << student.getName() << "'state birthday is in November" << '\n';
	else
		cout << "No, The student " << student.getName() << "'state birthday is not in November" << '\n';
}

Last edited on
Topic archived. No new replies allowed.