void AddNewResident (char userOption, RecordType ResidentRecords [], int& counter, string userSocial, string userLastName, string userPhoneNumber, string userTypePhone)
{
bool check = false;
bool flag = false;
char userInput = 0;
do
{
if (counter < MAX_RESIDENTS)
{
while (!flag)
{
cout << "Would you like to enter a new resident (Y/N)? ";
cin >> userInput;
userInput = (toupper (userInput));
if (userInput == 'N')
{
userOption = (DisplayMenu (userOption));
flag = true;
}
elseif (userInput == 'Y')
{
string ReadAndValidateUserLastName (userLastName);
string ReadAndValidateUserSocial (userSocial);
string ReadAndValidateUserPhoneNumber (userPhoneNumber);
string ReadAndValidateUserPhoneType (userTypePhone);
flag = true;
}
if (flag == false)
{
cout << "Invalid Entry! Please try again!" << endl << endl;
}
}
check = true;
}
else
{
cout << "There are too many lines of data!" << endl << endl;
check = false;
userOption = (DisplayMenu (userOption));
}
}
while (!check);
}
Currently, it asks me if i want to add a new resident...then regardless of my input, it ends the program. Any suggestions?
What it SHOULD do is...while the array has room left in it, ask if the user wants to add a new resident, if NO, show the display menu of options again, if YES, it should call functions to prompt, read, and validate ssn, lastname, phone number, and phone type.
If there is no more room in the array, it will display an error saying there are too many lines of data, and return to the display menu of options...
bool prompt(const string msg)
{
char answer;
do {
cout << msg << " (y/n) ";
cin >> answer;
answer = tolower(answer);
// If the user inputs more than one character, there will still be
// characters left in the buffer, which in this case leads to outputting
// the prompt question as many times as there are characters left
// in the buffer.
// The below line makes sure, everything left in the buffer is ignored.
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
} while (answer != 'y' && answer != 'n');
if (answer == 'y')
returntrue;
elsereturnfalse;
}
You should always try to make a function of everything that you reuse. And possibly make it universal, not only for this one case. Instead of a funtion like prompt_continue() or something similar, you have one prompt() for everything and you can type your message like you need it in this case.
With this function you can do this:
1 2 3
while (!prompt()) { // prompt() returned false
// code to execute if the answer was negative
}
Now you don't need this ugly check variable anymore.