0
#include <iostream>

using namespace std;

struct teacher
{
    int id;
    char name[50];
    int salary;

    void input(teacher a)
    {
        cout << "Enter Name : ";
        cin >> a.name;
        cout << "Enter ID : ";
        cin >> a.id;
        cout << "Enter Salary : ";
        cin >> a.salary;
    }

    void output(teacher b)
    {
        cout << "Your Name Is : " << b.name << endl;
        cout << "Your ID Is : " << b.id << endl;
        cout << "Your Salary Is : " << b.salary;
    }
};

int main()
{
    teacher t;
    t.input(t);
    t.output(t);

    return 0;
}

Is there any problem? The output is random numbers, don't know what is it. I tried writing the output function separately, but still same results.

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
  • Unrelated: drop `using namespace std;`, use `std::cout` instead. Replace `char` arrays by `std::string`. Don't use `std::endl` instead use `"\n"`, don't pass `teacher` to `input/output`, use `class` instead of `struct`. – Micha Wiedenmann Mar 02 '20 at 12:49
  • 1
    Does this answer your question? [What's the difference between passing by reference vs. passing by value?](https://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value) – Borgleader Mar 02 '20 at 12:49
  • Just as a side note: [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Andreas Wenzel Mar 02 '20 at 12:51
  • I'm currently learning oop , so before using class , I tried doing it with structure. – Qasim Ahmed Mar 02 '20 at 12:54
  • I'll try using class – Qasim Ahmed Mar 02 '20 at 12:54
  • 1
    `class` and `struct` are functionally equivalent in C++, the only difference is their default visibility (private for `class`, public for `struct`). You can use either/or. – NotAProgrammer Mar 02 '20 at 12:56

2 Answers2

0

This seems like a weird design, why don't your class methods directly operate on this?

struct teacher
{
    int id;
    std::string name;
    int salary;

    void input()
    {
        cout << "Enter Name : ";
        cin >> name;
        cout << "Enter ID : ";
        cin >> id;
        cout << "Enter Salary : ";
        cin >> salary;
    }

    void output() const
    {
        cout << "Your Name Is : " << name << endl;
        cout << "Your ID Is : " << id << endl;
        cout << "Your Salary Is : " << salary;
    }
};

Then main would look like

int main()
{
    teacher t;
    t.input();
    t.output();

    return 0;
}

Also I'd prefer to use std::string instead of char[] when possible.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
0

In input() you modify parameter a which goes out of scope when it reaches the end of the function.

Instead you should modify the class members variables themselves.

void input()
{
    cout << "Enter Name : ";
    cin >> name;
    cout << "Enter ID : ";
    cin >> id;
    cout << "Enter Salary : ";
    cin >> salary;
}
acraig5075
  • 10,588
  • 3
  • 31
  • 50