当我用字符串代替char时,代码可以完美地工作.为什么如此

The code works perfectly when I substitute char with string.why so?

本文关键字:完美 工作 为什么 代码 字符串 char      更新时间:2023-10-16

下面的代码可以很好地处理字符串,但是对于字符,它会产生分段错误。

#include<iostream>
using namespace std;
class salary
{
public:
    int empno;
    float inctax;
    float netsal;
    int gross;
    short int age;
    char name[50];
    salary(){
        empno=0;
        gross=0;
        age=0;
        strcpy(name,'');
    }
    salary(int empn,int gros,short int ag,char nam[]){
        empno=empn;
        gross=gros;
        age=ag;
        strcpy(name,nam);
    }
    void calc(){
        inctax=0.0;
        if(gross>1000000)
            inctax=0.3*gross;
        else if(gross>=500000 && gross<=1000000)
            inctax=0.2*gross;
        else if(gross>=250000 && gross<500000)
            inctax=0.1*gross;
        else
            inctax=0.0;
        netsal=gross-inctax;
        cout<<"inctax"<<inctax;
        cout<<"net sal"<<netsal;
    }
};
int main(){
    salary *r=new salary();
    salary *r1=new salary(112,500000,21,"Arnab");
    r1->calc();
    return 0;
}

我同意vu1p3n0x的评论,问题出在默认构造函数中。

strcpy(name, ''); // This is wrong!

strcpy()接受两个字符数组作为参数,但在代码中,字符本身作为第二个参数传递。strcpy()的语法是:

char * strcpy ( char * destination, const char * source );

为了使用char数组创建空字符串,您可能应该使用

strcpy(name, "");
     /*or*/ 
name[0] = '';

更多关于复制string, Reference和tutorialpoint

您的主要问题是编译器没有告诉您代码中明显的错误。如果您已经正确地告诉它报告错误,那么您可能需要一个更好的编译器。下面是我编译时得到的输出:

g++ -std=c++14 -fPIC -g -Wall -Wextra -Wwrite-strings -Wno-parentheses     38113648.cpp   -o 38113648
38113648.cpp: In constructor ‘salary::salary()’:
38113648.cpp:18:25: error: ‘strcpy’ was not declared in this scope
         strcpy(name,'');
                         ^
38113648.cpp: In constructor ‘salary::salary(int, int, short int, char*)’:
38113648.cpp:25:24: error: ‘strcpy’ was not declared in this scope
         strcpy(name,nam);
                        ^
38113648.cpp: In function ‘int main()’:
38113648.cpp:46:48: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
     salary *r1=new salary(112,500000,21,"Arnab");
                                                ^
38113648.cpp:45:13: warning: unused variable ‘r’ [-Wunused-variable]
     salary *r=new salary();
             ^

添加

#include <cstring>

并将构造函数改为取char const[],得到

g++ -std=c++14 -fPIC -g -Wall -Wextra -Wwrite-strings -Wno-parentheses     38113648.cpp   -o 38113648
38113648.cpp: In constructor ‘salary::salary()’:
38113648.cpp:19:25: warning: null argument where non-null required (argument 2) [-Wnonnull]
         strcpy(name,'');
                     ^

显然你的意思是 "" 。更好的是,提供一个初始化式:

salary()
    : empno{0},
      gross{0},
      age{0},
      name{0}
{
}
salary(int empno, int gross, short int age, char const name[])
    : empno{empno},
      gross{gross},
      age{age}
{
    strcpy(this->name, name);
}

(我还给了形式参数更有意义的名字,因为这通常构成构造函数的文档)。

添加-Weffc++也可能是值得的——在这种情况下,它警告你不要在构造函数中初始化inctaxnetsal。如果您乐于使用部分初始化的对象,那么显然需要使用Valgrind来检查这些值在使用之前是否确实设置了,因为它不能静态地确定。