我确实需要一些帮助来创建显示信息的循环
I do need some assistance with a creating a loop that displays the information
我正在尝试输出提供的所有信息,但我只能输出输入一组时间的最终输出。我对循环很陌生
#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
相关文章:
- 创建具有两个视口的Qt3D C++场景,其中显示适当的不同网格
- 创建具有验证和语法突出显示的自定义 printf
- 创建第二个应用窗口并显示它
- 如何从2D数组为QHeightMapSurfaceDataProxy创建高度图以显示2D傅立叶变换结果
- 显示 C++ 中"anonymous"变量创建的警告
- 创建直方图以显示每个类别/箱中带有星号的项目数量
- 使用带有诅咒库的 stdscr 创建窗口,在边框而不是线条中显示不需要的笑脸字符
- 通过显示一行星号来创建条形图,该条形图将读取数据的输入文件
- c++:如何创建收据,显示所选商品及其数量、价格和总价
- 如何创建一个递归函数来显示有多少元音具有输入
- 如何显示我在第一个函数中已经创建的文件的内容
- 如何创建跟踪以显示存在递归的调用顺序
- 我确实需要一些帮助来创建显示信息的循环
- 是否有一个函数可以显示我创建的类中的对象到 QTextBrowser 中?
- 哪些更好的方法可以显示使用C STL创建的堆栈而不弹出每个元素的方法
- 尝试创建一个读取.txt文件,显示它,计数唯一单词的程序,并在使用了多少次的情况下显示独特的单词.C
- 如何显示在堆上创建的内存的地址
- 为什么MessageBox在我创建的桌面中显示窗口,而AllocConsole在上一个桌面中显示它
- 在创建显示索引如何在另一个数组中移动的数组时遇到问题
- 从而创建显示功能