且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我确实需要一些帮助来创建一个显示信息的循环

更新时间:2023-11-20 16:12:46

您的代码中只有一个Employee实例.将两个循环合并为一个:

There is only a single Employee instance in your code. Either merge the two loops into one:

for (int i = 0; i < sized; i++)
{
    cout << "Enter Full name of employee: ";
    cin.ignore();
    getline(cin, Emp.name);
    cout << endl;
    cout << "Enter age of employee: ";
    cin >> Emp.age;
    cout << endl;
    cout << "Enter salary of employee: ";
    cin >> Emp.salary;
    cout << endl;
    system("cls");
    // To display the elements of the information given
    cout << endl << "Displaying Information." << endl;
    cout << "--------------------------------" << endl;
    Showinfo(Emp);
}

但是,这会将用户输入与输出交织在一起.相反,您可以使用向量:

However, this will interleave user input with the output. Instead you can use a vector:

std::vector<Employee> employees;
for (int i = 0; i < sized; i++)
{
    cout << "Enter Full name of employee: ";
    cin.ignore();
    getline(cin, Emp.name);
    cout << endl;
    cout << "Enter age of employee: ";
    cin >> Emp.age;
    cout << endl;
    cout << "Enter salary of employee: ";
    cin >> Emp.salary;
    cout << endl;
    employees.push_back(Emp);
    system("cls");
}

for (const auto& e : employess) {
   Showinfo(e);
}