Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 221
  • Last Modified:

Why does my C++ code keep crashing?

Hello,

When I try to run the attached code it ends up crashing. Specifically, when I answer yes to the first question that appears the code zips through to the very end and then crashes. I am not sure how to correct this.

Thanks.
C--Users-u166946-C---Level-3-Chap1-BankA
0
f19l
Asked:
f19l
1 Solution
 
ste5anSenior DeveloperCommented:
It should not even compile..
0
 
jkrCommented:
Both 'deposit' and 'withdrawal' are used to get character input from the user, yet they're declared as 'float'  that will not work. When you change that to 'char', it works for me, i.e.

//preprocessor calls...
#include <iostream>
#include <string>
#include <conio.h>
#include <istream> 
#include <ostream> //for output filing functions
#include <fstream> //for filing functions
using namespace std;

const int MAX=3;
char deposit; // <-- changed
char withdrawal; // <-- changed
float acc_balance;
int index=0;
int accnumin=0;
int acnum=-1; 
 
class BankAccount
{
      private:   //private attributes first...
         int accnumber;
         string accholder;
         float accbalance;
         
         
      public:  //followed by public methods
         
         
         BankAccount();   //the constructor
         
         void setAccNumber(int acnumin);
         int getAccNumber();
         
         void setAccHolder(string acholderin);
         string getAccHolder();
         
         void setAccBalance(float acamountin);
         float getAccBalance();
         
         void setDeposit(float depamnt);
         void setWithdrawal(float witamnt);
};

BankAccount::BankAccount()   //constructor sets account balance to zero
                             //when an instance of BankAccount is created
{
         accbalance=0;
}
void BankAccount::setAccNumber(int acnumin)   //sets the account number
{
         accnumber=acnumin;
}
int BankAccount::getAccNumber()   //returns integer account number
{
         return accnumber;
}
void BankAccount::setAccHolder(string acholderin)   //sets account name
{
         accholder=acholderin;
}                                                          
string BankAccount::getAccHolder()   //returns name of account number
{
         return accholder;
}                                         
void BankAccount::setAccBalance(float acamountin)   //sets the initial account balance
{
         accbalance=acamountin;
}                
float BankAccount::getAccBalance()    //returns current account balance
{
      return accbalance;
}
void BankAccount::setDeposit(float depamnt)   //adds an amount to the account balance
{
         accbalance+=depamnt;
} 
void BankAccount::setWithdrawal(float witamnt)   //subtracts an amount from the account balance
{
         accbalance-=witamnt;
} 
       
int main()
{
    //instantiate three objects of bank accounts...
    BankAccount account[MAX];
    
    //set the account details...
    //account1...
    account[0].setAccNumber(110);
    account[0].setAccHolder("Charles Darwin");
    account[0].setAccBalance(1562.65);
    //account2...
    account[1].setAccNumber(120);
    account[1].setAccHolder("Michael Faraday");
    account[1].setAccBalance(2753.48);
    //account3...
    account[2].setAccNumber(130);
    account[2].setAccHolder("Charles Babbage");
    account[2].setAccBalance(3951.42);
    
    //display the account details...
    //account1...
    cout<<"\n Account Number 1: "
        <<account[0].getAccNumber()<<endl;
    cout<<" Account Holder 1: "    
        <<account[0].getAccHolder()<<endl;
    cout<<" Account Balance 1: "
        <<account[0].getAccBalance()<<endl;

    //account2...
    cout<<"\n Account Number 2: "
        <<account[1].getAccNumber()<<endl;
    cout<<" Account Holder 2: "    
        <<account[1].getAccHolder()<<endl;
    cout<<" Account Balance 2: "
        <<account[1].getAccBalance()<<endl; 
        
    //account3...
    cout<<"\n Account Number 3: "
        <<account[2].getAccNumber()<<endl;
    cout<<" Account Holder 3: "    
        <<account[2].getAccHolder()<<endl;
    cout<<" Account Balance 3: "
        <<account[2].getAccBalance()<<endl;  
    
    cout<<endl<<endl;
        
    cout<<"Do you wish to deposit money into an account? (y/n) ";    
    cin>>deposit;   
    cout<<endl;
    if ((deposit!='n')&&(deposit!='N')) 
    {
        cout<<"Which account do you wish to deposit money into? ";
        cin>>accnumin;
        int i =0;
        while (acnum !=accnumin)
        {
              //fetch each a/c number, from the existing account
              //object, in turn...
              acnum=account[i].getAccNumber();
              cout<<"\n A/C number found; "<<acnum<<endl;
              //compare the found a/c number with that entered...
              //if no match, increment to move onto the next one...
              if (acnum==accnumin)
                 index = i;
              else
                 i++;
        }   
        if (acnum!=accnumin)
           cout<<"\n That account was not found.\n";
        else
        {
        cout<<"\n The account holder is "
            <<account[index].getAccHolder()<<endl;
        cout<<"\n The account balance is "
            <<account[index].getAccBalance()<<endl;        
        }                 
        cout<<endl<<endl;
        cout<<"How much do you wish to deposit? ";
        cin>>acc_balance;
        cout<<endl<<endl;
        acc_balance=account[index].getAccBalance()+acc_balance; 
        account[index].setAccBalance(acc_balance);                        
        cout<<"The new account balance is "<<account[index].getAccBalance()<<endl;                                                              
    }

    cout<<endl<<endl;
        
    cout<<"Do you wish to withdraw money from an account? (y/n) ";    
    cin>>withdrawal;   
    cout<<endl;
    if ((withdrawal!='n')&&(withdrawal!='N')) 
    {
        cout<<"Which account do you wish to withdraw money from? ";
        cin>>accnumin;
        int i =0;
        while (acnum !=accnumin)
        {
              //fetch each a/c number, from the existing account
              //object, in turn...
              acnum=account[i].getAccNumber();
              cout<<"\n A/C number found; "<<acnum<<endl;
              //compare the found a/c number with that entered...
              //if no match, increment to move onto the next one...
              if (acnum==accnumin)
                 index = i;
              else
                 i++;
        }   
        if (acnum!=accnumin)
           cout<<"\n That account was not found.\n";
        else
        {
        cout<<"\n The account holder is "
            <<account[index].getAccHolder()<<endl;
        cout<<"\n The account balance is "
            <<account[index].getAccBalance()<<endl;        
        }                 
        cout<<endl<<endl;
        cout<<"How much do you wish to deposit? ";
        cin>>acc_balance;
        cout<<endl<<endl;
        acc_balance=account[index].getAccBalance()-acc_balance; 
        account[index].setAccBalance(acc_balance);                        
        cout<<"The new account balance is "<<account[index].getAccBalance()<<endl;                                                              
    }
    cout<<"\t Press any key to close.\n";
    getch();
    return 0;
}                         

Open in new window



Output:


 Account Number 1: 110
 Account Holder 1: Charles Darwin
 Account Balance 1: 1562.65

 Account Number 2: 120
 Account Holder 2: Michael Faraday
 Account Balance 2: 2753.48

 Account Number 3: 130
 Account Holder 3: Charles Babbage
 Account Balance 3: 3951.42


Do you wish to deposit money into an account? (y/n) y

Which account do you wish to deposit money into? 110

 A/C number found; 110

 The account holder is Charles Darwin

 The account balance is 1562.65


How much do you wish to deposit? 110


The new account balance is 1672.65
0
 
f19lAuthor Commented:
Clear, precise and useful explanation.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now