2

I have overloaded a function in a class called students.The one that input the info works but the one that outputs it does not work. I do believe the problem is somewhere in the main. The code was way worse, I have fixed a lot of problems and come to this. If I try to output the student's name, it just ends the program.

These are the member functions and variables:

class ShowStudents
{
   public:
 //Declaring a struct
 struct Employee
 {
string name_public = "";
int age_public = 0;
double ID_No_publice = 0;
};

 //Array declaration of type Employee
 Employee employees[arraysize];


    void Students(int size_public)
    {
        cout << "Input the number of information to be entered: ";
        cin >> size_public;

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

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

    void Students(Employee employees[arraysize], int& size_public)
    {
        for (int i = 0; i < size_public; i++)
        {
            if (i == 0)
            {
                cout << setw(4) << "Name\t" << "Age\t" << "Wage" << endl << endl;
            }
            cout << setw(4) << employees[i].name_public << '\t'
                << employees[i].age_public << '\t'
                << employees[i].salary_public << endl;
        }
    }

};
> This is my main function:
  int main()
     {

        //Declared the variables

        student.Students(size);

       student.Students(student.employees, size);<------ does not output

        system("cls");
     }

I have removed some parts of the main that are not that important. I wanted to provide a minimal reproducible code. I am using the Stack Overflow for a month so far, so if my question is bad or not understandable, you can tell me. I am also a month and a week into programming so I may ask a lot of questions that might be dumb. Please do not get mad.

Ball Rall
  • 63
  • 8
  • You forgot to explain your problem. – tkausl Jul 02 '19 at 19:14
  • 1
    Thanks for cleaning up the code before posting it here. Can you tell us what the specific issue is that you're running into? – templatetypedef Jul 02 '19 at 19:14
  • 1
    Those 2 functions are very different (one inputs the data and one outputs it) and should have different names. – 001 Jul 02 '19 at 19:15
  • 1
    What is the desired behavior of your code? – Justin Randall Jul 02 '19 at 19:15
  • @tkausl i does not output the student names.If you look at main this is where the problem is caused else if (choice == 2) { student.Students(student.employees, size); } – Ball Rall Jul 02 '19 at 19:18
  • `else if (choice != 1 && 2)` is not how you compare against 2 values: `else if (choice != 1 && choice != 2)` – 001 Jul 02 '19 at 19:18
  • @tkausl i will edit the question – Ball Rall Jul 02 '19 at 19:18
  • `size` never gets modified - it is always 0. You could pass a reference: `void Students(int& size_public)`, or send it back as a return value: `int Students()` – 001 Jul 02 '19 at 19:20
  • @templatetypedef i did edit the question – Ball Rall Jul 02 '19 at 19:20
  • @JustinRandall i wanted to output the students name after inputting them – Ball Rall Jul 02 '19 at 19:22
  • @JohnnyMopp Thanks for tip.For now i just tried function overloading in classes and used the same names and sorry i spend some time on other works,so i was tired and did not write choice!=2.I will fix it – Ball Rall Jul 02 '19 at 19:24
  • @JohnnyMopp i did try that before but it did not work – Ball Rall Jul 02 '19 at 19:28
  • @JustinRandall thanks.I did get it it was the cls command that was not letting me read the output.Sorry everyone i was tired i did not remeber that – Ball Rall Jul 02 '19 at 19:30
  • @JohnnyMopp sorry it was not giving me the output due to cls command.You were right – Ball Rall Jul 02 '19 at 19:31
  • 1
    If you want to learn c++ the right way, there's a curated list of [good c++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). "Programming: Principles and Practice Using C++" and "A Tour of C++" should be enough and it's miles better than any web/youtube tutorials. – Quimby Jul 02 '19 at 20:11
  • @Quimby is this the book you were referring to https://www.amazon.com/Tour-C-Depth/dp/0321958314 – Ball Rall Jul 03 '19 at 06:15
  • @BallRall Yep, but this is kind of an overview of new things. It won't teach you programming from the beginning. First, read [link](https://www.amazon.com/Programming-Principles-Practice-Using-2nd/dp/0321992784) which gets you from 0 to modern C++14 and the second will review it and adds c++17/20. Ideally, the 3rd version of the first one will cover everything, but it won't be released before 2020 probably in order to incorporate c++20. – Quimby Jul 03 '19 at 08:43
  • @Quimby Thanks for the help.I had a question.The link leads me to Principles and Practice Using C++ (2nd Edition).Should i get the first edition,Then the second,Third and fourth.Is this a good option to start programming from beginning and get a good understanding of programming basics,principles and rules. – Ball Rall Jul 03 '19 at 10:49
  • @BallRall No, the second is just updated first. So get the 2nd only. You can find them or at least their indices on the internet, so you can take a look at what's inside. – Quimby Jul 03 '19 at 13:28
  • @Quimby Ok i will buy this book as soon as possible.I had a question before,I read the reviews of the book.From what i got it teaches me programming from the beginning to a point that i can create apps or games or i have to got the third and fourth edition for that and i wanted to know what is the difference between this and the third of fourth edition book.Is it just the updated version of the second or there are more to it and what is the best option to get.Sorry for bothering you a lot. – Ball Rall Jul 03 '19 at 16:57
  • @Quimby I wanted to know what book gives me a strong understanding of programming in general. – Ball Rall Jul 08 '19 at 18:50
  • Sorry, I must've missed the notification. It will teach you C++. It won't teach you anything about games or apps. There's no third or fourth edition of "Principles and Practice Using C++". The second is the newest there is. As I said, find a PDF and look through it and see if that is what you want to know. Or go to a library if you can :) Note that "making games" is hard, even if you know everything it just takes a lot of time. – Quimby Jul 08 '19 at 19:01

1 Answers1

1

The first thing I have noticed is as follows (according to the comments there are some other bugs):

In main you declare:

int size = 0;

and then call

student.Students(size);

However in the method

void Students(int size_public)

you pass size_public by value and its modification

 cin >> size_public;

has no effect outside.

Then you call:

student.Students(student.employees, size);<------ does not output

but as explained before size value is still =0 because its value was not modified by the Students(int size_public) method.

One possible fix is to pass size by reference. Modify the prototype:

void Students(int size_public)

to

void Students(int& size_public)
Picaud Vincent
  • 10,518
  • 5
  • 31
  • 70