Source Code:
#include <iostream>
using namespace std;
struct Employee {
string name;
int age;
float salary;
};
void displayData(Employee);
int main() {
Employee employee[3];
int sizeOfEmployee = sizeof(employee) / sizeof(employee[0]);
for(int i = 0; i < sizeOfEmployee; i++) {
cout << "Enter name for employee " << i + 1 << ": ";
cin >> employee->name;
cout << "Enter age for employee " << i + 1 << ": ";
cin >> employee->age;
cout << "Enter salary for employee " << i + 1 << ": ";
cin >> employee->salary;
}
for(int i = 0; i < sizeOfEmployee; i++) {
displayData(employee[i]);
}
}
void displayData(Employee employee) {
cout << "DISPLAYING INFORMATION" << endl;
cout << "Name: " << employee.name << endl;
cout << "Age: " << employee.age << endl;
cout << "Salary: " << employee.salary << endl;
}
My code doesn't display all the data i've stored into the array, it only reads and display the last data i've inputted.
CODE OUTPUT:
In what way could i possibly get all the data i've stored into the array and display it?
You are reading data into a pointer to the first element of the array. To read into n-th element your code should be using indexing, like cin >> employee[i].name;<\/code>
.
Consider using
std::array<\/code> instead - this will eliminate the need for code like
int sizeOfEmployee = sizeof(...<\/code> and will nicely encapsulate that array as a parameter to a function:
#include <array>
int main() {
std::array<Employee, 3> employee;
for(int i = 0; i < employee.size(); i++) {
cout << "Enter name for employee " << i + 1 << ": ";
cin >> employee[i].name;
// the rest of that for loop...
}
// and the second loop becomes:
for(const auto& e: employee) {
displayData(e);
}
You are reading data 3 times into the first element in your array:
Employee employee[3];
for(int i = 0; i < sizeOfEmployee; i++) {
cout << "Enter name for employee " << i + 1 << ": ";
cin >> employee->name; // employee points to the first element in the array
}
One more comment: your are passing the Employee
class by value just to display it. It would be mych better, efficient and robust to pass it by const reference:
void displayData(const Employee&);
int sizeOfEmployee = sizeof(employee) / sizeof(employee[0]);
Don't do this. Use std::size(employee)
if you want to get the length of the array.
for(int i = 0; i < sizeOfEmployee; i++) {
Don't do this. Instead, use a range-for to loop over all elements of a range:
for (auto&& e : employee)
Which incidentally will fix the bug that you had. The bug is that you always write to the first element of the array. Each iteration over-writes the previous ones and the other elements remain uninitialised.
The range-for will correctly iterate all elements with less chance of messing things up.
If you want to input a variable number of employees, then you should use the STL container std::vector<\/code> .
Please have a look at its documentation<\/a> .
#include <iostream>
#include <vector> //CKE: Very powerful STL container for arrays
using namespace std;
struct Employee {
string name;
int age = INT_MIN; //CKE: Always initialize variables in C++
float salary = 0.; //CKE: Always initialize variables in C++
};
void displayData(const Employee&); //CKE: Use const reference if possible
int main() {
vector<Employee> employees; //CKE: Declaration of dynamic array in C++
size_t counter = 0; //CKE: Counts number of given data sets
string addOneMore; //CKE: Holds check for more input data
do {
++counter;
Employee employee;
cout << "Enter name for employee " << counter << ": ";
cin >> employee.name;
cout << "Enter age for employee " << counter << ": ";
cin >> employee.age;
cout << "Enter salary for employee " << counter << ": ";
cin >> employee.salary;
employees.push_back(employee); //CKE: Save data in dynamic array
cout << "Want to add another employee? (y/n): ";
cin >> addOneMore;
} while (addOneMore == "y");
for (const auto& employee : employees) { //CKE: Use range base loop for output
displayData(employee);
}
return 0; //CKE: Always return an integer in int main function of C++
}
void displayData(const Employee& employee) { //CKE: No copying of struct Employee
cout << "DISPLAYING INFORMATION" << endl;
cout << "Name: " << employee.name << endl;
cout << "Age: " << employee.age << endl;
cout << "Salary: " << employee.salary << endl;
}
I simply fixed my problem by using loop counter:
#include <iostream>
using namespace std;
struct Employee {
string name;
int age;
float salary;
};
void displayData(Employee[], int);
int main() {
Employee employee[3];
int sizeOfEmployee = sizeof(employee) / sizeof(employee[0]);
for(int i = 0; i < sizeOfEmployee; i++) {
cout << "Enter name for employee " << i + 1 << ": ";
getline(cin, employee[i].name);
cout << "Enter age for employee " << i + 1 << ": ";
cin >> employee[i].age;
cout << "Enter salary for employee " << i + 1 << ": ";
cin >> employee[i].salary;
cin.ignore();
}
displayData(employee, sizeOfEmployee);
}
void displayData(Employee employee[], int sizeOfEmployee) {
cout << "DISPLAYING INFORMATION" << endl;
for(int i = 0; i < sizeOfEmployee; i++) {
cout << "Name: " << employee[i].name << endl;
cout << "Age: " << employee[i].age << endl;
cout << "Salary: " << employee[i].salary << endl;
}
}
Thanks to all the people who helped me and provided me with answers. Very much appreciated as I'm still learning:))
One way is not to use a "C" style arrays at all, but to use std::vector (or std::array).
#include <iostream>
#include <string>
#include <vector>
// using namespace std; do NOT use "using namespace"
struct Employee
{
std::string name;
int age; // consider unsigned type (age cannot be <0)
float salary; // consider double
};
// pass an Employee by const reference,
// const : display should NOT change the content,
// reference : the reference avoids copying of data
void displayData(const Employee& employee)
{
std::cout << "DISPLAYING INFORMATION\n"; // try not to use endl unless you really need to flush << endl;
std::cout << "Name: " << employee.name << "\n";
std::cout << "Age: " << employee.age << "\n";
std::cout << "Salary: " << employee.salary << "\n";
}
// however to output a struct overloading the operator<< for streams
// is more reusable
std::ostream& operator<<(std::ostream& os, const Employee& employee)
{
os << "DISPLAYING INFORMATION\n"; // try not to use endl unless you really need to flush << endl;
os << "Name: " << employee.name << "\n";
os << "Age: " << employee.age << "\n";
os << "Salary: " << employee.salary << "\n";
return os;
}
int main()
{
// either use std::array, or std::vector they keep the size with them
// initialize from code for now (easier testing)
std::vector<Employee> employees{ {"Alice",30,1000.0f},{"Bob",42,800.0f},{"Charly",12,0.0f}};
// when looping over collections use range based for loops
// they cannot go out of scope
for (const auto& employee : employees)
{
// because operator << has been overloaded for an employee
// we can just output it.
std::cout << employee << "\n";
}
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.