Question

When I write this program in Turbo C++, it works fine. However, when I write in CodeBlocks, Xcode, I get an error regarding char, and I don't know why. I thought I could just declare something like char* name to use as a string.

This program is about Multilevel Inheritance. Passing Interest rate and Period to the functions, it will display the output according to the Account type.`

#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;
}

At the place of calling functions, I'm getting this notification:

conversion from string literal to char * is deprecated.

and also at the place of if condition I'm getting:

Result of comparison against a string literal is unspecified (use strncmp instead)

Was it helpful?

Solution 2

String literals in C++ have types of constant charater arrrays.

But in any case the function definition

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

is wrong.

As the compiler warns the first parameter should have type const char * if you pass string literals as arguments to the function.

This statement

    accountType=&x;

is invalid. Thje right operand &x has type char ** while the left operand has type char * because accountType is declared as

char *accountType;

This

    if(accountType=="A"){

and other if else statements are also invalid. Here you are tryying to compare pointers.

It would be much simpler and more correctly if you defined accountType as

char accountType;

and the function would look as

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

and it would be called from main as

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

Also if indeed the three if-else statements have the same compound statement then they could be rewritten as one if statement

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

OTHER TIPS

Use std::string instead of the old C strings, which are just arrays of characters.

Note that the equality operator just compares the addresses of the arrays in the case of C strings, but its overloaded to do string comparison in the case of std::string. (So your elseif code should use std::string).

Also note that when using C strings, you should use const char*, never char*.

The solution is simple (And remember it as a good rule of thumb): Except in rare cases where its neccesary, always use C++ features instead of its C equivalents, in this case just use std::string. Its dessigned to make your life easier.

Ah It Worked. after making some changes according to answers. deleting const . and changing if(accountType=='A') to 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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top