C++ help. I am having difficulty understanding pointers within function prototyp
ID: 3669973 • Letter: C
Question
C++ help. I am having difficulty understanding pointers within function prototypes and structures. I understand the code withing the struc and func itself. I just am confused about how to get the information in main. Here is my programming exercise:
Change the declaration of
Student freshman;
to
Student *freshman'
Dynalmically allowcate a Student object and then fix the rest of the program to work. Make sure to use delete properly.
// This program demonstrates a function that uses a
// pointer to a structure variable as a parameter.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
struct Student
{
string name; // Student's name
int idNum; // Student ID number
int creditHours; // Credit hours enrolled
double gpa; // Current GPA
};
void getData(Student *); // Function prototype
int main()
{
Student freshman;
// Get the student data.
cout << "Enter the following student data: ";
getData(&freshman); // Pass the address of freshman.
cout << " Here is the student data you entered: ";
// Now display the data stored in freshman
cout << setprecision(3);
cout << "Name: " << freshman.name << endl;
cout << "ID Number: " << freshman.idNum << endl;
cout << "Credit Hours: " << freshman.creditHours << endl;
cout << "GPA: " << freshman.gpa << endl;
return 0;
}
//*******************************************************
// Definition of function getData. Uses a pointer to a *
// Student structure variable. The user enters student *
// information, which is stored in the variable. *
//*******************************************************
void getData(Student *s)
{
// Get the student name.
cout << "Student name: ";
getline(cin, s->name);
// Get the student ID number.
cout << "Student ID Number: ";
cin >> s->idNum;
// Get the credit hours enrolled.
cout << "Credit Hours Enrolled: ";
cin >> s->creditHours;
// Get the GPA.
cout << "Current GPA: ";
cin >> s->gpa;
}
Explanation / Answer
// This program demonstrates a function that uses a
// pointer to a structure variable as a parameter.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
struct Student
{
string name; // Student's name
int idNum; // Student ID number
int creditHours; // Credit hours enrolled
double gpa; // Current GPA
};
void getData(Student *); // Function prototype
int main()
{
Student *freshman;
//Dynamically allocating memory for freshman
freshman = new Student;
// Get the student data.
cout << "Enter the following student data: ";
getData(freshman); // Pass the address of freshman.
cout << " Here is the student data you entered: ";
// Now display the data stored in freshman
cout << setprecision(3);
cout << "Name: " << freshman->name << endl;
cout << "ID Number: " << freshman->idNum << endl;
cout << "Credit Hours: " << freshman->creditHours << endl;
cout << "GPA: " << freshman->gpa << endl;
//deleting dynamically allocated memory
delete freshman;
return 0;
}
//*******************************************************
// Definition of function getData. Uses a pointer to a *
// Student structure variable. The user enters student *
// information, which is stored in the variable. *
//*******************************************************
void getData(Student *s)
{
// Get the student name.
cout << "Student name: ";
getline(cin, s->name);
// Get the student ID number.
cout << "Student ID Number: ";
cin >> s->idNum;
// Get the credit hours enrolled.
cout << "Credit Hours Enrolled: ";
cin >> s->creditHours;
// Get the GPA.
cout << "Current GPA: ";
cin >> s->gpa;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.