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

标签 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;
}

The expected outcome is like

Inputs by the user***

Enter the no of information to be stored: 2

Enter Name:ball

Enter age:69

Enter wage:420

Enter Name:Rally

Enter age:42

Enter wage:690000

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

age:69

wage:420

Name:Rally

age:42

wage:690000

My output Displaying Information

Name:Rally

age:42

wage:690000

Name:Rally

age:42

wage:690000

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

最佳答案

So basically my program outputs the final set of information received

因为你只定义了一个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);

关于c++ - 我确实需要一些帮助来创建显示信息的循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56647789/

相关文章:

node.js - http.get请求循环关闭问题(Node)

c++ - 如何知道何时使用 CCdataType* varName 与 CCdataType *pVarName

c++ - 意外的 std::io_base::failure 异常

c++ - Boost timed_wait闰秒问题

ruby-on-rails - 如何使用 Ruby "Right Way!"创建嵌套循环?

javascript - For循环和2个数组

c++ - 如何打开特定 url 上的套接字?

C++ vector<vector<double>> 加倍 **

math - 确定一些向量有何不同

c++ - 为什么 vector 不能用这个拷贝构造函数插入类?