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

I do need some assistance with a creating a loop that displays the information

本文关键字:创建 显示 信息 循环 帮助      更新时间:2023-10-16

我正在尝试输出提供的所有信息,但我只能输出输入一组时间的最终输出。我对循环很陌生

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

预期的结果是这样的

用户输入***

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

输入名称:球

输入年龄:69

输入工资:420

输入名称:拉力赛

输入年龄:42

输入工资:690000

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

年龄:69

工资:420

名称:拉力赛

年龄:42

工资:690000

我的输出显示信息

名称:拉力赛

年龄:42

工资:690000

名称:拉力赛

年龄:42

工资:690000

所以基本上我的程序输出收到的最终信息集 * 尺寸次数

所以基本上我的程序输出收到的最终信息集

因为您只定义了Employee的一个实例:

Employee Emp;

然后将您的输入存储到该单个Emp中。

你想要更像:

cout << "Enter the number of employees you want to enter into the database: ";
cin >> sized;
//Declaring a vector type Employee of size sized
std::vector<Employee> Emps(sized);

代码中只有一个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;
    Showinfo(Emp);
}

但是,这会将用户输入与输出交错。相反,您可以使用向量:

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);
}

实际上,您需要一个大小可变的容器来存储所有输入的员工。

在您的情况下,更合适的容器是 std::vector .

这是一个示范性程序,

#include <iostream>
#include <string>
#include <iterator>
#include <vector>
struct Employee
{
    std::string name;
    int age;
    double salary;
};
std::ostream & Showinfo( const Employee &employee, std::ostream &os = std::cout );
int main() 
{
    size_t n = 0;
    std::cout << "Enter the number of employees you want to enter into the database: ";
    std::cin >> n;
    std::cout << 'n';
    std::vector<Employee> employees;
    employees.reserve( n );
    for ( size_t i = 0; i < n; i++ )
    {
        Employee emp;
        std::cout << "Enter Full name of employee: ";
        std::cin.ignore();
        std::getline( std::cin, emp.name );
        std::cout << 'n';
        std::cout << "Enter age of employee: ";
        std::cin >> emp.age;
        std::cout << 'n';
        std::cout << "Enter salary of employee: ";
        std::cin >> emp.salary;
        std::cout << 'n';
        employees.push_back( emp );
    }    
    // To display the elements of the information given
    std::cout << "nDisplaying Information.n";
    std::cout << "--------------------------------n";
    for ( const Employee &emp : employees ) Showinfo( emp );
    return 0;
}
//To display/showcase the information received
std::ostream & Showinfo( const Employee &emp, std::ostream &os )
{
    os << "Name: "   << emp.name   << 'n';
    os << "Age: "    << emp.age    << 'n';
    os << "Salary: " << emp.salary << 'n';
    return os << std::endl;
}

它的输出可能看起来像

Enter the number of employees you want to enter into the database: 2
Enter Full name of employee: ball
Enter age of employee: 69
Enter salary of employee: 420
Enter Full name of employee: Rally
Enter age of employee: 42
Enter salary of employee: 690000

Displaying Information.
--------------------------------
Name: ball
Age: 69
Salary: 420
Name: Rally
Age: 42
Salary: 690000