类划分头和定义文件

时间:2019-07-17 21:03:45

标签: c++

我必须分开。此代码有效。但是,每个类定义都在其自己的头文件(.h)中,每个类成员函数定义在其自己的源文件(.C)中,而程序应用程序在其自己的源文件中。我不知道该怎么做。它没有完全连接。

我收到以下错误:

  

C:\ Users \ Jay Yoon \ OneDrive-Parkland College \ Desktop \ project2.cpp | 48 |错误:在此范围内未声明'write_account'

#include<iostream>
#include<fstream>
#include<cctype>
#include<iomanip>
#include <stdlib.h>

using namespace std;

//  CLASS USED IN PROJECT

class account
{
    int acno;
    char name[50];
    int deposit;
    int ssn;
    char address[50];
    int intRate;
public:
    void create_account();  //function to get data from user
    void show_account() const;  //function to show data on screen
    void dep(int);  //function to accept amount and add to balance amount
    void draw(int); //function to accept amount and subtract from balance amount
    void report() const;    //function to show data in tabular format
    int retacno() const;    //function to return account number
    int retdeposit() const; //function to return balance amount
    int retssn() const; //function to return ssn of account
    int retintRate() const;
};         //class ends here

void account::create_account()
{
    cout<<"\nEnter The account No. :";
    cin>>acno;
    cout<<"\n\nEnter The Name of The account holder : ";
    cin.ignore();
    cin.getline(name,50);
    cout<<"\n\nEnter The Address of The account holder : ";
    cin.ignore();
    cin.getline(address,50);
    cout<<"\nEnter SSN of The account : ";
    cin>>ssn;
    ssn=toupper(ssn);
    cout<<"\nEnter The Initial amount(>=500 for Saving and >=1000 for current ) : ";
    cin>>deposit;
    cout<<"\nEnter The Interest Rates : ";
    cin>>intRate;
    cout<<"\n\n\nAccount Created..";
}

void account::show_account() const
{
    cout<<"\nAccount No. : "<<acno;
    cout<<"\nAccount Holder Name : "<<name;
    cout<<"\nAccount Holder's address : "<<address;
    cout<<"\nSSN of Account : "<<ssn;
    cout<<"\nBalance amount : "<<deposit;
    cout<<"\nInterest Rate : "<<intRate;
    cout<<"\nTotal Balance : "<<deposit+deposit*intRate;
}



void account::dep(int x)
{
    deposit+=x;
}

void account::draw(int x)
{
    deposit-=x;
}

void account::report() const
{
    cout<<acno<<setw(8)<<" "<<name<<setw(6)<<" "<<ssn<<setw(6)<<" "<<address<<setw(17)<<" "<<deposit+deposit*intRate/100<<setw(16)<<intRate<<endl;
}


int account::retacno() const
{
    return acno;
}

int account::retdeposit() const
{
    return deposit;
}

int account::retssn() const
{
    return ssn;
}


int account::retintRate() const
{
    return intRate;
}

//   function declaration

void write_account();   //function to write record in binary file
void display_sp(int);   //function to display account details given by user
void display_all();     //function to display all account details
void deposit_withdraw(int, int); // function to desposit/withdraw amount for given account

//   Main function

int main()
{
    char ch;
    int num;
    do
    {
        system("cls");
        cout<<"----------------------------------------";
        cout<<"\nWelcome to the First National Bank\n" << "of Parkland - Account transaction system ";
        cout<<"\n----------------------------------------";
        cout<<"\n\n\t01. Create Account";
        cout<<"\n\n\t02. Deposit";
        cout<<"\n\n\t03. Withdraw";
        cout<<"\n\n\t04. Select Account";
        cout<<"\n\n\t05. Print Accounts";
        cout<<"\n\n\t06. Quit";
        cout<<"\n\n\tSelect Your Option (1-6) ";
        cin>>ch;
        system("cls");
        switch(ch)
        {
        case '1':
            write_account();
            break;
        case '2':
            cout<<"\n\n\tEnter The account No. : "; cin>>num;
            deposit_withdraw(num, 1);
            break;
        case '3':
            cout<<"\n\n\tEnter The account No. : "; cin>>num;
            deposit_withdraw(num, 2);
            break;
        case '4':
            cout<<"\n\n\tEnter The account No. : "; cin>>num;
            display_sp(num);
            break;
        case '5':
            display_all();
            break;
         case '6':
            cout<<"\n\n\tThanks for using bank managemnt system";
            break;
         default :cout<<"\a";
        }
        cin.ignore();
        cin.get();
    }while(ch!='8');

    return 0;
}


//    function to write in file

void write_account()
{
    account ac;
    ofstream outFile;
    outFile.open("account.dat",ios::binary|ios::app);
    ac.create_account();
    outFile.write(reinterpret_cast<char *> (&ac), sizeof(account));
    outFile.close();
}

//    function to read specific record from file

