title: How to create a function to read/write data to struct members?
body: <p>I'm trying to create a program that read and print students' data with c++. for that, I've created a     struct Student, a function to read data from the user and assign it to a struct instance     s1 and a function to print students' data on the screen, and I think the problem is with the function that read/write data.</p>
  
  <p>Here is the my code:</p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;string&gt;
  using namespace std;
  
  struct Student
  {
      char name[30];
      int age;
      double gpa;
      string department;
  };
  
  Student read_data(Student x)
  {
      cout &lt;&lt; "Name (30 characters maximum): ";
      cin.get(x.name, 30);
      cout &lt;&lt; "Age: ";
      cin &gt;&gt; x.age;
      cout &lt;&lt; "Department: ";
      cin &gt;&gt; x.department;
      cout &lt;&lt; "GPA: ";
      cin &gt;&gt; x.gpa;
  
      return x;
  }
  
  void print_data(Student x)
  {
      cout &lt;&lt; 
  "\n***************************************************************" &lt;&lt; endl;
      cout &lt;&lt; "Name: " &lt;&lt; x.name &lt;&lt; endl;
      cout &lt;&lt; "Age: " &lt;&lt; x.age &lt;&lt; endl;
      cout &lt;&lt; "Department: " &lt;&lt; x.department &lt;&lt; endl;
      cout &lt;&lt; "GPA: " &lt;&lt; x.gpa &lt;&lt; endl;
  }
  
  int main()
  {
      Student s1, s2, s3;
  
      cout &lt;&lt; "This program stores -Temporarily- data of three students\n" &lt;&lt; endl;
  
      cout &lt;&lt; "Enter 1st student's data" &lt;&lt; endl;
  
      read_data(s1);
  
      print_data(read_data(s1));
  
      system("pause");
      return 0;
  }
  </code></pre>
  
  <p>The output of this code is:</p>
  
  <pre><code>This program stores data of three students
  
  Enter 1st student's data
  Name (30 characters maximum): Ahmed Maysara
  Age: 22
  Department: CS
  GPA: 3.5
  Name (30 characters maximum): Age: Department: GPA:
  ***************************************************************
  Name:
  Age: -858993460
  Department:
  GPA: -9.25596e+61
  Press any key to continue . . .
  </code></pre>
  
  <p>As you see, the output is out of my expectations :) ..</p>
  
  <p>Any help ?!</p>