Question:
C++ Homework Help - Class Member Fuctions?
2011-07-08 13:18:56 UTC
I have been working on this assignment all day and I can't seem to get it to run correctly. Here is what I have so far:

# include
# include
using namespace std;

const int MAX = 80;

class Student
{
private:
long SSN;
char name[MAX];

public:
Student( );
void setSSN(long);
void setName(char const*);
char const* getName( );
long getSSN( );

};

Student::Student( )
{
char name[MAX] = "unassigned";
long SSN = 999999999;
}


void Student::setSSN(long num)
{
SSN = num;
}

long Student::getSSN( )
{
return SSN;
}

void Student::setName(char const* stu)
{
name[MAX] = stu[MAX];
}

char const* Student::getName( )
{
return name;
}

int main( )
{
Student student1, student2;

cout << "Name for student1 is " << student1.getName( );
cout << " and ssn is " << student1.getSSN( ) << endl;

student2.setName("John Doe");
student2.setSSN(123456789);

cout << "Name of student2 is " << student2.getName( );
cout << " and ssn is " << student2.getSSN( ) << endl;

system("pause");

return 0;
}

The output is gibberish. The only thing that prints correctly is the student2 SSN. Any ideas how to fix this?

Name for student1 is ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠pi▄î└■. and ssn is -858993460
Name of student2 is ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠o╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠pi▄î└■. and ssn is 123456789
Press any key to continue ...
Four answers:
?
2011-07-08 14:51:23 UTC
You "are not to use strings", yet the assignment asks to store and retrieve a string value (the student name)? Sounds like 1991.



Either way, you have good answers already, they should help you turn in the expected solution (I would only point out that strncpy() should be used instead of strcpy() in setName() and in the constructor). But in case you're genuinely interested in C++, here's what's wrong with this design:



1) arrays instead of strings (already pointed out)

2) meaningless default constructor (what kind of student is someone with the name "unassigned"? Such student does not exist in real life, therefore no such Student object should ever exist in your object model)

3) unnecessary setters -- do you expect the student's SSN or name to ever change during his or her lifetime? If not, there should be no way to do that. If yes (your female students might get married?) then name them appropriately, "changeName()", not "set".

4) violations of const-correctness (you told the compiler that getName() and getSSN() could modify the Student, while in fact they don't)



With design and style fixes, the program becomes:



#include

#include

class Student

{

 private:

     unsigned long SSN;

     std::string name;

 public:

     Student(unsigned long ssn, const std::string& str) : SSN(ssn), name(str) {}

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

     unsigned long getSSN() const { return SSN; }

};

int main( )

{

     Student student1(123456789, "John Doe");

     std::cout << "Name for student1 is " << student1.getName()

                    << " and ssn is " << student1.getSSN() << '\n';

}



test: https://ideone.com/h856e



(don't turn this in, turn in what is expected of you to show that you've learned the material)
modulo_function
2011-07-08 13:44:31 UTC
Since you're coding in C++ why not use the string class rather than the old C-strings?



I've changed your program to use string class. You do need to include .



You also need to include in order to use system("pause");





Here's your program with those changes:



# include

# include



#include

using namespace std;



// const int MAX = 80;



class Student

{

private:

long SSN;

string name;



public:

Student( );

void setSSN(long);

void setName(string);

string getName( );

long getSSN( );



};



Student::Student( )

{

name = "unassigned";

long SSN = 999999999;

}





void Student::setSSN(long num)

{

SSN = num;

}



long Student::getSSN( )

{

return SSN;

}



void Student::setName(string stu) // jdw

{

// name[MAX] = stu[MAX];

name = stu; // jdw

}



string Student::getName( )

{

return name;

}



int main( )

{

Student student1, student2;



cout << "Name for student1 is " << student1.getName( );

cout << " and ssn is " << student1.getSSN( ) << endl;



student2.setName("John Doe");

student2.setSSN(123456789);



cout << "Name of student2 is " << student2.getName( );

cout << " and ssn is " << student2.getSSN( ) << endl;



// system("pause");



return 0;

}



+add

You should add you email to your profile. I had more to add when you previously asked for help but you had already chosen best answer and so there was no way for me to communicate.



++add

Too bad you can't use string. When I was learning C++ as soon as I could use string I did. Consequently I'm not very good at C-strings (char arrays). You might need to learn more about c-string functions such as strcpy and the such in order to do what you want. Also, I've seen people use pointers for what you're trying to do. Sorry I can't help you more.



+++add

Please add your email to your profile because I don't want to waste time posting additional help if you're not going to see it.



I added

#include



and change setName to use:

strncpy(name,stu,MAX);



and it seems to work OK. This is part of what I got out of Cubbi's post. Btw, Cubbi is one of the most capable C++ guys on this site.
The Phlebob
2011-07-08 13:28:37 UTC
Part of your problem is in the constructor:



Student::Student( )

{

char name[MAX] = "unassigned";

long SSN = 999999999;

}



Believe it or not, you're not setting the property SSN there. Because you declare a type (long), you're creating a local variable with the name SSN and the property is going uninitialized. Lose the "long" and try again.



Same for name[]. The end result should be something like this:



Student::Student( )

{

strcpy( name, "UNASSIGNED" );

SSN = 999999999;

}





Hope that helps.
eli porter
2011-07-08 13:28:11 UTC
firstly in the constructor remove the char and long, you've already declared the variables in the class, by adding the identifier are you instead creating new local variables which you don't meant to do and are leaving the class members still unassigned.



also remove the [MAX] from stu in setName, try:

name[MAX] = stu;

or

name = stu;

(I don't quite remember which is correct)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...