// Programm für den Gesamtgehalt der MitarbeiterWie gebe ich eine andere Zeichenfolge in demselben Datenelement für ein anderes Objekt in C++ ein?
#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;
}
In dem obigen Code für das Objekt e1 Anzeige der Eingang ist in Ordnung, aber es springt Variable „ename“ für das Objekt „e2“ und Objekt „e3“. Ich löschte den Puffer mit "fflush (stdin);". Was wäre das Problem? Ich verwende g ++ Compiler und Geany-Editor. Es gibt keine Kompilierungsfehler im Code.
fflush ist für Ausgabestrom. Verwenden Sie https://StackOverflow.com/a/34518907/8491726 –
Die Links von der Cin ist wahrscheinlich das Problem nach cin hinzufügen std :: cin.ignore() –
Verwenden Sie std :: string anstelle von Char-Arrays! –