C++ Pointer question

Hello, I started pointers last week in college. I'll get straight to my problem

Edit - Adding main() just in case
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
int main()

{
	Person   myobject;
	
	
		myobject.askPerson();
		//myobject.getName();
		//myobject.getAge();

		//myobject.Point();
		//myobject.displayPersonDetails();
		
	/*Person arrayOfpeople[NUMBEROFPEOPLE];
	{
		for (int i = 0; i < NUMBEROFPEOPLE; i++)
		{
			arrayOfpeople[i].askPerson();

		}


		for (int i = 0; i < NUMBEROFPEOPLE; i++)
		{
			cout << i;
			arrayOfpeople[i].displayPersonDetails();
		}
		
	}
	*/
	return (0);

}



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
#include <iostream>
#include <string>
#include <stdio.h>

using namespace std;

#define		CHARARRAY 20
#define		NUMBEROFPEOPLE 5

class Person

{
public:
void Point(void);
	void askPerson(void);
	char* getName();
	int getAge();

	void displayPersonDetails(void);
	
private:

	char Name[CHARARRAY];
	int Age;
	char *NamePtr;
	int *AgePtr;
}

LIKELY LOCATION OF FAULT BELOW
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
inline void Person::askPerson()
{
	
	
	cout << "Hello please enter name " << endl;
	cin.getline(Name, sizeof(Name));
	
	cout << "Please enter Age" << endl;
	cin >> Age;
	cin.ignore();

	NamePtr = &Name[CHARARRAY];
	NamePtr = new char[CHARARRAY];
	AgePtr = &Age;
	AgePtr = new int;

	cout << " This Persons name is " << *NamePtr
		<< " and their age is " << *AgePtr << endl;
	delete NamePtr;
	delete AgePtr;
}


This is the general jist of my code, it will be separated out into different funcions(ask, point, display) and will be then looped 5 times for 5 Names/ages(which is why cin.ignore is there, not even sure if it's needed but yeah). I ahve written the functions already and the loop works, however I can't get the pointers to work, the error on the console is the same regardless of its in the functions or not.

The console outputs as
1
2
3
4
5
"Please enter name..."
Brian
"Please enter Age..."
20
This persons name is = and this persons age is -842150451


Probably a simple fix but well, it's above me. Can anyone help please?
Last edited on
Hey,
let's check it out step by step ;)

A pointer stores adresses, what you're doing in the following is this:
You give the adress from Name[20] to your pointer.
Your "char-Array-name" goes from Name[0] to Name[19] which means your name "Brian"
begins at Name[0] with 'B' and goes on from there
We have no clue what happens to be at "Name[20]" because we didn't put anything there (it is even out of bounds of our array)
What we want to achieve is to give our pointer the adress of the first character in name = 'B' and that happens to be Name[0]
1
2
3
4
5
NamePtr = &Name[CHARARRAY];
//My bla-bla above means we change what you wrote above this comment to:
NamePtr = &Name[0];  //Give our pointer to adress of the start => 'B'
//Actually you can also write
NamePtr = Name; //but that's probably confusing (some people are even angry that it works) 


The next line is overwriting what we did before with a new empty char Array
It doesn't really make any sense to put this code-line here => we will just comment it out
 
//NamePtr = new char[CHARARRAY]; 


This is actually totally fine
 
AgePtr = &Age; //great! 


..if it weren't for this line
Again you overwrite the adress you gave the Age-Pointer above with some
uninitialized int which happens to be "-842150451" in your output
=> just remove this line ;)
 
//AgePtr = new int;  //bye bye line of code 


1
2
3
4
5
//I removed the * before NamePtr => with the * it just prints "B"
//Without it knows it has to print a string => Brian
//Age stuff is cool, good job
cout << " This Persons name is " << NamePtr
		<< " and their age is " << *AgePtr << endl;


Have a nice day

Edit: and remove those "delete" lines they're just not right at this point (should probably result in an error)
Last edited on
You are a god among this earth! thank you!
Topic archived. No new replies allowed.