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

时间:2019-06-18 11:10:59

标签: c++ loops vector

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

#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

     

输入名称:ball

     

输入年龄:69

     

输入工资:420

     

输入姓名:拉力赛

     

输入年龄:42

     

输入工资:690000

     

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

     

年龄:69

     

工资:420

     

名称:拉力赛

     

年龄:42

     

工资:690000

     

我的输出显示信息

     

名称:拉力赛

     

年龄:42

     

工资:690000

     

名称:拉力赛

     

年龄:42

     

工资:690000

     

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

3 个答案:

答案 0 :(得分:3)

  

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

因为您仅定义了dotnet new react -o boilerplate的一个实例:

System.IO.FileNotFoundException: 'Could not load file or assembly 'System.Runtime, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.'

,然后将您的输入存储到单个Employee中。

您想要更多类似的东西:

Employee Emp;

答案 1 :(得分:1)

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

答案 2 :(得分:1)

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

您认为更合适的容器是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