如何在 C++ 中为另一个对象在同一数据成员中输入另一个字符串?
how do i input another string in same data member for another object in c++?
//显示员工工资总额的程序
#include <iostream>
#include <cstdio>
using namespace std;
[enter image description here][1]
class employee
{
char ename[25];
int emp_id, basic_sal;
float da, hra, total;
public:
int input();
int calculate();
int output();
};
int main()
{
employee e1;
employee e2;
employee e3;
cout <<"\n Enter details of first employee\n";
e1.input();
e1.calculate();
cout <<"\n Enter details of Seond employee\n";
e2.input();
//fflush(stdin);
e2.calculate();
cout <<"\n Enter details of Third employee\n";
e3.input();
//fflush(stdin);
e3.calculate();
e1.output();
e2.output();
e3.output();
return 0;
}
int employee::input()
{
cout <<"Enter the name of employee\n";
cin.clear();
fflush(stdin);
cin.getline(ename, 49);
cout <<"Enter employee id\n";
cin >>emp_id;
cout <<"Enter the basic salary\n";
cin >>basic_sal;
return 0;
}
int employee::calculate()
{
int pda, phra;
cout <<"Enter DA (percentage)\n";
cin >>pda;
cout <<"Enter HRA (Percentage)\n";
cin >>phra;
da = (basic_sal*pda)/100;
hra = (basic_sal*phra)/100;
total = basic_sal + da + hra;
return 0;
}
int employee::output()
{
cout <<"Name of Employee - " <<ename <<"\n" <<"Employee ID - ";
cout <<emp_id <<"\n" <<"Basic Salary - " <<basic_sal <<"\n";
cout <<"Da - " <<da <<"\n" <<"hra - " <<hra << "\n";
cout <<"Total Salary - " <<total <<"\n\n\n";
return 0;
}
在上述对象 e1 的代码中,输入没有问题,但它跳过对象 "e2" 和对象 "e3" 的变量 "ename"。我使用 "fflush(stdin);" 清除了缓冲区。会有什么问题?
我正在使用 g++ 编译器和 geany 编辑器。
代码中没有编译错误。
是否在新行输入了每位员工的详细信息?如果是这样,您必须使用类似
cin >> ws;
在 employee::input()
函数的开头。它将跳过所有空格,直到找到第一个非空格字符(保持原样)。
现在,cin >> phra
从 stdin
加载一些数字(我假设是行中的最后一个数字),但不消耗它后面的换行符。因此,它留在那里供 employee::input()
中的第一个 cin.getline()
调用,它将只读取这个换行符,因此 ename
将为空。
//显示员工工资总额的程序
#include <iostream>
#include <cstdio>
using namespace std;
[enter image description here][1]
class employee
{
char ename[25];
int emp_id, basic_sal;
float da, hra, total;
public:
int input();
int calculate();
int output();
};
int main()
{
employee e1;
employee e2;
employee e3;
cout <<"\n Enter details of first employee\n";
e1.input();
e1.calculate();
cout <<"\n Enter details of Seond employee\n";
e2.input();
//fflush(stdin);
e2.calculate();
cout <<"\n Enter details of Third employee\n";
e3.input();
//fflush(stdin);
e3.calculate();
e1.output();
e2.output();
e3.output();
return 0;
}
int employee::input()
{
cout <<"Enter the name of employee\n";
cin.clear();
fflush(stdin);
cin.getline(ename, 49);
cout <<"Enter employee id\n";
cin >>emp_id;
cout <<"Enter the basic salary\n";
cin >>basic_sal;
return 0;
}
int employee::calculate()
{
int pda, phra;
cout <<"Enter DA (percentage)\n";
cin >>pda;
cout <<"Enter HRA (Percentage)\n";
cin >>phra;
da = (basic_sal*pda)/100;
hra = (basic_sal*phra)/100;
total = basic_sal + da + hra;
return 0;
}
int employee::output()
{
cout <<"Name of Employee - " <<ename <<"\n" <<"Employee ID - ";
cout <<emp_id <<"\n" <<"Basic Salary - " <<basic_sal <<"\n";
cout <<"Da - " <<da <<"\n" <<"hra - " <<hra << "\n";
cout <<"Total Salary - " <<total <<"\n\n\n";
return 0;
}
在上述对象 e1 的代码中,输入没有问题,但它跳过对象 "e2" 和对象 "e3" 的变量 "ename"。我使用 "fflush(stdin);" 清除了缓冲区。会有什么问题? 我正在使用 g++ 编译器和 geany 编辑器。 代码中没有编译错误。
是否在新行输入了每位员工的详细信息?如果是这样,您必须使用类似
cin >> ws;
在 employee::input()
函数的开头。它将跳过所有空格,直到找到第一个非空格字符(保持原样)。
现在,cin >> phra
从 stdin
加载一些数字(我假设是行中的最后一个数字),但不消耗它后面的换行符。因此,它留在那里供 employee::input()
中的第一个 cin.getline()
调用,它将只读取这个换行符,因此 ename
将为空。