C-String issue

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.
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
// DateClass.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"

#include <iostream>
#include <iomanip>
using namespace std;
const int size = 80;

class Student
{
private:
	
	long social;
	char name[size];
public:

	Student (long = 999999999 , char[] = "unassigned");
	void setSSN(long);
	long getSSN();
	void setName(char[]);
	char getName();
};

Student::Student(long ssn, char studentname[size])
{
	social = ssn;
	name[size] = studentname[size];
}

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

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

void Student::setName(char name2[size])
{
	name[size] = name2[size];
}
char Student::getName()
{
	return (name[size]);
}

int main()
{
	Student a, b(123456789, "John Doe");
	cout << a.getSSN() << a.getName() << endl;
	system ("pause");
	return 0;
}
name[size] = studentname[size];

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.
getName has a similar problem. You're returning a single char, not the whole string.
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
1
2
3
const int MAXCHAR = 80
char message [MAXCHAR] = "hello"
cout << message << endl;
"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.
return (*name);

The first thing you need to do is change the type the function is returning.

This line is incorrect:

 
char Student::getName()


Because that line says the function returns a char.

You need it to return a pointer to a char.
change
 
char getName()

to
 
char const* getName() //remember to add "const" 



I'm about to pull my hair out. Here's what i have.
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
// DateClass.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"

#include <iostream>
#include <iomanip>
using namespace std;
const int SIZE = 80;

class Student
{
private:
	
	long social;
	char name[SIZE];

public:

	Student (long = 999999999 , char [] = "unassigned");
	void setSSN(long);
	long getSSN();
	void setName(char  []);
	char const* 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);
}
char const* 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.
Don't return (*name), that will deference the pointer, which means you will return a single char. Just return name by itself.
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.
no no, joker. If return (name[SIZE]); is compiling, then your function is not returning a pointer.

You need to change your function so that it returns a pointer. Then you just return 'name':

1
2
3
4
5
char const* Student::getName()
{	
	
	return name;  // no *, no [SIZE]
}


Remember that [brakets] give you a single character from the string, they do not give you the whole string. Not even if you put the size in them.
Last edited on
oops, I forgot to add const
char const* getName() const
Topic archived. No new replies allowed.