void display_sp(int n)
{
    account ac;
    bool flag=false;
    ifstream inFile;
    inFile.open("account.dat",ios::binary);
    if(!inFile)
    {
        cout<<"File could not be open !! Press any Key...";
        return;
    }
    cout<<"\nBALANCE DETAILS\n";

        while(inFile.read(reinterpret_cast<char *> (&ac), sizeof(account)))
    {
        if(ac.retacno()==n)
        {
            ac.show_account();
            flag=true;
        }
    }
    inFile.close();
    if(flag==false)
        cout<<"\n\nAccount number does not exist";
}


//    function to display all accounts deposit list

void display_all()
{
    account ac;
    ifstream inFile;
    inFile.open("account.dat",ios::binary);
    if(!inFile)
    {
        cout<<"File could not be open !! Press any Key...";
        return;
    }
    cout<<"\n\n\t\tACCOUNT HOLDER LIST\n\n";
    cout<<"=========================================================================================\n";
    cout<<"A/c no.    NAME          SSN         Address             Total Balance     Interest Rate\n" ;
    cout<<"=========================================================================================\n";

    while(inFile.read(reinterpret_cast<char *> (&ac), sizeof(account)))
    {
        ac.report();
    }
    inFile.close();
}

//   function to deposit and withdraw amounts

void deposit_withdraw(int n, int option)
{
    int amt;
    bool found=false;
    account ac;
    fstream File;
    File.open("account.dat", ios::binary|ios::in|ios::out);
    if(!File)
    {
        cout<<"File could not be open !! Press any Key...";
        return;
    }
    while(!File.eof() && found==false)
    {
        File.read(reinterpret_cast<char *> (&ac), sizeof(account));
        if(ac.retacno()==n)
        {
            ac.show_account();
            if(option==1)
            {
                cout<<"\n\n\tTO DEPOSITE AMOUNT ";
                cout<<"\n\nEnter The amount to be deposited";
                cin>>amt;
                ac.dep(amt);
            }
            if(option==2)
            {
                cout<<"\n\n\tTO WITHDRAW AMOUNT ";
                cout<<"\n\nEnter The amount to be withdraw";
                cin>>amt;
                int bal=ac.retdeposit()-amt;
                if((bal<500 && ac.retssn()=='S') || (bal<1000 && ac.retssn()=='C'))
                    cout<<"Insufficience balance";
                else
                    ac.draw(amt);
            }
            int pos=(-1)*static_cast<int>(sizeof(ac));
            File.seekp(pos,ios::cur);
            File.write(reinterpret_cast<char *> (&ac), sizeof(account));
            cout<<"\n\n\t Record Updated";
            found=true;
           }
         }
    File.close();
    if(found==false)
        cout<<"\n\n Record Not Found ";
}

我该如何解决?

1 个答案:

答案 0 :(得分:1)

就这么简单:

main.cpp:

#include <iostream>
#include <fstream>
#include <cctype>
#include <iomanip>
#include <stdlib.h>

#include "account.h"

using namespace std;

//   function declaration

void write_account();   //function to write record in binary file
void display_sp(int);   //function to display account details given by user
void display_all();     //function to display all account details
void deposit_withdraw(int, int); // function to desposit/withdraw amount for given account

//   Main function

int main()
{
    char ch;
    int num;
    do
    {
        system("cls");
        cout<<"----------------------------------------";
        cout<<"\nWelcome to the First National Bank\n" << "of Parkland - Account transaction system ";
        cout<<"\n----------------------------------------";
        cout<<"\n\n\t01. Create Account";
        cout<<"\n\n\t02. Deposit";
        cout<<"\n\n\t03. Withdraw";
        cout<<"\n\n\t04. Select Account";
        cout<<"\n\n\t05. Print Accounts";
        cout<<"\n\n\t06. Quit";
        cout<<"\n\n\tSelect Your Option (1-6) ";
        cin>>ch;
        system("cls");
        switch(ch)
        {
        case '1':
            write_account();
            break;
        case '2':
            cout<<"\n\n\tEnter The account No. : "; cin>>num;
            deposit_withdraw(num, 1);
            break;
        case '3':
            cout<<"\n\n\tEnter The account No. : "; cin>>num;
            deposit_withdraw(num, 2);
            break;
        case '4':
            cout<<"\n\n\tEnter The account No. : "; cin>>num;
            display_sp(num);
            break;
        case '5':
            display_all();
            break;
        case '6':
            cout<<"\n\n\tThanks for using bank managemnt system";
            break;
        default :cout<<"\a";
        }
        cin.ignore();
        cin.get();
    }while(ch!='8');

    return 0;
}


//    function to write in file

void write_account()
{
    account ac;
    ofstream outFile;
    outFile.open("account.dat",ios::binary|ios::app);
    ac.create_account();
    outFile.write(reinterpret_cast<char *> (&ac), sizeof(account));
    outFile.close();
}

//    function to read specific record from file

