使用结构来保存用户 C++ 输入的信息 [英] Using a struct to hold information entered by the user C++

查看:46
本文介绍了使用结构来保存用户 C++ 输入的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我正在编写一个 C++ 程序来声明一个结构数据类型,该数据类型保存有关员工的以下信息(名字、姓氏、ID、工资率和工作时间).我的问题是用户只能输入 ID 和名字,然后整个程序运行而不让用户输入其余数据.

Okay, so I am writing a C++ program to declare a struct data type that holds the following information on an employee (First Name, Last Name, ID, Pay Rate, and Hours). My problem is that the user can only enter in the ID and First Name, then the whole program runs without letting the user enter the rest of the data.

这是我的代码:

 #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;



};

推荐答案

首先请阅读使用 C++ I/O(输入/输出)的提示和技巧.了解 C++ I/O 可能会有所帮助.

First, please read Tips and tricks for using C++ I/O (input/output). It might be helpful to understand C++ I/O.

以下是对您的代码的一些评论:

Here are some comments on your code:

第一

使用string代替char.

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

第二

使用 Employee 对象数组来存储多个员工.

Use array of Employee object to store multiple employees.

Employee info[100]; 

第三

根据数据类型谨慎使用 cin.在你的情况下,它会是这样的:

Use cin carefully depending data types. In your case, it would be something like this:

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;

第四

std::getline()std::cin >> 之前使用时可能会遇到问题变量.因此,在这种情况下可以使用 std::cin.ignore() 来解决问题.

std::getline() can run into problems when used before std::cin >> var. So, std::cin.ignore() can be used in this case to solve the problem.

希望能帮到你.

这篇关于使用结构来保存用户 C++ 输入的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