Variable and array declaration related query

Hi all,

I am trying to write a program to evaluate V=P(1+r)^n
Simple program to do this is done but now I am trying to print a table also with all the values of P, r and n entered by user and V calculated from it.
Here I am facing 2 problems:
1) I want to declare and initialize a variable (counter) and be able to access it in all 3 methods.
2) I want to declare array without specifying its size and load values in it at run time.

Can I achieve above 2 things? I have provided the code I have written so far which doesn't achieve above 2 things :-(

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <cmath>
using namespace std;

class value
{
	float V, P, r, n;
		
	public:
		//int counter;
		float value[], principal[], rate[], number_of_years[];
		void getValues(void);
		void displayValues(void);
		void displayTable(void);	
};

void value :: getValues(void)
{
	cout << "\nPlease enter principal amount 'P' : ";
	cin >> P;
	cout << "\nPlease enter number of years 'n' : ";
	cin >> n;
	cout << "\nPlease enter rate of interest 'r' : ";
	cin >> r;
}

void value :: displayValues(void)
{
	//counter = 0;
	V = P * (pow((1 + r),n));
	cout << "\n---------------------------------------------------------------------------------------------------------";
	cout << "\nValue of money is : " << V;
	cout << "\nFor principal amount 'P' = " << P << "; number of years 'n' = " << n << "; and rate of interest 'r' : " << r;
	cout << "\n---------------------------------------------------------------------------------------------------------";
	value[counter] = V;
	principal[counter] = P;
	rate[counter] = r;
	number_of_years[counter] = n;
	counter++;
	cout << "value= " << value[counter] << "principal= " << principal[counter] << "rate= " << rate[counter] << "number_of_years= " << number_of_years[counter] << endl;
}

void value :: displayTable(void)
{
	int loop;
	
	if (counter != 0)
	{
		cout << "\nKeep watching this space for more to come :-)";
		for (loop = 0; loop < counter; loop++)
		{
			cout << "value" << value[loop] << "principal" << principal[loop] << "rate" << rate[loop] << "number_of_years" << number_of_years[loop] << endl;
		}
		cout << "\nValue of counter : " << counter;
	}
	else
	{
		cout << endl;
	}
}

int main()
{
	value v;
	int choice;	
				
	do
	{
		cout << "\n-----------------------------------------------------------------------------------------\n";
		cout << "If you want to calculate the value(V) then please enter '1' (numeric one)!\n";
		cout << "If you want to exit, please enter '0' (numeric zero)!\n";
		cout << "-----------------------------------------------------------------------------------------\n";
		cout << endl;
		cout << "Please enter your choice : ";
		cin >> choice;
				
		if (choice != 0)
		{
			v.getValues();
			v.displayValues();
		}
		else
		{
			v.displayTable();
			cout << endl;
			cout << "\nGood Bye!\n";
		}
				
	}while(choice != 0);
	
	return 0;
}


Pardon me for making code such a mess but I tried many things but I know I haven't reached to the point where I can get knowledge of these 2 things.
Hi all,

While studying further, I realized solution to first question:
1) I want to declare and initialize a variable (counter) and be able to access it in all 3 methods.
Ans: I declared variable "int counter" as public variable in "class value" as I had done earlier and then initialized it to zero in main using "v.counter = 0"

About second question:
2) I want to declare array without specifying its size and load values in it at run time.
I think I'll have to use vector here, but I am not yet at a stage to understand how to pass vectors as arguments from one function to another.

So once I am there, I am hopeful that I'll find solution to this :-)
question. in the displayvalues function, what does value[], principal[], and number-of-years[] output? i tried compiling it on my computer and for some reason it keeps on displaying the same value.

2) I want to declare array without specifying its size and load values in it at run time.
I think I'll have to use vector here, but I am not yet at a stage to understand how to pass vectors as arguments from one function to another.


the basics of vectors aren't that hard to understand. I can pretty much assume that the only functions of vector you'll need to use is the .size (give the size of the vector [ex. nameofvariable.size() ]) and .empty (checks if variable is empty [ex. nameofvariable.empty() ). if you know arrays then it isn't that hard. try this video:

http://www.youtube.com/watch?v=8-65K0qq-P4
It seems that you do not really understand how classes work, so why use a class in the first place ? This program could as easily be written using only C-style functions.


Regarding your questions :
1) "counter" does not need to be a public variable. It should be initialized in the class' constructor (along with V, P, r and n).

2) Yes, you should use a vector. You won't need to pass them as arguments if they are members of your class.

Also, do not define arrays like that : float value[], principal[], rate[], number_of_years[];
It's not doing what you think.
Thank you Dammned and totum. Yes I am really new at C++ and at times I am trying to ahead of what I have learned till the moment.
@totum yes, I tried this wothout using class and it is much easier for my level that way. Once I learn arrays and vectors, I'll try to complete that part.
Thanks for your time guys :)
Topic archived. No new replies allowed.