I wrote the code I just do not know how to establish an array that refers to other arrays this is a test code not the real one
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
enum HealthType { Poor, Fair, Good, Excellent } ;
////struct method///
struct StudentType{
// declares a struct data type
// does not allocate memory
long id;
string firstname, lastname;
string school;
string major;
int age;
float weight;
HealthType health;
};
///////prints health////////
void PrintWord(HealthType health){
string msg;
switch (health) {
case Poor: msg = "Poor"; break;
case Fair: msg = "Fair"; break;
case Good: msg = "Good"; break;
case Excellent: msg = "Excellent"; break;
}
cout << msg;
}
////prints all students//////////
void PrintallStudents(StudentType allStudents[], int size){
for (int i=0;i< size; i++)
PrintStudentData(allStudents[i]);
}
/////prints one student that is selected/////////
void PrintStudentData( StudentType aStudent)
// Prints out values of all members of aStudent
// Precondition: all members of aStudent are assigned
// Postcondition: all members have been written out
{
cout <<"__________________________________________________________" << endl;
cout << setw(25)<< "ID #: " << aStudent.id <<endl;
cout << setw(25)<< "First and Last Name: " << aStudent.firstname << " " << aStudent.lastname << endl;
cout << setw(25)<< "School and Major: "<< aStudent.school << aStudent.major << endl;
cout << setw(25)<< "Age: " << aStudent.age << "years " << endl;
cout << setw(25)<< "Weight: "<< aStudent.weight << " lbs. " << endl;
cout << setw(25)<< "General health : ";
PrintWord ( aStudent.health );
cout << endl;
cout << "__________________________________________________________" << endl;
}
////////////Here is the get data where I enter the data///////////REFERENCE TO QUESTION/
StudentType GetStudentData ( void ) {
// Obtains all information about a student from keyboard
// Postcondition:
// Function value = StudentType members entered at keyborad
int res;
StudentType aStudent ;
cout<<"\nEnter student's info.."<<endl;
cout << setw(20) << "ID: "; cin>> aStudent.id;
cout << setw(20) << "First Name: "; cin>> aStudent.firstname;
cout << setw(20) << "Last Name: "; cin>> aStudent.lastname;
cout << setw(20) << "School: "; cin>> aStudent.school;
cout << setw(20) << "Major: "; cin>> aStudent.major;
cout << setw(20) << "Age: "; cin>> aStudent.age;
cout << setw(20) << "Weight: "; cin>> aStudent.weight;
cout << setw(10) << "(Poor=0,Fair=1,Good=2,Excellent=3)" << endl;
cout << setw(20) << "Health Type: ";
cin>> res; aStudent.health = (HealthType) res;
return aStudent ;
}
//////sort students using swap method///////
void swap(StudentType *std1, StudentType *std2){
StudentType hold = *std1;
*std1 = *std2;
*std2 = hold;
}
/////////sort students using bubblesort method//////////////
void bubbleSort(StudentType allStudents[], const int size){
int choice;
for (int pass=0; pass<size-1; pass++)
for (int k=0; k<size-1; k++)
if (allStudents[k].id > allStudents[k+1].id)
swap(&allStudents[k], &allStudents[k+1]);
}
////////main//////////////
int main(int argc, char* argv[])
{
StudentType aStudent, allStudents[10];
int nbStudents = 0;
char moreData = 'Y';
while(moreData=='Y') {
aStudent = GetStudentData();
allStudents[nbStudents] = aStudent;
nbStudents++;
bubbleSort( allStudents, nbStudents );
cout << "\nThe information you entered is:" << endl;
PrintallStudents(allStudents, nbStudents);
PrintStudentData(aStudent);
cout << endl << "More data?(y/n): ";
cin >> moreData;
moreData =(moreData=='y'?'Y':moreData);
}
return 0;
}
I hope you understand the code so that I do not need spent most of the time explaining codes I already know.
The problem is simple I want an array to call upon other arrays. I already did a similar function in the getfuncion that has the reference to question label. astudents.id is an example of an array of astudents calling id which is inside the datatype studenttype. Problem is if I want for example to type the id of the function cin>>id; I want it to print or cout all data of data of student like age, major, health, fname, lastname etc.

New Topic/Question
Reply




MultiQuote




|