0

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

Ball Rall
  • 63
  • 8

3 Answers3

3

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);
Paul Evans
  • 27,315
  • 3
  • 37
  • 54
1

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);
}
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • is there a way to do it without using vectors and array Emps; because i the question i am asked to use loops and functions along structures rather than vector or arrays – Ball Rall Jun 18 '19 at 11:22
  • @BallRall the first variant does not use a vector, but then you only have a single instance, ie you need to print it before taking input for the next – 463035818_is_not_an_ai Jun 18 '19 at 11:23
  • i meant to say i have to take all the inputs first and then output them is there a way to do this by just modifying the loop or creating another loop – Ball Rall Jun 18 '19 at 11:25
  • 1
    @BallRall no, you have to store the input somewhere. If you overwrite it in the next iteration of the loop there is no way to print it later – 463035818_is_not_an_ai Jun 18 '19 at 11:27
  • Also it's never to early to learn about const correctness: Change `void Showinfo(Employee Emp)` to `void Showinfo(const Employee& Emp)` and use `const auto&` in the for loop. – nada Jun 18 '19 at 11:36
  • @nada you forgot the @, anyhow thanks for reminding me, I considered it, but then forgot to add it – 463035818_is_not_an_ai Jun 18 '19 at 11:37
  • @nada well only now I noticed that it is passed by value, so it isnt that relevant and a too long detour from the actual question to add it to the answer – 463035818_is_not_an_ai Jun 18 '19 at 11:40
  • @formerlyknownas_463035818 True, another possibility would be to just use it without explaining it, so OP might go *"Oh what is that '&' and 'const'? I need to learn about this."* – nada Jun 18 '19 at 11:42
  • @nada ok thanks i search it up but is there a way to do it with storing it in arrays rather than using vectors the professor is complaining. – Ball Rall Jun 19 '19 at 16:11
  • @formerlyknownas_463035818 can i do it storing it in an array – Ball Rall Jun 19 '19 at 16:12
  • @BallRall if you mean `std::array` you need to know the size already at compile time. Note that `std::vector` basically **is** a resizable array (and much more). If you are asking for c-style arrays, I would actually suggest you to consult your teacher/tutor becaues c-style arrays are by no means easy to deal with and require thorough studying of intricate details to avoid common pitfalls. – 463035818_is_not_an_ai Jun 19 '19 at 16:22
  • 1
    @BallRall It is a bit controversial whether beginners should start with low-tech such as c-arrays or jump to the fun part of std containers like `std::vector`. I have a strong opinion on that, anyhow, if your teacher thinks it is a good idea to keep you away from `std::vector` then they should provide a solid base for using c-style arrays. – 463035818_is_not_an_ai Jun 19 '19 at 16:25
  • @formerlyknownas_463035818 ok i would do that so that means there is an alternative to vectors by storing it in an array but can i know what is it that you prefer since you said you have a strong opinion on that – Ball Rall Jun 19 '19 at 17:09
  • @BallRall vectors are simple c-arrays are difficult. – 463035818_is_not_an_ai Jun 19 '19 at 17:13
  • @formerlyknownas_463035818 ok thanks can i know how to store all these values in the array and then output them the required way and by arrays i meant https://www.tutorialspoint.com/cplusplus/cpp_arrays.htm this . I did not understand what c arrays meant is the link i provided is what c arrays are i searched them and this is what i got " C-Style array is just a "naked" array - that is, an array that's not wrapped in a class, like this " char[] array = {'a', 'b', 'c', '\0'}; – Ball Rall Jun 19 '19 at 17:35
1

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
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335