Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用结构保存用户C++; < >好的,所以我正在编写一个C++程序来声明一个结构数据类型,该类型的数据包含一个雇员的名字(姓氏、姓氏、ID、工资率和小时)。我的问题是,用户只能输入ID和名字,然后整个程序运行,而不允许用户输入其余的数据_C++_Struct - Fatal编程技术网

使用结构保存用户C++; < >好的,所以我正在编写一个C++程序来声明一个结构数据类型,该类型的数据包含一个雇员的名字(姓氏、姓氏、ID、工资率和小时)。我的问题是,用户只能输入ID和名字,然后整个程序运行,而不允许用户输入其余的数据

使用结构保存用户C++; < >好的,所以我正在编写一个C++程序来声明一个结构数据类型,该类型的数据包含一个雇员的名字(姓氏、姓氏、ID、工资率和小时)。我的问题是,用户只能输入ID和名字,然后整个程序运行,而不允许用户输入其余的数据,c++,struct,C++,Struct,这是我的密码: #include <iostream> #include <iomanip> using namespace std; struct Employee { int employeeID; char firstName; char lastName; float payRate; int hours; }; int main() { int i, j; cout << "How Many

这是我的密码:

 #include <iostream> 
 #include <iomanip> 


    using namespace std;

    struct Employee
{

int employeeID;
char firstName;
char lastName;
float payRate;
int hours;

};



int main()
{
    int i, j;

    cout << "How Many Employees Do You Wish To Enter?:\n\n";
    cin >> j;

   Employee info;

   for (i = 0; i < j; i++)
   {
        cout << "Enter in the Data for Employee number " << i + 1 << endl;

        cout << setw(5) << "\n Please Enter The Employee ID Number: ";
        cin >> info.employeeID;

        cout << setw(5) << "\n Please Enter Employees First Name: ";
        cin >> info.firstName;

        cout << setw(5) << "\n Please Enter Employees Last Name: ";
        cin >> info.lastName;

        cout << setw(5) << "\n Please Enter Employees Pay Rate: ";
        cin >> info.payRate;

        cout << setw(5) << "\n  Please Enter The Hours The Employee Worked: 
";
        cin >> info.hours;


    }

    cout << "\n\n                                   \n";

    cout << "ID" << setw(15) << "First Name" << setw(10) << "Last Name" << 
setw(10) << "Pay Rate" << setw(10) << "Hours";
    cout << endl;

    for (i = 0; i < j; i++)
    {

    cout << "\n" << info.employeeID << setw(15) << info.firstName << setw(10) << info.lastName << setw(10) << info.payRate << setw(10) << info.hours;

}

cout << "\n\n                                    \n";



system("pause");
return 0;



};
#包括
#包括
使用名称空间std;
结构雇员
{
国际雇员ID;
姓名;
姓氏字符;
浮动工资率;
整小时;
};
int main()
{
int i,j;
cout>j;
员工信息;
对于(i=0;i可能有帮助。
以下是对您的代码的一些评论:

首先

使用
string
而不是
char

struct Employee
{
    int employeeID;
    string firstName;
    string lastName;
    float payRate;
    int hours;
};

使用
Employee
对象的数组存储多个雇员

Employee info[100]; 
第三

根据数据类型小心地使用cin。在您的情况下,应该是这样的:

cout << "Enter in the Data for Employee number " << i + 1 << endl;
cout << setw(5) << "\n Please Enter The Employee ID Number: ";
cin >>  info[i].employeeID;
cin.ignore(); //It is placed to ignore new line character.
cout << setw(5) << "\n Please Enter Employees First Name: ";
getline (cin,  info[i].firstName);
cout << setw(5) << "\n Please Enter Employees Last Name: ";
getline (cin,  info[i].lastName);
cout << setw(5) << "\n Please Enter Employees Pay Rate: ";
cin >>  info[i].payRate;
cout << setw(5) << "\n  Please Enter The Hours The Employee Worked: ";
cin >>  info[i].hours;
cout
#包括
#包括
#include//允许您使用字符串,这对于文本操作来说更加方便
#include//允许您使用向量,这意味着要动态地重新划分,这就是您的情况
使用名称空间std;
结构雇员
{
国际雇员ID;
string firstName;//此处:使用string而不是char(string是char的数组)
string lastName;//此处:使用string而不是char
浮动工资率;
整小时;
};
int main()
{
int j;
cout>j;
vector info;//创建向量(动态数组)以存储用户将提供给您的员工信息
对于(inti=0;i员工工作时间;
info.push_back(employee_i);//将员工信息存储到向量中。push_back()方法每次将向量大小扩展1,以便能够将项目放入其中
}//因为您的employee变量是在循环中创建的,所以在这里它将被析构函数,而不是在外部创建的向量

问题是什么?请。您需要重新查看
char
数据类型所包含的内容。我想显示用户在每个类别下输入的信息,比如ID信息在“ID”下,名字在“First Name”下,它不会让我显示数据谢谢@NanBlanc,一切正常,但我有几个问题,我如何编写它,以便程序运行时“ID”和“名字”等都有自己的列?例如,所有输入的ID都显示在“ID”下然后在下一列中,所有的名字都显示在“名字”列下。我的第二个问题是,我如何才能将员工的工资加在一起?将所有工作时间乘以工资率,然后将它们加在一起?
#include <iostream> 
#include <iomanip> 
#include <string> //Allows you to use strings, which are way more handy for text manipulation
#include <vector> //Allows you to use vector which are meant to be rezied dynamicaly, which is your case


using namespace std;

struct Employee
{

    int employeeID;
    string firstName; //HERE : use string instead of char (string are array of char)
    string lastName; //HERE : use string instead of char
    float payRate;
    int hours;

};



int main()
{
    int j; 

    cout << "How Many Employees Do You Wish To Enter?:\n\n";
    cin >> j;

    vector<struct Employee> info; //creation of the vector (dynamic array) to store the employee info the user is going to give you

    for (int i = 0; i < j; i++) //declare your looping iterator "i" here, you will avoid many error 
    {
        struct Employee employee_i; // create an employee at each iteration to associate the current info
        cout << "Enter in the Data for Employee number " << i + 1 << endl;

        cout << "\n Please Enter The Employee ID Number: ";
        cin >> employee_i.employeeID; 

        cout <<  "\n Please Enter Employees First Name: ";
        cin >> employee_i.firstName;

        cout << "\n Please Enter Employees Last Name: ";
        cin >> employee_i.lastName;

        cout << "\n Please Enter Employees Pay Rate: ";
        cin >> employee_i.payRate;

        cout << "\n  Please Enter The Hours The Employee Worked: ";
        cin >> employee_i.hours;

        info.push_back(employee_i); //store that employee info into your vector. Push_back() methods expands the vector size by 1 each time, to be able to put your item in it
    } // because you employee variable was create IN the loop, he will be destruct here, but not the vector which was created outside

    cout << "\n\n                                   \n";

    for (int i = 0; i < j; i++) //the loop to get back all the info from the vector
    {
        cout << "ID :" << info[i].employeeID << "  First Name :" << info[i].firstName << "  Last Name :" <<
            info[i].lastName << "  Pay Rate :" << info[i].payRate << "  Hours :"<< info[i].hours;
        cout << endl;
//notice the info[i], which leads you to the employee you need and the ".hours" which leads to the hours info of that specific employee
        }

        system("pause");
        return 0;
}