带有指针的字符传递函数

char passing functions with pointers

本文关键字:字符 传递函数 指针      更新时间:2023-10-16

当我用 Turbo C++ 编写这个程序时,它工作正常。但是,当我在 CodeBlocks、Xcode 中编写时,我收到一个关于 char 的错误,我不知道为什么。我想我可以声明类似 char* name 的东西用作字符串。

该程序是关于多级继承的。将利率和期限传递给函数,它将根据帐户类型显示输出。

#include <string>
#include <iostream>
#include <fstream>
using namespace std;
class banktype1{
public:
    char *accountType;
    float interestAmt,depositAmt,period,totalAmt;
public:
    void interestCal(char *x,int y,int z){
        accountType=&x;
        depositAmt=y;
        period=z;
        if(accountType=="A")
           interestAmt=depositAmt*0.5*period;
        else if(accountType=="B")
           interestAmt=depositAmt*0.15*period;
        else if(accountType=="C")
           interestAmt=depositAmt*0.25*period;
    }   
};
class banktype2:public banktype1{
public:
    void displayData(){
        cout<<interestAmt<<"n"<<depositAmt<<endl;
        cout<<"Total"<<interestAmt+depositAmt;
    }
};
class banktype3:public banktype2{
};
int main(){
    banktype3 b1;
    b1.interestCal("A",1000,12);
    b1.interestCal("B",1000,12);
    b1.interestCal("C",1000,12);
    b1.displayData();
    return 0;
}

在调用函数的地方,我收到以下通知:

不推荐从字符串文本转换为字符 *。

以及在我得到的 if 条件的地方:

未指定与字符串文本的比较结果(请改用 strncmp)

使用 std::string 而不是旧的 C 字符串,后者只是字符数组。

请注意,相等运算符只是在 C 字符串的情况下比较数组的地址,但在 std::string 的情况下,它重载以做字符串比较。(所以你的 elseif 代码应该使用 std::string )。

另请注意,使用 C 字符串时,应使用 const char* ,切勿char*

解决方案很简单(请记住,这是一个很好的经验法则):除非在极少数情况下,它是必不可少的,否则始终使用 C++ 功能而不是其 C 等效项,在这种情况下,只需使用 std::string .它旨在让您的生活更轻松。

C++中的字符串文字具有常量字符 arrray 的类型。

但无论如何函数定义

void interestCal(char *x,int y,int z){
    accountType=&x;
    depositAmt=y;
    period=z;
    if(accountType=="A"){
    interestAmt=depositAmt*0.5*period;
    }else if(accountType=="B"){
    interestAmt=depositAmt*0.05*period;
    }else if(accountType=="C"){
    interestAmt=depositAmt*0.05*period;
    }
}

是错误的。

正如编译器警告的那样,如果将字符串文本作为参数传递给函数,则第一个参数应具有类型 const char *

此声明

    accountType=&x;

无效。右操作数&x具有类型 char **,而左操作数具有类型 char *因为帐户类型声明为

char *accountType;

    if(accountType=="A"){

和其他 if else 语句也无效。在这里,您正在尝试比较指针。

如果您将帐户类型定义为

char accountType;

并且该函数看起来像

void interestCal( char x, int y, int z )
{
    accountType = x;
    depositAmt = y;
    period = z;
    if ( accountType == 'A' )
    {
       interestAmt = depositAmt * 0.5 * period;
    }
    else if ( accountType == 'B' )
    {
       interestAmt = depositAmt * 0.05 * period;
    }
    else if( accountType == 'C' )
    {
       interestAmt = depositAmt * 0.05 * period;
    }
}

它将从主调用为

b1.interestCal( 'A', 1000, 12 );
b1.interestCal( 'B', 1000, 12 );
b1.interestCal( 'C', 1000, 12 );

此外,如果三个 if-else 语句确实具有相同的复合语句,那么它们可以重写为一个 if 语句

    if ( accountType == 'A' || accountType == 'B' || accountType == 'C')
    {
       interestAmt = depositAmt * 0.05 * period;
    }

啊,它奏效了。 根据答案进行一些更改后。 删除const . 并将 if(accountType=='A') 更改为 if(*accountType=='A')

#include <string>
#include <iostream>
#include <fstream>
using namespace std;
class banktype1{
public:
    char *accountType;
    float interestAmt,depositAmt,period,totalAmt;
public:
    void interestCal(char x,int y,int z){
        accountType=&x;
        depositAmt=y;
        period=z;
        if(*accountType=='A'){
            interestAmt=depositAmt*0.5*period;
        }else if(*accountType=='B'){
            interestAmt=depositAmt*0.05*period;
        }else if(*accountType=='C'){
            interestAmt=depositAmt*0.05*period;
        }
    }
};
class banktype2:public banktype1{
public:
    void displayData(){
        cout<<interestAmt<<"n"<<depositAmt<<endl;
        cout<<"Total"<<interestAmt+depositAmt;
    }
};
class banktype3:public banktype2{
};
int main(){
    banktype3 b1;
    b1.interestCal('A',1000,12);
    b1.interestCal('B',1000,12);
    b1.interestCal('C',1000,12);
    b1.displayData();
    return 0;
}