0

I am trying to store the name,age and wage of data type Emp into an array and ask the user to first input them all and then output them all.

This is what Final wanted output looks like using plain arrays

Number of users to be inputted:3

first one inputted reset*

second one inputted reset*

third one inputted reset*

--------------------------------------------------->>

Displaying information

Name age salary

QWe 69 420

Dvor 42 6900

RT 24 6898

------------------------------------------------------------>>

As for information of three employees inputted.I got a solutions using vectors but can i do it by storing it in arrays

//Current code

// Included header files
#include <iostream>
#include <string>
#include<algorithm>
#include<vector>

using namespace std;

// Global variable declaration
int sized = 0;

// Struct declaration
struct Employee
{
    string name;
    int age;
    double salary;
};

// Protype
void Showinfo(Employee);

int main()
{
    // Declaring a variable of type Employee
    Employee Emp;

    // Inputting the number of employees that are being inputted
    cout << "Enter the number of the employees that you want to enter into the database: ";
    cin >> sized;

    cout << endl << endl;

    // Reseting the screen
    system("cls");

    // For loop to get the name,age and salary of the given number of employees
    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");
    }

    cout << "Name\t" << "Age\t" << "Wage" << endl;

    for (const auto& e : employees) {
        Showinfo(e);
    }

    cout << "\n" << endl;

    // Pause the console
    cout << "The program has ended" << endl << endl;
    system("pause");
    return 0;
}

// To display/showcase the information received
void Showinfo(Employee Emp)
{
    cout << Emp.name << '\t'
        << Emp.age << '\t'
        << Emp.salary << '\t' << endl;
}

I am trying to get the same output but using an array of type Employee to store the employee information and outputting them without using resizeable arrays/vectors

Like in this code I do need some assistance with a creating a loop that displays the information i do have an array of type Employee declared in global scope after declaring the integer sized.How can i stored these values(Employees info:name,age and salary and output them or is it not possible that way)