void display_sp(int n)
{
    account ac;
    bool flag=false;
    ifstream inFile;
    inFile.open("account.dat",ios::binary);
    if(!inFile)
    {
        cout<<"File could not be open !! Press any Key...";
        return;
    }
    cout<<"\nBALANCE DETAILS\n";

    while(inFile.read(reinterpret_cast<char *> (&ac), sizeof(account)))
    {
        if(ac.retacno()==n)
        {
            ac.show_account();
            flag=true;
        }
    }
    inFile.close();
    if(flag==false)
        cout<<"\n\nAccount number does not exist";
}


//    function to display all accounts deposit list

void display_all()
{
    account ac;
    ifstream inFile;
    inFile.open("account.dat",ios::binary);
    if(!inFile)
    {
        cout<<"File could not be open !! Press any Key...";
        return;
    }
    cout<<"\n\n\t\tACCOUNT HOLDER LIST\n\n";
    cout<<"=========================================================================================\n";
    cout<<"A/c no.    NAME          SSN         Address             Total Balance     Interest Rate\n" ;
    cout<<"=========================================================================================\n";

    while(inFile.read(reinterpret_cast<char *> (&ac), sizeof(account)))
    {
        ac.report();
    }
    inFile.close();
}

//   function to deposit and withdraw amounts

void deposit_withdraw(int n, int option)
{
    int amt;
    bool found=false;
    account ac;
    fstream File;
    File.open("account.dat", ios::binary|ios::in|ios::out);
    if(!File)
    {
        cout<<"File could not be open !! Press any Key...";
        return;
    }
    while(!File.eof() && found==false)
    {
        File.read(reinterpret_cast<char *> (&ac), sizeof(account));
        if(ac.retacno()==n)
        {
            ac.show_account();
            if(option==1)
            {
                cout<<"\n\n\tTO DEPOSITE AMOUNT ";
                cout<<"\n\nEnter The amount to be deposited";
                cin>>amt;
                ac.dep(amt);
            }
            if(option==2)
            {
                cout<<"\n\n\tTO WITHDRAW AMOUNT ";
                cout<<"\n\nEnter The amount to be withdraw";
                cin>>amt;
                int bal=ac.retdeposit()-amt;
                if((bal<500 && ac.retssn()=='S') || (bal<1000 && ac.retssn()=='C'))
                    cout<<"Insufficience balance";
                else
                    ac.draw(amt);
            }
            int pos=(-1)*static_cast<int>(sizeof(ac));
            File.seekp(pos,ios::cur);
            File.write(reinterpret_cast<char *> (&ac), sizeof(account));
            cout<<"\n\n\t Record Updated";
            found=true;
        }
    }
    File.close();
    if(found==false)
        cout<<"\n\n Record Not Found ";
}

account.h:

#pragma once

class account
{
    int acno;
    char name[50];
    int deposit;
    int ssn;
    char address[50];
    int intRate;


public:
    void create_account();  //function to get data from user
    void show_account() const;  //function to show data on screen
    void dep(int);  //function to accept amount and add to balance amount
    void draw(int); //function to accept amount and subtract from balance amount
    void report() const;    //function to show data in tabular format
    int retacno() const;    //function to return account number
    int retdeposit() const; //function to return balance amount
    int retssn() const; //function to return ssn of account
    int retintRate() const;
};

account.cpp:

#include "account.h"

#include <iostream>
#include <iomanip>

using namespace std;

void account::create_account()
{
    cout<<"\nEnter The account No. :";
    cin>>acno;
    cout<<"\n\nEnter The Name of The account holder : ";
    cin.ignore();
    cin.getline(name,50);
    cout<<"\n\nEnter The Address of The account holder : ";
    cin.ignore();
    cin.getline(address,50);
    cout<<"\nEnter SSN of The account : ";
    cin>>ssn;
    ssn=toupper(ssn);
    cout<<"\nEnter The Initial amount(>=500 for Saving and >=1000 for current ) : ";
    cin>>deposit;
    cout<<"\nEnter The Interest Rates : ";
    cin>>intRate;
    cout<<"\n\n\nAccount Created..";
}

void account::show_account() const
{
    cout<<"\nAccount No. : "<<acno;
    cout<<"\nAccount Holder Name : "<<name;
    cout<<"\nAccount Holder's address : "<<address;
    cout<<"\nSSN of Account : "<<ssn;
    cout<<"\nBalance amount : "<<deposit;
    cout<<"\nInterest Rate : "<<intRate;
    cout<<"\nTotal Balance : "<<deposit+deposit*intRate;
}



void account::dep(int x)
{
    deposit+=x;
}

void account::draw(int x)
{
    deposit-=x;
}

void account::report() const
{
    cout<<acno<<setw(8)<<" "<<name<<setw(6)<<" "<<ssn<<setw(6)<<" "<<address<<setw(17)<<" "<<deposit+deposit*intRate/100<<setw(16)<<intRate<<endl;
}


int account::retacno() const
{
    return acno;
}

int account::retdeposit() const
{
    return deposit;
}

int account::retssn() const
{
    return ssn;
}


int account::retintRate() const
{
    return intRate;
}