even better

This commit is contained in:
whaffman 2025-04-01 11:39:41 +02:00
parent 31ed280bb9
commit 6f31bee965
3 changed files with 38 additions and 38 deletions

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/03/19 16:52:26 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]; str = argv[i];
for (char c : str) for (char c : str)
{
std::cout << (char)toupper(c); std::cout << (char)toupper(c);
}
std::cout << " "; std::cout << " ";
} }
std::cout << std::endl; std::cout << std::endl;

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/03/20 10:53:48 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 { class PhoneBook {
public: public:
PhoneBook(); PhoneBook();
void run(); void run();
private: private:
Contact _contacts[MAX_CONTACTS]; Contact _contacts[MAX_CONTACTS];
int _contactCount; int _contactCount;
void printContact(const int i) const; void printContact(const int i) const;
void printAllContacts() const; void printAllContacts() const;
std::string truncate(std::string str) const; std::string truncate(std::string str) const;
void searchContact() const; void searchContact() const;
void addContact(); void addContact();
}; };

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/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 "Contact.hpp"
#include <iostream> #include <iostream>
#include <iomanip> #include <iomanip>
#include <cctype>
PhoneBook::PhoneBook() PhoneBook::PhoneBook()
{ {
@ -78,6 +79,16 @@ void PhoneBook::printContact(const int i) const
return; return;
} }
bool isNumeric(const std::string &str)
{
for (char c : str)
{
if (!std::isdigit(c))
return false;
}
return true;
}
void PhoneBook::searchContact() const void PhoneBook::searchContact() const
{ {
std::string command; std::string command;
@ -93,38 +104,29 @@ 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: ";
while (true) while (index < 0)
{ {
std::cout << "Enter the index of the contact you want to see: ";
std::getline(std::cin, command); std::getline(std::cin, command);
std::cout << std::endl; std::cout << std::endl;
if (command == "EXIT") 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; 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; std::cout << "---------------------------------------------" << std::endl;
printContact(index); printContact(index);
std::cout << "---------------------------------------------" << std::endl; std::cout << "---------------------------------------------" << std::endl;