Declare dynamically and pointer notation

Objective: To demonstrate your knowledge of using pointers of the built in data types.

Solicit two numbers from the user, one whole number and one decimal number.
Display the corresponding mathematical results. All necessary variables should be declared dynamically. Use pointer notation wherever possible.

I have the layout for what the code will do, but I don't understand how to alter my code so that the variables are declared dynamically. I think I know what to do with the pointer notation, but if you want to help with that as well it would be appreciated! Thanks!

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
  #include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{

	int number;
	double decimal; 
	double add = 0; 
	double multiply = 0;
	double divide = 0; 
	double subtract = 0;

	cout << "Please enter a whole number value: ";
	cin >> number;
	cout << endl;
	cout << "Please enter a decimal value: " ;
	cin >> decimal;
	cout << endl;

	add = number + decimal;
	subtract = number - decimal;
	divide = number / decimal;
	multiply = number * decimal;

	cout << "The total is " << add << endl;
	cout << "The product is " << multiply << endl;
	cout << "The division result is " << divide << endl;
	cout << "The difference is " << subtract << endl << endl;

	system ("PAUSE");
	return 0;
}
As you can see, this is a horrible assignment because there is absolutely no reason to use dynamic memory or pointers here (though, honestly, there's almost never a reason to use a pointer in C++). However, your assignment is very clear and explicit: all variables must be pointers.

Here's something to start you off:
7
8
9
	int *number = new int;
	double *decimal = new double; 
	double *add = new double(0); 
Don't forget to properly call delete at the end of your program, once for each object you create with new.

Also, if you get the chance, show this article to whoever is forcing this assignment upon you:
http://www.lb-stuff.com/pointers
@LB Can you elaborate on your statement a little further? What exactly do you mean
though, honestly, there's almost never a reason to use a pointer in C++

Why would you think that their is never a reason to use pointers in C++, that is pretty comical to me that you would think that.
Last edited on
I gave a link explaining why at the bottom of my post.
Topic archived. No new replies allowed.