Pointers question

I have almost everything done except me entering 3 digits and getting the correct sum through pointers. Did I do the formula right? Also, for some reason when I put in the numbers it doesn't even display them back anymore. I did nothing to alter the display itself.
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
 #include <iostream>
using namespace std;
const int MAXNAME = 10;

int main()
{
	int pos;
	char* name;
	int* one;
	int* two;
	int* three;
	int result;

	
	one = new int;
	two = new int;
	three = new int;
	name = new char;

	cout << "Enter your last name with exactly 10 characters." << endl;
	cout << "If your name has < 10 characters, repeat last letter. " << endl
		<< "Blanks at the end do not count." << endl;

	for (pos = 0; pos < MAXNAME; pos++)
		cin >> *(name+pos);		// read a character into the name array 					
		cout << "Hi ";


		for (pos = 0; pos < MAXNAME; pos++)
			cout << *(name+pos);	// print a character from the name array					

		cout << endl << "Enter three integer numbers separated by blanks" << endl;
		cin >> *one >> *two >> *three;
	

	cout << "The three numbers are " << endl;
	cout << *one << "|" << *two << "|" << *three << endl;// output those numbers 

	result = *one + *two + *three / 3;// calculate the sum of the three numbers

		cout << "The sum of the three values is " << result << endl;

		delete one;//code to deallocate one, two, three and name 
		delete two;
		delete three;
		delete name;
	return 0;
}
Line 39 needs parentheses.
result = (*one + *two + *three) / 3
That's not the sum, like line 41 misleadingly reports

new char creates exactly one char. If you want space for 10 of them, say so:
1
2
3
int* name = new char[10];
// ... 
delete[] name; // N.B.: delete[] 

Remember that new must be paired with delete; new[] must be paired with delete[].
Get in the habit of freeing resources in the opposite order of their acquisition.
1
2
3
4
5
6
7
int* one   = new int;  
int* two   = new int;  
int* three = new int;  
// ... delete in opposite order of allocation
delete three;  
delete two;
delete one;

Ignoring this guideline is a source of errors when the resources are interdependent.

new and delete are a huge source of problems. There's more in addition to what I've mentioned already. For example, even though delete is used, the program still leaks memory in certain cases because it does not provide any exception safety guarantee.

The bottom line is that new and delete are for careful use by experts. Strongly prefer library components that eliminate these problems, such as std::string, std::vector, and std::unique_ptr.
Ok thanks for the input, but I still can't tell why in the world it won't display the numbers I put in and have the formula do its work for the final answer. It was working find prior to posting, but decided not to work.
Last edited on
I still can't tell why in the world it won't display the numbers I put in


Writing outside of the bounds of your char (pretending there's space for 10 when there's only room for one) causes undefined behavior.

Programs with undefined behavior have meaningless results. The C++ standard doesn't say how such programs should behave, so their behavior is incidental at best. You got (un)lucky, and then you didn't.
Last edited on
Whoa. What the hell? it just worked! How?! I never had this happen to me before.
Your program's incidental behavior might depend on what you enter.

You may be able to exploit this to make the system do something you didn't intend:
https://en.wikipedia.org/wiki/Stack_buffer_overflow
Last edited on
Topic archived. No new replies allowed.