I'm doing an assignment learning about classes. We are suppose to write a program that uses our class we write and it is suppose to accept a Social security number and a name. We have to use a C style string no string class allowed. I think i have it all right except my C-String stuff. The compile tells me both my a and b objects have a garbage value for the char part. I know I'm not assigning my char array variable right i just can't see how to fix it. Also we have to use a character array of 80.
This doesn't do what you think. This just copies a single character from one name to the other. And actually the character you're copying is out of bounds of your array -- so you're actually corrupting memory when you do it.
To copy a C style string, you need to copy all the characters up to the null terminator. This can be accomplished with the strcpy function:
strcpy(name,studentname); // copy studentname to name
Note you also have the same problem with your setName function.
that got me farther then i have gotten so far. This is why i shouldn't try and do this stuff late at night. LOL. Now it prints the two socials but gives a hieroglyph type of char for the name. But if i put a break point at line 54 the objects are saying the proper values.
ya i see that. The problem is if i remove the [SIZE] from the return value i get a red squiggly under the first parenthesis saying "Error: return value does not match the function type". All the example programs i am looking at declare and print like this. As far as i can tell all i should have to do is return name and I'm golden but it just doesn't want to cooperate. Stupid computer:P
"Error: return value does not match the function type
That's because your return type is wrong. You're returning a single char.
C strings work by passing a pointer to the first char in a string. From the pointer, the program can find the entire string because each char is stored sequentially.
So instead of returning a char, return a pointer to a char.
Thats kind of what i figured i tried it in the form
return (*name);
and was able to get the first letter. I know I'm not doing it entirely right. I'll just have to look at the pointer section about that again. I do remember that being mention in my reading. Right now i have to to to bed for work in the morning. Thanx for your help.
// DateClass.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <iomanip>
usingnamespace std;
constint SIZE = 80;
class Student
{
private:
long social;
char name[SIZE];
public:
Student (long = 999999999 , char [] = "unassigned");
void setSSN(long);
long getSSN();
void setName(char []);
charconst* getName();
};
Student::Student(long ssn, char studentname[SIZE])
{
social = ssn;
strcpy (name, studentname);
}
void Student::setSSN(long SSN)
{
social = SSN;
}
long Student::getSSN()
{
return (social);
}
void Student::setName(char name2[SIZE])
{
strcpy (name, name2);
}
charconst* Student::getName()
{
return (*name);
}
int main()
{
Student a, b(123456789, "John Doe");
cout << a.getSSN() << a.getName() << endl;
cout << b.getSSN() << b.getName() << endl;
system ("pause");
return 0;
}
I have tried the changes you guys have mentioned looked at the tutorial section here and skimmed through the chapters on pointers and c style strings in two different books. Right now the when i run the code i get build errors if i say run last successful build it prints what i want. I have a little red squiggly under the first parenthesis on line 49 it says return value doesn't match the function type. If i take the const out of line 46 and 23 i get the same error.
thats what i would figure. But if i do that i get a build error. I have to put return (name[SIZE]); to make the program run through and then in the pint out i get some weird symbols. The social part of the objects print fine but the name part doesn't do it.