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

问题描述:

我正在尝试输出所提供的所有信息,但我只能输出经过一定次数输入的最终输出.对于循环,我还是很新的

I am trying to output the all of the information provided but i can only output the final output entered a set of times.I am pretty new to loops

#include <iostream>
#include <string>
using namespace std;

int sized = 0;

 //Global array declaration 
 Employee Array [60]:

//Struct declaration
struct Employee
{
    string name;
    int age;
    double salary;
};

//Protype
void Showinfo(Employee);

int main()
{
    //Declaring a variable of type Employee
    Employee Emp;

    cout << "Enter the number of employees you want to enter into the database: ";
    cin >> sized;
    cout << endl << endl;
    system("cls");

    //Getting the name of the Employee
    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;

    for (int i = 0; i < sized; i++)
    {
        Showinfo(Emp);
    }

    cin.ignore();
    return 0;
}

//To display/showcase the information received
void Showinfo(Employee Emp)
{
    cout << "Name: " << Emp.name << endl;
    cout << "Age: " << Emp.age << endl;
    cout << "Salary: " << Emp.salary << endl << endl;
}

预期结果类似于

The expected outcome is like

用户输入***

输入要存储的信息编号:2

Enter the no of information to be stored: 2

输入姓名:ball

输入年龄:69

输入工资:420

输入姓名:拉力赛

输入年龄:42

输入工资:690000

Enter wage:690000

预期输出:显示信息 -------------------------名称:ball

Expected output: Displaying information ------------------------- Name:ball

年龄:69

工资:420

名称:拉力赛

年龄:42

工资:690000

我的输出显示信息

名称:拉力赛

年龄:42

工资:690000

名称:拉力赛

年龄:42

工资:690000

所以基本上我的程序输出收到的最后一组信息 *重复次数

So basically my program outputs the final set of information received * Sized number of times

您的代码中只有一个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);
}