4-13f. Here is part 4-13 part d, modify the code to produce parts b (separate he
ID: 3929087 • Letter: 4
Question
4-13f. Here is part 4-13 part d, modify the code to produce parts b (separate header file student4-13.h for the structure definition, see fields in code to see data fields) and parts e and f (separate function for the printing of the array and pass the address of the array to a student pointer that will increment through the array, printing the student address label by accessing the fields through the arrow operator and making the for loop into a pointer comparison while loop). Submit the final part f main .cpp and student4-13.h.
** d. with an array of student structures
** b. with the structure definition in a separate header file
* e. with a function to print the array
** f. with a pass by reference (pointer to the array) to the function (using the arrow operator), using the pointer comparison while loop
/*
Exercise 4-13d
This program will make a student structure
*/
#include<iostream>
#include"student4-13.h" // define student and address structures
using namespace std;
void main()
{
student s[2]; // array of two student structures
int i, j;
for (i = 0; i < 2; i++)
{
cout << "Enter Student First Name: ";
cin >> s[i].first; // input into the ith student's first name field
cout << "Enter Student Last Name: ";
cin.ignore(1, ' '); // clear enter
cin >> s[i].last;
cout << "Enter Street Address: ";
cin.ignore(1, ' ');
cin.getline(s[i].home.street, sizeof(s[i].home.street), ' '); /* whole line for street*/
cout << "Enter City: ";
cin.getline(s[i].home.city, sizeof(s[i].home.city), ' ');
cout << "Enter State (ex. NY): ";
cin >> s[i].home.state;
cout << "Enter Zip Code: ";
cin.ignore(1, ' ');
cin >> s[i].home.zip;
cout << "Enter Class Year: ";
cin >> s[i].year;
cout << endl;
}
// pass student array to separate function - student pointer
for (j = 0; j < 2; j++) // change to while pointer comparison loop
{
cout << " "; // change to print fields using pointer and arrow operator
cout << s[j].first << " " << s[j].last << " " << s[j].year << endl;
cout << s[j].home.street << endl;
cout << s[j].home.city << ", " << s[j].home.state << " " << s[j].home.zip << endl;
}
}
Explanation / Answer
// student4-13.h
#include <iostream>
using namespace std;
struct address{
char street[256];
char city[256];
char state[256];
int zip;
};
struct student{
string first;
string last;
address home;
int year;
};
// main.cpp
#include <iostream>
#include "student4-13.h" // define student and address structures
using namespace std;
void print(student *s, int size){
int j = 0;
while(j < size) // change to while pointer comparison loop
{
cout << " "; // change to print fields using pointer and arrow operator
cout << (s + j)->first << " " << (s + j)->last << " " << (s + j)->year << endl;
cout << (s + j)->home.street << endl;
cout << (s + j)->home.city << ", " << (s + j)->home.state << " " << (s + j)->home.zip << endl;
j++;
}
}
int main()
{
student s[2]; // array of two student structures
int i, j;
for (i = 0; i < 2; i++)
{
cout << "Enter Student First Name: ";
cin >> s[i].first; // input into the ith student's first name field
cout << "Enter Student Last Name: ";
cin.ignore(1, ' '); // clear enter
cin >> s[i].last;
cout << "Enter Street Address: ";
cin.ignore(1, ' ');
cin.getline(s[i].home.street, sizeof(s[i].home.street)); /* whole line for street*/
cout << "Enter City: ";
cin.getline(s[i].home.city, sizeof(s[i].home.city));
cout << "Enter State (ex. NY): ";
cin >> s[i].home.state;
cout << "Enter Zip Code: ";
cin.ignore(1, ' ');
cin >> s[i].home.zip;
cout << "Enter Class Year: ";
cin >> s[i].year;
cout << endl;
}
// pass student array to separate function - student pointer
print(&s[0], 3);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.