display vector with struct
Apr 3, 2014 at 5:39am UTC
To start off, I have a menu system. I have a struct, which I put in a vector. I add name and age to to vector. That works fine.
In a seperate function, I want display whats in the vector. How can I do this? Nothing I've tried seems to work
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
struct Patient
{
string name;
string age;
string gender;
};
void getNames(vector<Patient>&);
void displayNames(vector<Patient>&);
int main()
{
vector<Patient> patientData;
string mainMenuSelection;
while (menuSelection != "quit" )
{
cout << "1) add" << endl;
cout << "2) display" << endl;
cout << "3) quit" << endl
cout << "> " ;
cin >> menuSelection;
if (menuSelection == "add" || menuSelection == "1" )
{
getNames(patientData);
}
else if (menuSelection == "display" || menuSelection == "2" )
{
else if (menuSelection == "quit" || menuSelection == "3" )
{
break ; //terminates program
}
else
{
cout << "Invalid entry" << endl;
}
}
return 0;
}
void getNames(vector<Patient>& patientData)
{
system("cls" );
//Header
cout <<"********" << endl;
cout <<"* DATA *" << endl;
cout <<"********" << endl << endl;
cin.ignore();
string name, gender;
int age;
Patient aPatient; //Struct for patient data
aPatient.name = name;
aPatient.age = age;
aPatient.gender = gender;
while (name != "quit" )
{
cout << "Name: (quit to stop) \t" ;
getline(cin, name);
if (name == "quit" )
{
break ;
system("cls" );
}
cout << "Age: \t" ;
getline(cin, age);
cout << "gender: \t" ;
getline(cin, gemder);
vector<Patient>patientData;
patientData.push_back(aPatient);
}
system("cls" );
}
void displayNames(vector<Patient>& patientData)
{
system("cls" ); // clear screen
cout << "*******************" << endl; // page header
cout << "* Display Records *" << endl;
cout << "*******************" << endl << endl;
Patient aPatient;
string name, age, gender;
aPatient.name = name;
aPatient.age = age;
aPatient.gender = gender;
int y = patientData.size();
for (int x = 1; x < y; x++)
{
cout << patientData[x].name; // ??
}
system("PAUSE" );
system("cls" );
}
Apr 3, 2014 at 5:51am UTC
Line 75, you are creating a new vector with the same name as the vector passed as argument to the function. Why are you doing this?
Apr 3, 2014 at 5:55am UTC
That's a mistake...but was deleted.
Apr 4, 2014 at 4:14am UTC
I went back using your code as a example and tried to keep it close to what you had, but made a few changes one being system() due to OS (I'm on linux and System("cls") is windows, but rather than changing it to my OS equivalents I simply removed them though you can add them back if you like).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct Patient{
string name;
string age;
string gender;
};
void getNames(vector<Patient>&);
void displayNames(vector<Patient>&);
int main()
{
vector<Patient> patientData;
string menuSelection;
do {
cout << "1) add" << endl;
cout << "2) display" << endl;
cout << "3) quit" << endl;
cout << "> " ;
cin >> menuSelection;
if (menuSelection == "add" || menuSelection == "1" )
{
getNames(patientData);
}
else if (menuSelection == "display" || menuSelection == "2" )
{
displayNames(patientData);
}
else if (menuSelection == "quit" || menuSelection == "3" )
{
break ;
}
else
{
cout << "Invalid entry" << endl;
}
}while (menuSelection != "quit" || menuSelection != "3" );
return 0;
}
void getNames(vector<Patient> &patientData)
{
Patient aPatient;
cout << "*************" << endl;
cout << "* DATA *" << endl;
cout << "*************" << endl << endl;
cin.ignore();
cout << "Name:\t" ;
getline(cin, aPatient.name);
cout << "Age:\t" ;
getline(cin, aPatient.age);
cout << "Gender:\t" ;
getline(cin, aPatient.gender);
patientData.push_back(aPatient);
}
void displayNames(vector<Patient> &patientData)
{
cout << "*******************" << endl;
cout << "* DISPLAY RECORDS *" << endl;
cout << "*******************" << endl << endl;
for (unsigned int i = 0; i < patientData.size(); i++)
{
cout << patientData[i].name << endl;
}
cout << endl;
}
Topic archived. No new replies allowed.