Ball Rall
  • 63
  • 8
  • _"and outputting them without using resizeable arrays/vectors"_ Where does this restrriction comes from. Any concicse reasons? – πάντα ῥεῖ Jun 20 '19 at 11:35
  • What have you tried? I see a working solution using an `std::vector`, but I see no attempt to use arrays here. Additionally, why would you want that? `std::vector` is **the** correct solution here. To partially answer your question - array's size must be a compile time constant. You can't read a number, declare an array of that size and use it. There are compiler extensions which allow that, but that's non-standard. Let me ask again - what's wrong with `std::vector` here and why would you want to use `T[]` or `std::array`? – Fureeish Jun 20 '19 at 11:35
  • @πάνταῥεῖ i just started coding and we are learning arrays and loops and i learned structures myself.I started from the start of this month and i know i skipped some parts i learned struct myself and was trying to do this myself as a small project and the professor told me to do it using c_style arrays which i believe are plain arrays and asked me to try it.I tried inputting it like how i do it with normal arrays using for loop and then "cin>>Arrayy[i];" Array is Employee type and i guess he is just being annoying,because when he wanted to teach me loops first he asked me to input and output – Ball Rall Jun 20 '19 at 11:46
  • @πάνταῥεῖ ran out of chars and i did input 20 Elements and output them manually and then he told me that is why you use loops.He is probably trying to do that in this case too – Ball Rall Jun 20 '19 at 11:47
  • 1
    @BallRall _and the professor told me to do it using c_style arrays which i believe are plain arrays and asked me to try it"_ That's probably the culprit. Your professor is incompetent about teaching idiomatic c++. – πάντα ῥεῖ Jun 20 '19 at 11:48
  • @πάνταῥεῖ Ok thanks i will shift from his class to another if possible but is it possible to do it that way – Ball Rall Jun 20 '19 at 11:54
  • 1
    @πάνταῥεῖ i believe he does that in case of vectors like what he did with loops but anyway thanks to you i will change from his class as soon as possible – Ball Rall Jun 20 '19 at 11:57
  • @Fureeish i was asked to try it that way and i do not believe i can use a std::array i will search it tho and learn it.I tried to input the elements the way i do with normal arrays and got an error "No matching >> operator found" as i mentioned in the previous comment and i did not quite get this part is there a link for it so i can read of it:: "array's size must be a compile time constant. You can't read a number, declare an array of that size and use it. There are compiler extensions which allow that, but that's non-standard" – Ball Rall Jun 20 '19 at 12:04
  • Regarding the citation of my comment, you might be interested in reading about [VLA](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard)s. As for the `std::array` - it's just a wrapper around `T[]`. – Fureeish Jun 20 '19 at 12:06
  • @Fureeish so it is still possible to do it using c_style arrays but it is larger.Is that right or not i actually want to try it before shifting classes.But thanks for the link tho – Ball Rall Jun 20 '19 at 12:19
  • "*so it is still possible to do it using c_style arrays but it is larger*" - what exactly is *larger*? What do you mean by "*shifting classes*"? – Fureeish Jun 20 '19 at 12:22
  • @Fureeish _"What do you mean by "shifting classes"?"_ Probably atending a better course I believe. Their proffessor seems to be incompetent as I mentioned. – πάντα ῥεῖ Jun 20 '19 at 12:56
  • @πάνταῥεῖ oh, thank you - I didn't pay much attention to the rest of the comments. Yup, totally agree with you here. – Fureeish Jun 20 '19 at 12:58
  • @πάνταῥεῖ _"That's probably the culprit. Your professor is incompetent about teaching idiomatic c++."_ - personally, I would not hire a C++ developer that _cannot_ do this using plain C arrays. – Kit. Jun 20 '19 at 14:04
  • @Kit. That's beyond the topic anyways ... – πάντα ῥεῖ Jun 20 '19 at 14:05
  • @πάνταῥεῖ the topic of this exercise, I think, is array indexing. – Kit. Jun 20 '19 at 14:11
  • @Kit. I don't care anyways. – πάντα ῥεῖ Jun 20 '19 at 14:12
  • @Fureeish i meant to get another person to teach me cs and i was asking that whether the size of program or lines of code used would be more yet they both give me the same result/output – Ball Rall Jun 20 '19 at 14:33
  • I think it's a bad idea to teach the basics of CS using C++ these days; Python would be a much better choice. C++ is not about writing the smallest amount of lines of code that give the desired output, C++ is about writing programs that will give the desired output using a smaller - or a more _predictable_ - amount of computer resources. Sometimes it means that you cannot use resizable containers provided by the standard library. – Kit. Jun 20 '19 at 15:34
  • @Kit. thanks.I was told that c++ is the best for game development so i chose it.Don't you mind if you help me on how to do this using plain arrays i tried calling an array of type Employee like Employee array[40]; but cannot input it using loops or how i do it with other normal arrays like "for loop { cin>>array[i];}" and outputting it the same way but i for some reason i cannot do it with this. – Ball Rall Jun 20 '19 at 15:49

2 Answers2

0

The basic operation for arrays is indexing. Indexing is accessing an array element by its position in the array. An array with N elements in C++ has the elements with positions from 0 to N-1. An indexing operation is designated with square brackets with the index value written inside, like array[0] or array[N-1] or array[i], where i is allowed to take a value from 0 to N-1.

A rough example:

#include <iostream>

using namespace std;

struct Employee
{
    string name;
    int age;
    double salary;
};

Employee employees[40];

int main()
{
    int size;
    cin >> size;
    if (size > 40) {
        return -1;
    }

    for (int i = 0; i < size; ++i) {
        cin >> employees[i].name >> employees[i].age >> employees[i].salary;
    }

    for (int i = 0; i < size; ++i) {
        cout << employees[i].name << " "
             << employees[i].age << " "
             << employees[i].salary << endl;
    }

    return 0;
}

