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
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
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);
}
So basically my program outputs the final set of information received
Because you only define one instance of Employee
:
Employee Emp;
and then store your input into that single Emp
.
You want something more like:
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);
In fact you need a container with a variable size to store all entered employees.
The more appropriate container in your case is std::vector
.
Here is a demonstrative program,
#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;
}
Its output might look like
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