fork download
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. struct address
  5. {
  6. char street[30];
  7. char city[30];// added city member
  8. char state[2];
  9. int zip;// no need for array
  10. };
  11.  
  12. struct student
  13. {
  14. address home;
  15. char first[15];
  16. char last[20];
  17. int year;//no need for an array
  18. };
  19.  
  20. int main()
  21. {
  22. student s[2]; // array of two student structures
  23. int i, j;
  24.  
  25. for (i = 0; i < 2; i++)
  26. {
  27. cout << "Enter Student First Name: ";
  28. cin >> s[i].first; // input into the ith student's first name field
  29. cout << "Enter Student Last Name: ";
  30. cin.ignore(1, '\n'); // clear enter
  31. cin >> s[i].last;
  32. cout << "Enter Street Address: ";
  33. cin.ignore(1, '\n');
  34. cin.getline(s[i].home.street, sizeof(s[i].home.street), '\n'); /* whole line for street*/
  35. cout << "Enter City: ";
  36. cin.getline(s[i].home.city, sizeof(s[i].home.city), '\n');
  37. cout << "Enter State (ex. NY): ";
  38. cin >> s[i].home.state;
  39. cout << "Enter Zip Code: ";
  40. cin.ignore(1, '\n');
  41. cin >> s[i].home.zip;
  42. cout << "Enter Class Year: ";
  43. cin >> s[i].year;
  44. cout << endl;
  45. }
  46.  
  47. // pass student array to separate function - student pointer
  48.  
  49. for (j = 0; j < 2; j++) // change to while pointer comparison loop
  50. {
  51. cout << "\n\n"; // change to print fields using pointer and arrow operator
  52. cout << s[j].first << " " << s[j].last << " " << s[j].year << endl;
  53. cout << s[j].home.street << endl;
  54. cout << s[j].home.city << ", " << s[j].home.state << " " << s[j].home.zip << endl;
  55. }
  56. }
Success #stdin #stdout 0s 3348KB
stdin
Dee
Snuts
29 Center St.
Elgin
IL
60120
1995
Otto
Verker
10 Candlewood Rd.
Brentwood
NY
11717
1994
stdout
Enter Student First Name:  Enter Student Last Name:  Enter Street Address:  Enter City:  Enter State (ex. NY):  Enter Zip Code:  Enter Class Year:  
Enter Student First Name:  Enter Student Last Name:  Enter Street Address:  Enter City:  Enter State (ex. NY):  Enter Zip Code:  Enter Class Year:  


Dee Snuts  1995
29 Center St.
Elgin, IL  60120


Otto Verker  1994
10 Candlewood Rd.
Brentwood, NY  11717