Hopefully you already know what for (int i = 0; i < size; ++i) means.

Kit.
  • 2,386
  • 1
  • 12
  • 14
  • thanks did not know i have to add .name or .age at the end.Yes i do know that and can i ask about how long you have been programming and how long would it take me to learn programming at advance level like getting a job and being able to create games and apps and got a good understanding of the rules in the language if i learn it myself rather than following the college method of teaching. – Ball Rall Jun 20 '19 at 17:00
  • I am not a good example: I learned C++ even before it was standardized for the first time more 20 years ago, and the language itself grew quite a lot since then. Because C++ has such an old history, modern programming in it is quite complicated for a newcomer. I would really recommend Python as the first language to learn; it's a relatively simple and powerful language, good for prototyping, and sometimes I even write Python scripts to generate C++ or Java code. Don't be afraid of knowing and using more than one language. – Kit. Jun 20 '19 at 17:31
  • Ok thanks for the reply i have no problem learning more than one language but now i have to finish the syntax for one language i started this month so around 3 weeks and this is actually now only i started using the computer for coding and did not learn it before and only used it for gaming.Lets say from now on i continue coding in how long can i be at professional level and can i know when you start coding almost everyone says they write their code at the age of 10 or below and i am here starting it when i'm 17 but everyone starts somewhere – Ball Rall Jun 20 '19 at 17:43
  • To _get paid_ for game development in C++, I guess you would need a couple of years of involvement in some programming activities, with at least half a year of them in C++. If I were you, I would consider internship. – Kit. Jun 20 '19 at 18:10
0

Here is the final code

#include <iostream>
#include <iomanip>

using namespace std;

//Global variable declaration
const int arraysize = 40;

struct Employee
{
    string name;
    int age;
    double salary;
};

//Protype
void Showinfo(Employee employees[40], int& size);

//Global array declaration
Employee employees[arraysize];

int main()
{
    int size;
    cout << "Input the size: ";
    cin >> size;

    cout << endl << endl;

    // Reseting the screen
    system("cls");

    if (size > 40) {
        return -1;
    }

    for (int i = 0; i < size; ++i) {
        cout << "Enter a name: ";
        cin >> employees[i].name;
        cout << endl;
        cout << "Enter age: ";
        cin >> employees[i].age;
        cout << endl;
        cout << "Enter the salary: ";
        cin >> employees[i].salary;
        cout << endl;
        system("cls");
    }

    cout << "Displaying Information" << endl;
    cout << "----------------------------------" << endl;

    Showinfo(employees, size);

    cout << "----------------------------------" << endl;
    cout << "\n" << endl;

    cout << "The program has ended" << endl << endl;
    system("pause");
    return 0;
}

void Showinfo(Employee employees[arraysize], int& size) {
    for (int i = 0; i < size; ++i) {
        if (i == 0)
        {
            cout << setw(4) << "Name\t" << "Age\t" << "Wage" << endl << endl;
        }
        cout << setw(4) << employees[i].name << '\t'
            << employees[i].age << '\t'
            << employees[i].salary << endl;
    }
}
Ball Rall
  • 63
  • 8
  • You are creating a copy of the full array when you are passing it into the Showinfo function. There are several ways to avoid it (the most common two are references and pointer arithmetic), but none of them are trivial and I don't know which one(s) you have already learned. That's why C++ is bad for initial learning. – Kit. Jun 20 '19 at 18:42
  • Actually, you are passing the size by reference (which you could pass by value, `int` is a small value and you don't need to modify the size from `Showinfo()`), but passing the array by value. You could pass the array by reference, but you function would still work only with the arrays of predefined size. Alternatively, you could pass Employee by reference into a function that shows info for the single employee, and make the loop external to this function. – Kit. Jun 20 '19 at 18:47
  • @Kit. Thanks i would do that and update the answer soon. – Ball Rall Jun 21 '19 at 02:55