[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

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

Why does my C++ code not work?

Hello,

I have created a simple program consisting of a number of header files into which I have placed various functions. I have tried to run it but get an error message. Could someone please have a look?

Thanks.
C--Users-u166946-C---Level-3-Chap1-banka
C--Users-u166946-C---Level-3-Chap1-banka
C--Users-u166946-C---Level-3-Chap1-Banka
0
f19l
Asked:
f19l
1 Solution
 
jkrCommented:
Your header files are included as

#include "bankacct.h"
#include "bankacdef.h"

Open in new window


yet they seem to be named with a '.hpp' suffix - change that to '.h'.

Then, you are using 'account[]' in "bankacdef.h", which is declared in your 'main()'. To get that to run, make it a global variable in the -cpp file and be sure to declare it before including "bankacdef.h", i.e.

    //preprocessor calls...
#include "bankacct.h"

BankAccount account[MAX]; // <-- !!!

#include "bankacdef.h"


       
int main()
{
    //instantiate three objects of bank accounts...
    
    
    //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;
        
    //deposit
    setdeposit();
    
    //withdrawal
    setwithdrawal();
    
    cout<<"\n\t Press any key to close.\n";
    getch();
    return 0;
}                         

Open in new window


Are you sure you don't want to turn "bankacdef.h" into a "bankacdef.cpp"? That would make sense, since it contains code.

BTW, with the above changes, it compiles for me.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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