I'm working on a project where the user will input various amounts of data. There will only be one user, but the user can put in various amounts of data about other people like first and last name, age, an ID number, and location.
I assume that I could use a class/object to store all of the information for each individual. But I've planned for the program to prompt the user for the number of individuals he wants to create. I could make a set number of classes available by default, but the user would have to complete that form, then start a new one if more than the default number of individuals were needed.
How would I code the program to allow the program to create new classes according to the user's need? Is there a way to create new classes as needed while the program is running?
It's not necessary at the moment, but I'd also like to create a unique ID number for each case that the program uses. Is there a way for me to do that in a way that the computer would not repeat ID numbers, if the random() function was used?
Review classes again, specifically dynamically allocating objects. Just create a function that you can call repeatedly to create a new object, and then place it into a list of objects. And you can, in fact, make a unique user ID for each one. Again, review classes.
Well, the focus of the program would be for the user to input data, so i really only need the user to input their data. No need for multiple users, or IDs. From what I've read so far about classes and dynamically allocated objects are combining vectors and/or pointers with classes. But I haven't found anything about being able to uniquely identify each patient (for lack of a better word) and call that object by name. It looks as if I'd have to go through the entire list to get to that one object, using linked lists.
I know with a vector I can add nodes if needed, but they'll all have the same name. Plus, i'm not exactly sure how I'd reference the members of the patient class from within the vector. I'll keep looking though.
class CMortgage{ // Define the class. This class has the data storage and manipulations.
private:
string fName, lName; // Variables
double loanVal, payAmt, interest; // Variables
int matTerm; // Variables
public:
void getValues(); // Functions
void print();
void printName();
virtualvoid pmtCalc() =0; // Virtual Functions (don't exist in the base class, only inherited classes)
virtualvoid amortize() =0;
};
class CMenu{ // Different class. This one is administrative, it runs the user input.
vector<CMortgage *> morts; // Vector of pointers to class objects of type CMortgage
vector<CMortgage *>::iterator it; // Iterator to point at pointers to class objects of type CMortgage
void newLoan(); // Functions
void selLoan();
void editLoan();
public:
int menu();
};
void CMenu::newLoan(){ // Function creates a new CMortgage from user input.
int sel;
CMortgage * mort; // Define a pointer to CMortgage class.
/*
Unnecessary code.
*/
switch(sel){
case 1:
mort = new CSimple; // Create a new child class of type CSimple with a pointer to it's parent class CMortgage.
mort->getValues();
mort->print();
morts.push_back(mort); // Store the pointer in the vector.
break;
case 2:
mort = new CSeventyTwo; // Create a new child class of type CSeventyTwo with a pointer to it's parent class CMortgage.
mort->getValues();
mort->print();
morts.push_back(mort); // Store the pointer in the vector.
break;
case 3:
mort = new CFixed; // Create a new child class of type CFixed with a pointer to it's parent class CMortgage.
mort->getValues();
mort->print();
morts.push_back(mort); // Store the pointer in the vector.
break;
default:
cout << "That is not a valid choice!" << endl;
newLoan();
}
}
Here is how to locate a specific instance of the class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
void CMenu::selLoan(){ // Function of a class.
int sel, i = 1;
it = morts.begin(); // Set the iterator to the beginning of the vector.
while(it != morts.end()){ // Until the end of the vector,
cout << i << ". "; // Print the number for a line,
(*it)->printName(); // Then print the fName and lName values from CMortgage,
++it; // Go to the next item in the list,
++i; // And finally, make the next line the next number
}
cout << "$ "; // User shell input prompt
cin >> sel; // Get input from the user
it = morts.begin(); // Set the iterator to the beginning of the vector.
advance(it, sel-1); // Move the iterator to the item selected by the user. It's (sel - 1) because vectors start at 0, and the list as printed starts at 1.
}
@ne555: So it's the same type, and can be used as if it is, in fact, a member of the class.
On second thought, do you mean what's the point of declaring the iterator inside of the class scope of class CMenu?
That's so that the iterator keeps it's position when I move to a different function within the parameters of the class; in that way, I only have to write one function to put the iterator where I want it whenever I go looking for something, instead of having to write it three times, one each for editing, deleting and printing.
The book I've been reading (Beginning C++ Through Game Programming) first introduced traversing vectors similar to how you would traverse an array - you'll see in the examples of code I have.
I know it's messed up, but this is what I got after hacking it out for a few days... I've still got to read the rest of the sections following the vector and class sections (even those 2 subjects are 5 chapters apart)...
void OfficerPackage()
{
int numberToReport = 0;
char addOfficer = 'n';
cout << "Enter the officer(s) data below..." << endl << endl;
// It compiles fine, the message above is displayed but I get a runtime error as soon as this following part of the code come up.
officer.push_back();
do
{
officer[0].promptOfficerData(0); // one of the examples I was talking about
cout << "Add another officer?(Y/N): ";
cin >> addOfficer;
officer.push_back(officer[0]);
numberToReport = officer.size();
officer[numberToReport].promptOfficerData(numberToReport);
cout << "Add another officer? (Y/N): ";
cin >> addOfficer;
if((addOfficer == 'Y') || (addOfficer == 'y'))
{
officer.push_back(officer[0]);
cout << "New officer created..." << endl << endl;
}
else
{
numberToReport = officer.size();
}
} while((addOfficer != 'Y') || (addOfficer != 'y'));
officer[numberToReport].printOfficerData(numberToReport);
officer[numberToReport].correctOfficerData();
officer[numberToReport].printOfficerData(numberToReport);
}
So the only way to traverse a vector, or combine a vector with a class is with pointers and iterators?