I'm trying to create a print void function, however I'm not understanding how to call the database in the print function. I've tried to do something similar to what is in my textbook but all I'm getting is errors. This is what I've done so far.

#include <iomanip>
#include <iostream>
#include <string>
using namespace std; 

 struct Student
  {
	string name;
	string address;
	string city;
	string state;
	int zip;
	char gender;
	string id;
	float gpa;
  } a, b, c;
 void printstudent(Student& a)
 {
	 cout << name << endl;
	 cout << address << endl;
 }
int main()
{
  // student 1 user input info
  cout << "What is Student 1's name?: ";
  getline(cin,a.name);
  cout << "What is Student 1's address?: ";
  getline(cin,a.address);
  cout << "What is Student 1's city?: ";
  getline(cin,a.city);
  cout << "What is Student 1's state?: ";
  getline(cin,a.state);
  cout << "What is Student 1's student ID?: ";
  getline(cin,a.id);
  cout << "What is Student 1's zip?: ";
  cin >> a.zip;
  cout << "What is Student 1's gender?[M/F]: ";
  cin >> a.gender;
  cout << "What is Student 1's gpa?: ";
  cin >> a.gpa;

  printstudent(Student& a);

  return 0;
}

Recommended Answers

All 13 Replies

I'm trying to create a print void function, however I'm not understanding how to call the database in the print function. I've tried to do something similar to what is in my textbook but all I'm getting is errors. This is what I've done so far.

#include <iomanip>
#include <iostream>
#include <string>
using namespace std; 

 struct Student
  {
	string name;
	string address;
	string city;
	string state;
	int zip;
	char gender;
	string id;
	float gpa;
  } a, b, c;
 void printstudent(Student& a)
 {
	 cout << name << endl;
	 cout << address << endl;
 }
int main()
{
  // student 1 user input info
  cout << "What is Student 1's name?: ";
  getline(cin,a.name);
  cout << "What is Student 1's address?: ";
  getline(cin,a.address);
  cout << "What is Student 1's city?: ";
  getline(cin,a.city);
  cout << "What is Student 1's state?: ";
  getline(cin,a.state);
  cout << "What is Student 1's student ID?: ";
  getline(cin,a.id);
  cout << "What is Student 1's zip?: ";
  cin >> a.zip;
  cout << "What is Student 1's gender?[M/F]: ";
  cin >> a.gender;
  cout << "What is Student 1's gpa?: ";
  cin >> a.gpa;

  printstudent(Student& a);

  return 0;
}

Hello, when you try to print your student data you have to use the struct variable a like this:

void printstudent(Student& a)
 {
	 cout << a.name << endl;
	 cout << a.address << endl;
 }

Moreover when you call your function you just have to pass your struct variable, like this:

printstudent(a);

I'm still getting an error. It says my variables are undeclared, the ones I have so far in the print function. I want it to use the variables from struct Student. How do I do that? I want it to be able to use a generic one for 3 students. I was trying it with 'a' first to see how it works.

The problem is that you are passing a particular structure to your printstudent function BUT you are not actually using that data. Let me explain with an example:

int main()
{
   Student FirstStudent;    // Make a student
   Student SecondStudent;   // Make a DIFFERENT student

   // Now populate the student with infomation

   printstudent(FirstStudent);   // Print only about first student
   std::cout<<"Now write about second"<<std::endl;
   printstudent(SecondStudent);   
}

Note that in the above code, you call printstudent with the object that is required to be printed.
Not with a definition e.g. This is wrong: printstudent(Student& a); In the code the student writing should work with each student passed to the function.
in your function you do not do that. So change it to something like this:

void printstudent(const Student& SItem)
{
  cout << SItem.name << endl;
  cout << SItem.address << endl;
}

Note the use of SItem. That indicates that the object SItem's data will be used. You can equally use the same construct if you want to call a method of the Student struct for a particular object.

Hope that helps.

I'm still getting an error. It says my variables are undeclared, the ones I have so far in the print function. I want it to use the variables from struct Student. How do I do that? I want it to be able to use a generic one for 3 students. I was trying it with 'a' first to see how it works.

You can have lots of variables with an array of structs.

Student arr[3]; //that's an array of 3 students

So, to read students' data:

for (int i = 0; i < 3; i++) {
   cout << "what's 1st student's name:";
   getline(cin, [B][U]arr[i].name[/U][/B]); //that's how you manipulate each struct of 
                                            //the array
   etc..
 
}

and to print student's data:

void printStudents(Student array[], int size) {
   for (int i = 0; i < size; i++) {
    cout << i << "st student's name" << array[i].name << endl;";
  etc...
  }
}

Yeah, I get it. I've chosen not to use arrays in this simple one because it is only 3 students and I've been able to keep track of it easily.
I do have one final question. How do I take my questions and put them into a void function?
I tried this

void questionsstudent(const Student& SItem)
 {
	 cout << "What is Student 1's name?: ";
 }

But if I put in the code from below it will only go for the first student. How do I make it generic? I know I can't just leave it as name because that makes it an undeclared variable.

getline(cin,a.name);

Yeah, I get it. I've chosen not to use arrays in this simple one because it is only 3 students and I've been able to keep track of it easily.
I do have one final question. How do I take my questions and put them into a void function?
I tried this

void questionsstudent(const Student& SItem)
 {
	 cout << "What is Student 1's name?: ";
 }

But if I put in the code from below it will only go for the first student. How do I make it generic? I know I can't just leave it as name because that makes it an undeclared variable.

getline(cin,a.name);

If you use an array makes things simpler..In order to read your data via a function do it like this:

void readData(Student arr[], int size) {
 for (int i = 0; i < size; i++) {
  cout << "what's " << i << "st student's name?:";
  getline(cin, arr[i].name);
  etc..
}
}

If you want to keep up with your way (means that you won't use an array) then your have to use your function 3 times:

void readStudent(Student& s) {

  cout << "what's student's name?:";
   getline(cin, s.name);
  cout << "what's student's address?:";
  getline(cin, s.address);
  etc...
}

and in main function you will have to code like this:

readStudent(a);
  readStudent(b);
  readStudent(c);

I choose the array..

So without an array I have to create 3 functions?

So without an array I have to create 3 functions?

No, you will create one function:

void readStudent(Student& s) {
      cout << "what's student's name?:";
      getline(cin, s.name);
      cout << "what's student's address?:";
      getline(cin, s.address);
      etc..
      }

but you will have to execute your function 3 times..

readStudent(a);
  readStudent(b);
   readStudent(c);

Oh, okay. That is what I was doing with it. It works, however when it asks about the 2nd and 3rd student, it puts the first two questions together and only gets the answer for the 2nd one.

Oh, okay. That is what I was doing with it. It works, however when it asks about the 2nd and 3rd student, it puts the first two questions together and only gets the answer for the 2nd one.

If it doesn't wait for your input then you'll have to clear your buffer input..Do it entering wherever there's a problem:

cin.ignore(1, '\n');

I don't understand what you mean. Do I put that in when I bring up my program and it isn't waiting for input?

I don't understand what you mean. Do I put that in when I bring up my program and it isn't waiting for input?

Post your code as it is now..

Nevermind I figured it out. It works properly now. Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.