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 :-(
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:
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 :)