2016-03-08 37 views
-2

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

#ifndef ACCOUNT_H 
     #define ACCOUNT_H 
     class Account { 

     public: 
     Account(char* ='\0',char='S',char* ='\0',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]='\0'; 
     type='\0'; 
     owner[0]='\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:'\0'); 
     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:'\0'); 

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; 
    } 
+1

使用'std :: string'。 –

回答

2

一些C-标准库仍处于C++技术上可行,但至少由一些编译器来阻止它们的使用。您有几种选择:

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

  2. 将strncpy替换为更安全的替代方法。

  3. 使用std :: string代替原始字符串。这是BY FAR提供给您的最佳选择。

+0

好的谢谢,但我有另一个错误是致命的错误LNK1104:/ – Caroline

+0

你可以谷歌的那种问题。我这样做,并发现这一点:http://stackoverflow.com/questions/133698/why-does-fatal-error-lnk1104-cannot-open-file-c-program-obj-occur-when-ic –