这个程序不会从用户那里获取更多的信息,只是运行而不获取信息,并在获取用户信息的空白处显示随机字母,并idk要做什么
using namespace std;
struct employee
{
char name[20];
int no;
char duty;
float salary_per_day;
};
int main()
{
struct employee b[10];
int i;
cout<<"enter the details of the employee :"<<endl;
for(i=0;i<3;++i)
{
b[i];
cout<<"employees name :"<<b[i].name<<endl;
cout<<"enter name =";
cin>>b[i].name;
cout<<"employees number:"<<b[i].no<<endl;
cout<<"enter number =";
cin>>b[i].no;
cout<<"employees duty:"<<b[i].duty<<endl;
cout<<"enter duty = ";
cin>>b[i].duty;
cout<<"employees salary:"<<b[i].salary_per_day<<endl;
cout<<"enter salary = ";
cin>>b[i].salary_per_day;
}
cout<<"display info of employees:"<<endl;
for(i=0;i<3;++i)
{
cout<<endl<<"info of employees:";
cout<<endl<<"name:"<<b[i].name;
cout<<endl<<"number:"<<b[i].no;
cout<<endl<<"duty:"<<b[i].duty;
cout<<endl<<"salary:"<<b[i].salary_per_day;
}
在此代码中
cout<<"employees name :"<<b[i].name<<endl;
cout<<"enter name =";
cin>>b[i].name;
在用户输入名称之前,您正在打印该名称,这是没有意义的。在用户输入名称后打印该名称,如下所示
cout<<"enter name =";
cin>>b[i].name;
cout<<"employees name :"<<b[i].name<<endl;
还有这个代码
b[i];
什么都不做。它无害但毫无意义,移开它。
除了John所说的以外,还有一个问题很可能是您输入的名称带有空格'
,比如John Doe
。std::cin
的运算符>>
读取到第一个空格。这意味着只有john
作为名称读入,然后doe
尝试将其转换为int
,然后将其放入b[i].no
中。
要解决这个问题,您需要阅读直到遇到\n
,或者使用std::getline
你知道吗
cout << "employees name :" << b[i].name << endl;
在你读过名字之前。请检查您的代码中是否有类似的错误。