Vectors and Objects
Aug 24, 2009 at 10:06pm UTC
It has been suggested I should use a vector to create an indefinate number of objects. I am having some issues making this work. I get a windows crash whenever I input a value into my object. This is all I have so far, I know all the functions aren't used but I wanted to see what I am doing wrong before I finish.
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
#include<iomanip>
#include<vector>
#include<iostream>
using namespace std;
class account{
public :
void account::init();
void account::withdraw();
void account::calcint();
void account::deposit();
void account::changeint();
void account::display();
private :
double balance, interest,;};
void account::init(){
cout<<setprecision(2)<<setiosflags(ios::fixed)<<setiosflags(ios::showpoint);
cout<<"\nOpening balance for account: " ;cin>> balance;
cout<<"\nInterest rate for account: " ;cin>> interest;}
void account::withdraw(){
cout<<setprecision(2)<<setiosflags(ios::fixed)<<setiosflags(ios::showpoint);
double w;
cout<<"\nEnter amount for withdrawel: " ;cin>>w;
if (w<balance){balance-=w;}
else {cout<<"\nInsufficient Funds!" ;}
}
void account::calcint(){
cout<<setprecision(2)<<setiosflags(ios::fixed)<<setiosflags(ios::showpoint);
cout<<"Applying interest rate." ;balance+=(balance*(interest/100));}
void account::deposit(){
cout<<setprecision(2)<<setiosflags(ios::fixed)<<setiosflags(ios::showpoint);
double d;
cout<<"\nEnter amount for deposit: " ;cin>>d;
balance+=d;}
void account::changeint(){
cout<<setprecision(2)<<setiosflags(ios::fixed)<<setiosflags(ios::showpoint);
cout<<"\nEnter new interest rate: " ;cin>>interest;}
void account::display(){
cout<<setprecision(2)<<setiosflags(ios::fixed)<<setiosflags(ios::showpoint);
cout<<"\nAccount balance: " <<balance<<"\nInterest rate: " <<interest;}
int main(){
cout<<setprecision(2)<<setiosflags(ios::fixed)<<setiosflags(ios::showpoint);
cout<<"\nAccount Manipulation pragram\nBy Joshua Smith" ;
vector<account> a;
int go=0;
int current=0;
int ans;
while (go==0){
system("cls" );
cout<<"Current Account: " <<current;
cout<<"\n1.Open New Account\n2.Close Account\n3.Deposit funds\n4.Withdraw funds\n5.Apply Interest\n6.Change Interest Rate\n7.Save Data\n8.Load Data\n9.Exit\n" ;
cin>>ans;
switch (ans){
default :
cout<<"\nInvalid Entry" ;
break ;
case 1:
cout<<"\nEnter three digit account number: " ;
cin>>current;
if (a.size()<current){a.resize(current);}
a[current].init();cout<<"success" ;
break ;}}}
Aug 24, 2009 at 10:26pm UTC
You have an off by one error. The vector when resized to current only goes from 0 - (current -1)
Aug 24, 2009 at 11:17pm UTC
thanks a lot. Just learning how to deal with vectors.
Aug 25, 2009 at 12:47pm UTC
Just FYI...
1 2 3 4 5 6 7 8 9 10
class account{
public :
void account::init(); // Many compilers will complain about the "account::" here; remove it.
void account::withdraw(); // ditto, and ditto for the next 5 lines also
void account::calcint();
void account::deposit();
void account::changeint();
void account::display();
private :
double balance, interest,;};
Topic archived. No new replies allowed.