diff --git a/ex00/src/megaphone.cpp b/ex00/src/megaphone.cpp index 77971a0..0047995 100644 --- a/ex00/src/megaphone.cpp +++ b/ex00/src/megaphone.cpp @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/03/19 16:52:26 by whaffman #+# #+# */ -/* Updated: 2025/03/31 13:38:09 by whaffman ######## odam.nl */ +/* Updated: 2025/04/01 11:37:19 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -28,9 +28,7 @@ int main(int argc, char **argv) { str = argv[i]; for (char c : str) - { std::cout << (char)toupper(c); - } std::cout << " "; } std::cout << std::endl; diff --git a/ex01/inc/PhoneBook.hpp b/ex01/inc/PhoneBook.hpp index 882ea21..abee46f 100644 --- a/ex01/inc/PhoneBook.hpp +++ b/ex01/inc/PhoneBook.hpp @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/03/20 10:53:48 by whaffman #+# #+# */ -/* Updated: 2025/03/31 14:40:58 by whaffman ######## odam.nl */ +/* Updated: 2025/04/01 11:38:02 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -20,16 +20,16 @@ class PhoneBook { public: PhoneBook(); - void run(); + void run(); private: - Contact _contacts[MAX_CONTACTS]; - int _contactCount; - void printContact(const int i) const; - void printAllContacts() const; - std::string truncate(std::string str) const; - void searchContact() const; - void addContact(); + Contact _contacts[MAX_CONTACTS]; + int _contactCount; + void printContact(const int i) const; + void printAllContacts() const; + std::string truncate(std::string str) const; + void searchContact() const; + void addContact(); }; diff --git a/ex01/src/PhoneBook.cpp b/ex01/src/PhoneBook.cpp index 442ebb1..39ca69a 100644 --- a/ex01/src/PhoneBook.cpp +++ b/ex01/src/PhoneBook.cpp @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/03/20 11:02:32 by whaffman #+# #+# */ -/* Updated: 2025/03/31 14:58:09 by whaffman ######## odam.nl */ +/* Updated: 2025/04/01 09:17:51 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,6 +14,7 @@ #include "Contact.hpp" #include #include +#include PhoneBook::PhoneBook() { @@ -78,6 +79,16 @@ void PhoneBook::printContact(const int i) const return; } +bool isNumeric(const std::string &str) +{ + for (char c : str) + { + if (!std::isdigit(c)) + return false; + } + return true; +} + void PhoneBook::searchContact() const { std::string command; @@ -93,38 +104,29 @@ void PhoneBook::searchContact() const std::cout << "---------------------------------------------" << std::endl; int index = -1; + std::cout << "Enter the index of the contact you want to see: "; - while (true) + while (index < 0) { - std::cout << "Enter the index of the contact you want to see: "; std::getline(std::cin, command); std::cout << std::endl; if (command == "EXIT") + return; + if (!isNumeric(command)) + { + std::cout << "Invalid index. Please enter a number: "; + continue; + } + index = std::stoi(command); + if (index < 0 || index >= _contactCount) + { + std::cout << "Invalid index. Please enter a number: "; + index = -1; + } + else break; - if (command.empty()) - { - std::cout << "Invalid index" << std::endl; - continue; - } - try - { - index = std::stoi(command); //0dsfsdf is also valid.... - } - catch (const std::invalid_argument &e) - { - std::cout << "Invalid index" << std::endl; - continue; - } - catch (const std::out_of_range &e) - { - std::cout << "Index out of range" << std::endl; - continue; - } - if (index >= 0 && index < _contactCount) - { - break; - } } + std::cout << "---------------------------------------------" << std::endl; printContact(index); std::cout << "---------------------------------------------" << std::endl;