Homework help:Classes

I think I am near finishing this assignment, but am having problems in the default constructor saying "the expression must be a modifiable lvalue" for line 21. I am pretty new to C++ obviously and would appreciate any tips to point me in the right direction for this and any other blaring mistakes. I will include the directions for the assignment as well.

Create a class named Student. The class should consist of the following private data members : social security number and name (last, first or first, last?). The social security number (SSN) should be a long integer. The name variable must be a character array of 80 characters.
Create the following class member functions: setSSN, getSSN, setName, getName. Each of these member functions should be public.
The setSSN function should accept 1 argument and update the the social security number member variable. Do not allow the the social security number to be set to zero or less than zero. The getSSN should return the class SSN.

The setName member function should accept one string argument. Use the argument to update the name class member variable. Do not update the class variable for name if the argument has a length of 0. (This indicates the name in the argument is "empty".) The getName method should return the class value for name.
Create a default constructor for the Student class. This constructor will accept no arguments. Use the default constructor to initialize the social security number to 999999999 and the name to "unassigned".
Make sure all your methods are defined in the implementation section of the class. Do not use any inline class member functions.
Create a main function. In the main function create two Student objects. Use the appropriate get functions to print all the values of all the member variables for the first Student object. For the second object, use the set methods to change the student name to John Doe and the social security number to 123456789. Use the appropriate get functions to print all the values of all the member variables for the second Student object.
Do not print from the Student class. Instead you will retrieve the data in the main() function and print from main.

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
include "stdafx.h"
#include <iostream>
using namespace std;

class Student
{
private:
	long ssn;
	char name[80];
public:
	Student();
	Student(char name[80], long ssn);
	void setSSN(long);
	long getSSN();
	void setName(char *);
	char getName();
};

Student::Student()
{
	name = "unassigned";
	ssn = 999999999;
	
}

Student::Student(char name[80], long ssn)
{
	setSSN(ssn);
	setName(name);
}
long Student::getSSN()
{
	return ssn;
}

char Student::getName()
{
	return name[80];
}
	
void Student::setSSN(long newSSN)
{
	while (ssn>0)
	ssn = newSSN;
}

void Student::setName(char newName[80])
{
	while (strlen(newName) > 0)
		name[80] = newName[80];
}

int main()
{
	Student stu1;
	cout << "Name for student1 is " << stu1.getName() << " and ssn is " << stu1.getSSN() << endl;
	Student stu2("John Doe", 123456789);
	cout << "Name for student2 is " << stu2.getName() << "and ssn is " << stu2.getSSN() << endl;
	return 0;
}
Last edited on
Is there any particular reason you're using C-strings instead of std::string?

With C-string, unlike std::string, you can't use the assignment operator=, you'll need to use strcpy().

The assignment requires using a char array. Can I use std:string with the char array? I feel that that is a silly question. Forgive me, I truly am new at this.
Last edited on
Line#21, you cannot just assign a a string to a char array, you need to use a string function to copy the chars into the array and then terminate with a null terminator. I would suggest that you use:

 
strcpy_s(name, sizeof(name), "unassigned");


You don't appear to have leant too much about strings aka char arrays. You have made the same mistake in the 'setName' function, your code is simply assigning the char at index 80 on the char array named 'newName' to the member variable named 'name'. To achieve your objective use the strcpy_s function, which ensures that the string your copying to is large enough and won't result in an overflow hence the argv[1].

Your function 'getName' is also returning a char and not a string, it should be declared as:

 
char* Student::getName()


... and should return the pointer to the first char in the array:

 
return name;


HTH
Thanks for the quick responses!
Your post definitely helped with my default constructor and getName function, ajh32. It is now successfully retrieving and printing the ssn and name for stu1.

It is not printing anything from my 2nd cout statement yet. At a glance, is there anything wrong with my setSSN and setName functions? For now, I'll tinker with it and come back if I can't figure it out for myself.

Last edited on
See my comments on your 'setName' and 'getName' functions. That should solve your problem.

Your custom constructor should be declared thus:

 
Student::Student(char* name, long ssn)


and the 'setName' thus:

 
void Student::setName(char* newName)


Also, in the 'setName' function replace while with if, and in 'setSSN'. Also, in 'setSSN' check the variable you are checking for length.
Last edited on
Yea, I realized i should replace the While loops with if's. I don't know why I did that in the first place.

Got it working great! It's pretty apparent that I am not too knowledgeable about char pointers and classes in general, so I really appreciate the tips.
Topic archived. No new replies allowed.