如何调试这个C++类出了什么问题?
How to debug what's wrong with this C++ class?
#include<iostream>
using namespace std;
class emp
{
public:
int en;
char name[10],des[10];
void get()
{
cout<<"enter emp no.";
cin>>en;
cout<<"enter emp name";
cin>>name;
cout<<"enter designation";
cin>>des;
}
};
class salary : public emp
{
public:
float bp,hra,da,pf,np;
void get1()
{
cout<<"enter basic pay";
cin>>bp;
cout<<"enter domestic allowance";
cin>>da;
cout<<"enter profit fund";
cin>>pf;
cout<<"enter human resource admittance";
cin>>hra;
}
void calculate()
{
np=bp+da+hra-pf;
}
void display()
{
cout<<en<<"t"<<name<<"t"<<des<<"t"<<da<<"t"<<pf<<"t"<<np<<"n";
}
};
int main()
{
salary s[10];
int i,n;
char ch;
cout<<"enter the no. of employees";
cin>>n;
for(i=0;i<=n;i++)
{
s[i].get();
s[i].get1();
s[i].calculate();
}
cout<<"n eno. t ename t des t bp t hra t da t pf t np n";
for(i=0;i<=n;i++)
{
s[i].display();
}
return 0;
}
cin>>des[10];
读取标准输入的一个(单个)字符,并尝试将其写入des[10]
。不幸的是,您将des
定义为具有10个字符,因此只有des[0]
至des[9]
是有效的,因此当它尝试写入des[10]
时,您会得到未定义的行为。
我的猜测是您可能想要更多类似的东西:
cin.getline(des, 10);
这试图从cin
中读取10个字符的最多10个字符,并将其写入des
(并确保其nul终止)。
当然,同样的是name
。
完成此操作后,您可能想忘记上述所有内容,将name
和des
同时定义为std::string
s。然后,您可以使用std::getline(std::cin, name);
。因此,您不必指定最大尺寸;该字符串将扩展到用户输入的数量。
而不是名称[10]和des [10],使用cin>>名称和cin>> des
相关文章:
- 警告处理为错误这里有什么问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 当我尝试添加 2 个大字符串时,我无法弄清楚出了什么问题
- 违反const正确性:我应该现实地期待什么问题
- 这个带有模板<类 Vector 的C++代码片段有什么问题>
- 我的逻辑反转字符串中的元音有什么问题?
- 需要以下代码的帮助,下面的代码有什么问题
- 常量公共成员有什么问题?
- 以下代码中的函数模板有什么问题?
- 这个返回元素位置的基于循环的函数有什么问题?
- creat_list2功能有什么问题?
- 格式说明符C++有什么问题
- 任何人都可以告诉我我的 C++ 代码出了什么问题?
- 从 argv[1] 转换为字符 * 字符串后有什么问题?
- 我的堆栈和库存清单程序的结构有什么问题?
- 此工厂功能有什么问题?
- 以下 C++ 代码有什么问题?
- 数组为此合并排序函数提供了正确的输出,但向量给出了不正确的输出.出了什么问题?
- reinterpret_cast,只读访问,简单的可复制类型,会出什么问题?
- 它解决了什么问题,对于非真空初始化,生命周期在初始化之前就开始了