类帐户输出

Class Account -output

本文关键字:输出      更新时间:2023-10-16

我的程序是关于创建一个类帐户,我需要创建3个帐户,其中包括帐号,类型,所有者姓名,余额和利率。 并创建一个用于计算每月利息的函数。我想知道我的代码出了什么问题,因为我得到的输出不正确

   #ifndef ACCOUNT_H
        #define ACCOUNT_H
        class Account {
        public:
        Account(char* ='',char='S',char* ='',double=0.0,double=0.05);
        ~Account();
        void setaccounts(char*,char,char*,double,double);
        void setBalance(double);
        void setInterestRate(double);
        double getBalance(void);
        double getInterestRate(void);
        void monthlyInterest();
        void printAccounts();
        private:
        char AccountNumber[7];
        char type;
        char owner[30];
        double balance;
        double interestRate;
        };
        #endif
   #include<iostream>
    using std::cout;
    using std::endl;
#include<cstring>
    #include"Account.h"
    Account::Account(char* accountnumber,char typee,char* Owner,double Balance,double Interestrate){
        AccountNumber[0]='';
        type='';
        owner[0]='';
        balance=0.0;
        interestRate=0.0;
    }
    Account::~Account(){
    }

    void Account::setaccounts(char* accountnumber1,char typee1,char* Owner1,double Balance1,double Interestrate1){
int length=strlen(accountnumber1);
length=(length==7?length:'');
       type=typee1;
        switch(typee1)
        {
        case 1:
            if(typee1=='S')
                cout<<"Saving accounts"<<endl;
                break;
        case 2:
            if(typee1=='C')
                cout<<"Checking accounts"<<endl;
                break;
        case 3:
            if(typee1=='L')
                cout<<"Loans accounts"<<endl;
                break;
        default:
            cout<<'S'<<endl;
        }
int length2=strlen(Owner1);
length2=(length2<30?length2:'');
balance=Balance1;
interestRate=Interestrate1;
    }
    void Account::setBalance(double bal){
    balance=bal>=0?bal:0;
        }
    double Account::getBalance(){
        return balance;
    }
     void Account::setInterestRate(double inter){
    interestRate=(inter>0 && inter<1)?inter:0.05;
        }
    double Account::getInterestRate(){
        return interestRate;
    }
    void  Account::monthlyInterest(){
     balance+=(balance*interestRate)/12;
    }
        void Account::printAccounts(){
        cout<<"The account number: "<<AccountNumber<<endl;
        cout<<"The account's type: "<<type<<endl;
        cout<<"The name of the account's owner"<<owner<<endl;
        cout<<"The balance-amount of money in USD currently existing in the account: "<<balance<<endl;
        cout<<"The annnual interest rate is :"<<interestRate<<endl;
        cout<<"The monthly interest is: "<<balance<<endl;
    }

    int main()
    {
        Account Acc, arrays[3],*theseaccounts;
        arrays[0].setaccounts("LAM1234",'C',"Sam Smith",5.3,0.6);                                                 
        arrays[1].setaccounts("JDM2345",'L',"Mark Wayne",4.3,0.2);
        arrays[2].setaccounts("HWN9342",'S',"Drake Bell",2.3,0.9);
        theseaccounts=&arrays[3];
        theseaccounts->printAccounts();
        for(int i=0;i<3;i++){
            arrays[i].printAccounts();
        }

    system("pause");
        return 0;
    }

许多 C 标准库在技术上仍然在C++中可用,但至少一些编译器不鼓励使用它们。您有以下几种选择:

  1. 关闭警告。不是最好的选择,但你正在做的是合法C++。

  2. 用更安全的替代方案替换 strncpy。

  3. 使用 std::string 而不是原始字符串。这是迄今为止为您提供的最佳选择。