diff --git a/ex01/src/Contact.cpp b/ex01/src/Contact.cpp index 7e101da..4270e1c 100644 --- a/ex01/src/Contact.cpp +++ b/ex01/src/Contact.cpp @@ -37,7 +37,6 @@ Contact& Contact::operator=(Contact& src) Contact::~Contact() { - std::cout << "je suis finit: " << this << std::endl; } std::string Contact::to_string_partial() const diff --git a/ex01/src/PhoneBook.cpp b/ex01/src/PhoneBook.cpp index 8a618bb..b6d1105 100644 --- a/ex01/src/PhoneBook.cpp +++ b/ex01/src/PhoneBook.cpp @@ -49,7 +49,8 @@ void PhoneBook::display_contacts() Contact* PhoneBook::search(int index) { - if ((size_t) index > this->len || index < 0) + std::cout << this->len << " " << index << std::endl; + if ((size_t) index >= this->len || index < 0) return (NULL); return (&this->contacts[index]); } diff --git a/ex01/src/main.cpp b/ex01/src/main.cpp index 7fd2223..9a565fe 100644 --- a/ex01/src/main.cpp +++ b/ex01/src/main.cpp @@ -38,19 +38,41 @@ int read_contact(Contact& contact) return (0); } +int isdigit(std::string& str) +{ + for (size_t i = 0; i < str.size(); i++) + { + if (!std::isdigit(str[i])) + return(0); + std::cout << i << std::endl; + } + return (1); +} + Contact* search(PhoneBook &phone_book) { - int index; + long index; + char *error; std::string index_str; Contact* contact; contact = NULL; while (contact == NULL) { + index_str = ""; if (get_input(index_str, "index: ")) return (NULL); - index = std::atoi(index_str.c_str()); - contact = phone_book.search(index); + index = std::strtol(index_str.c_str(), &error, 10); + if (errno == ERANGE || error[0] != '\0') + { + std::cerr << "Invalid input" << std::endl; + errno = 0; + } + else { + contact = phone_book.search(index); + if (contact == NULL) + std::cerr << "Contact not found" << std::endl; + } } return (contact); } @@ -71,7 +93,6 @@ int main() { if (read_contact(contact)) return (0); - std::cout << std::endl << "dasd" << std::endl; phone_book.add_contact(contact); } else if (command == "SEARCH")