Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include<iostream>
using namespace std;

class  BankDeposit
{ 
    
private: 
int principal;
int years ;
float InterestRate;
float returnValue;    

public: 
        BankDeposit(){}
        BankDeposit(int p , int y ,float r);
        BankDeposit(int p , int y ,int r);
    void show();



};

BankDeposit::BankDeposit(int p,int y,float r){


    principal = p;
    years = y;
    InterestRate = r;
    returnValue = principal ;

    for (int i = 0; i < y; i++)
    {
        
      returnValue = returnValue * (i+r);        
    }
    
}


 void BankDeposit ::show (){


    cout<< endl<<"principal amount was"<<principal<<"return value after"<<years
    <<" years is"<<returnValue<<endl;
}







int main()
 {  BankDeposit bd1,bd2,bd3;
 int p ,y ;
 float r;
    int R;
    cout<<" Enter the value of p y and r"<<endl;
    cin>>p>>y>>r;
       bd1.show();
       return 0 ;
  }


What I have tried:

i have tried to some interest rate problems using classes... but somehow this program is giving me some garbage value...
plz someone help me...
Posted
Updated 30-Nov-21 6:50am
Comments
jeron1 29-Nov-21 15:16pm    
You haven't set the data variables of your class with anything. You probably want to create the object after you have received input from the user, and use the parameterized constructor that you've made, then call the show() method.
Richard MacCutchan 29-Nov-21 15:26pm    
That is the actual solution.
Neeraj Surnis 30-Nov-21 8:11am    
okk sir thank you for your help

You need to discover an important part of software development: debugging. Debugging is the process of executing your code line-by-line and watch for the behaviour of the values you are working with. This way, you can spot where the difference occurs between what should happen, and what actually happens.

There are a lot of resources available on the subject, you just have to find the one which is appropriate to your compiler/ide.
c++ debugging[^]
 
Share this answer
 
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Your input is going to some local vars but your output is the object.

The primary fix is to code the constructor in c++
C++
BankDeposit(){
  principal = years = InterestRate = returnValue = 0;
}
but the real solution is to Learn C++ tutorial and use the debugger.

tip: write a function which the BankDeposit is asking for input its value and some print function. Thats for classes were invented ;-)
 
Share this answer
 
As previously stated, the class members need to be set to a value. C++ allows one to initialize class members inline. For your class, that could look like this :
C++
class  BankDeposit
{ 
private: 
    int   m_Principal     { 0 };
    int   m_Years         { 0 };
    float m_InterestRate  { 0 };
    float m_ReturnValue   { 0 };
};
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900