even more better

This commit is contained in:
whaffman 2025-04-01 11:52:40 +02:00
parent 6f31bee965
commit be1794cd09
2 changed files with 31 additions and 15 deletions

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/03/20 10:44:49 by whaffman #+# #+# */ /* Created: 2025/03/20 10:44:49 by whaffman #+# #+# */
/* Updated: 2025/03/31 14:40:31 by whaffman ######## odam.nl */ /* Updated: 2025/04/01 11:46:24 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -20,6 +20,21 @@ Contact::Contact(const std::string &fn, const std::string &ln, const std::string
: firstName(fn), lastName(ln), nickname(nn), phoneNumber(pn), darkestSecret(ds) : firstName(fn), lastName(ln), nickname(nn), phoneNumber(pn), darkestSecret(ds)
{} {}
void getInput(std::string &input, const std::string &prompt, bool isRequired)
{
std::cout << std::setw(16) << prompt;
std::getline(std::cin, input);
if (isRequired && input.empty())
{
while (input.empty())
{
std::cout << "This field is required. Please enter a value: ";
std::getline(std::cin, input);
}
}
}
Contact Contact::newContact() Contact Contact::newContact()
{ {
std::string firstName; std::string firstName;
@ -29,17 +44,13 @@ Contact Contact::newContact()
std::string darkestSecret; std::string darkestSecret;
std::cout << "---------------------------------------------" << std::endl; std::cout << "---------------------------------------------" << std::endl;
std::cout << "Enter the contact information:" << std::endl << std::endl; std::cout << "Enter the contact information: (firstname and phonenumber are required)" << std::endl << std::endl;
std::cout << std::setw(16) << "first name: "; getInput(firstName, "First name: ", true);
std::getline(std::cin, firstName); getInput(lastName, "Last name: ", false);
std::cout << std::setw(16) << "last name: "; getInput(nickname, "Nickname: ", false);
std::getline(std::cin, lastName); getInput(phoneNumber, "Phone number: ", true);
std::cout << std::setw(16) << "nickname: "; getInput(darkestSecret, "Darkest secret: ", false);
std::getline(std::cin, nickname); std::cout << std::endl;
std::cout << std::setw(16) << "phone number: ";
std::getline(std::cin, phoneNumber);
std::cout << std::setw(16) << "darkest secret: ";
std::getline(std::cin, darkestSecret);
std::cout << "---------------------------------------------" << std::endl; std::cout << "---------------------------------------------" << std::endl;
return (Contact(firstName, lastName, nickname, phoneNumber, darkestSecret)); return (Contact(firstName, lastName, nickname, phoneNumber, darkestSecret));

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/03/20 11:02:32 by whaffman #+# #+# */ /* Created: 2025/03/20 11:02:32 by whaffman #+# #+# */
/* Updated: 2025/04/01 09:17:51 by whaffman ######## odam.nl */ /* Updated: 2025/04/01 11:51:53 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -104,7 +104,7 @@ void PhoneBook::searchContact() const
std::cout << "---------------------------------------------" << std::endl; std::cout << "---------------------------------------------" << std::endl;
int index = -1; int index = -1;
std::cout << "Enter the index of the contact you want to see: "; std::cout << "Enter the index of the contact to see details: (Or EXIT to return to menu) ";
while (index < 0) while (index < 0)
{ {
@ -112,9 +112,14 @@ void PhoneBook::searchContact() const
std::cout << std::endl; std::cout << std::endl;
if (command == "EXIT") if (command == "EXIT")
return; return;
if (command.empty())
{
std::cout << "No input. Please enter a number: ";
continue;
}
if (!isNumeric(command)) if (!isNumeric(command))
{ {
std::cout << "Invalid index. Please enter a number: "; std::cout << "Input is not numeric. Please enter a number: ";
continue; continue;
} }
index = std::stoi(command); index = std::stoi(command);