//structures passing to functions by value // create a Student structure that co
ID: 3778490 • Letter: #
Question
//structures passing to functions by value
// create a Student structure that contains an ID number (integer), student name (string class),
//number of courses taken (integer) and GPA (double). Use a function getStudent to enter data for a student.
//Use a function showStudent to show the data. Pass the Student structure to the functions by VALUE, not by reference.
/*
Enter the student ID (integer): 1234
Enter the student name: Ernst Bekkering
Enter the number of courses taken: 16
Enter the GPA: 3.75
Student ID: 1234
Name: Ernst Bekkering
Course taken: 16
GPA: 3.75
Press any key to continue . . .
*/
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
struct Student
{
int studentID;
string studentName;
int coursesTaken;
double GPA;
};
void getStudent (????????);
void showStudent (????????);
int main()
{
Student nsuStudent;
getStudent(????????);
showStudent(????????);
return 0;
}
//**********************
//definition of getStudent (by value)
//*************************
void getStudent(????????)
{
cout << "Enter the student ID (integer): ";
cin >> ????????;
cout << "Enter the student name: ";
cin.ignore();
getline(cin, ????????);
cout << "Enter the number of courses taken: ";
cin >> ????????;
cout << "Enter the GPA: ";
cin >>????????;
}
//**************************************
// definition of showStudent (by value)
//**************************************
void showStudent(????????)
{
cout << fixed << showpoint << setprecision(2);
cout << "Student number: " <<???????? << endl;
cout << "Name: " << ???????? << endl;
cout << "Course taken: " << ???????? << endl;
cout << "GPA: " << ???????? << endl;
}
Explanation / Answer
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
struct Student
{
int studentID;
string studentName;
int coursesTaken;
double GPA;
};
void getStudent (struct Student s);
void GetStudent (struct Student *s);
void showStudent (struct Student s);
int main()
{
Student nsuStudent;
getStudent(nsuStudent);//HERE PASSING AS VALUE SO.. SO VALUES WILL NOT GET UPDATED
showStudent(nsuStudent);//PASSING VAULE//PRINTS GARBAGE VALUES
GetStudent(&nsuStudent);//pass by reference
showStudent(nsuStudent);//PASSING VAULE//PRINTS GARBAGE VALUES
return 0;
}
//**********************
//definition of getStudent (by value)
//*************************
void getStudent(struct Student s)
{
cout << "Enter the student ID (integer): ";
cin >> s.studentID;
cout << "Enter the student name: ";
cin.ignore();
getline(cin,s.studentName);
cout << "Enter the number of courses taken: ";
cin >> s.coursesTaken;
cout << "Enter the GPA: ";
cin >>s.GPA;
}
//pass reference
void GetStudent(struct Student *s)
{
cout << "Enter the student ID (integer): ";
cin >> s->studentID;
cout << "Enter the student name: ";
cin.ignore();
getline(cin,s->studentName);
cout << "Enter the number of courses taken: ";
cin >> s->coursesTaken;
cout << "Enter the GPA: ";
cin >>s->GPA;
}
//**************************************
// definition of showStudent (by value)
//**************************************
void showStudent(struct Student s)
{
//cout << fixed << showpoint << setprecision(2);
cout << "Student ID: " <<s.studentID << endl;
cout << "Name: " << s.studentName << endl;
cout << "Course taken: " << s.coursesTaken << endl;
cout << "GPA: " << s.GPA << endl;
}
OUTPUT:-
Enter the student ID (integer): 1234
Enter the student name: SURYA
Enter the number of courses taken: 16
Enter the GPA: 3.71
Student ID: 0
Name:
Course taken: 4201003
GPA: 6.95279e-310
Enter the student ID (integer): 1234
Enter the student name: SURYA
Enter the number of courses taken: 17
Enter the GPA: 3.67
Student ID: 1234
Name: SURYA
Course taken: 17
GPA: 3.67
Process exited normally.
Press any key to continue . . .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.