d00 ex01 phonebook resolu pbms

This commit is contained in:
Hugo LAMY
2022-02-16 19:41:53 +01:00
parent ae62a6d0d7
commit 1a1074a7c8
7 changed files with 54 additions and 37 deletions

View File

@@ -14,16 +14,24 @@ Contact::~Contact( void ) {
} }
// ADD // ADD
void Contact::add_first( std::string str ) {this->first.assign(str);} void Contact::add_first( std::string str ) {this->_first.assign(str);}
void Contact::add_last( std::string str ) {this->last.assign(str);} void Contact::add_last( std::string str ) {this->_last.assign(str);}
void Contact::add_nick( std::string str ) {this->nick.assign(str);} void Contact::add_nick( std::string str ) {this->_nick.assign(str);}
void Contact::add_num( std::string str ) {this->num.assign(str);} void Contact::add_num( std::string str ) {this->_num.assign(str);}
void Contact::add_secret( std::string str ) {this->secret.assign(str);} void Contact::add_secret( std::string str ) {this->_secret.assign(str);}
// GET // GET
std::string Contact::get_first( void ) {return first;} std::string Contact::get_first( void ) {return _first;}
std::string Contact::get_last( void ) {return last;} std::string Contact::get_last( void ) {return _last;}
std::string Contact::get_nick( void ) {return nick;} std::string Contact::get_nick( void ) {return _nick;}
std::string Contact::get_num( void ) {return num;} std::string Contact::get_num( void ) {return _num;}
std::string Contact::get_secret( void ) {return secret;} std::string Contact::get_secret( void ) {return _secret;}
// CLEAR
void Contact::clear_contact() {
this->_first.clear();
this->_last.clear();
this->_nick.clear();
this->_num.clear();
this->_secret.clear();
}

View File

@@ -23,15 +23,17 @@ public:
std::string get_num(); std::string get_num();
std::string get_secret(); std::string get_secret();
void clear_contact();
void print_contact(); void print_contact();
private: private:
std::string first; std::string _first;
std::string last; std::string _last;
std::string nick; std::string _nick;
std::string num; std::string _num;
std::string secret; std::string _secret;
}; };

View File

@@ -16,51 +16,55 @@ void PhoneBook::add_contact() {
std::string str; std::string str;
int it; int it;
this->_contact_count++; it = this->_contact_count % 8;
it = this->_contact_count; this->_contact[it].clear_contact();
if (it > 8)
it = 1;
it--;
std::cout << B_CYAN "enter a first name pliz :" << std::endl; std::cout << B_CYAN "enter a first name pliz :" << std::endl;
while (_contact[it].get_first().length() == 0) while (this->_contact[it].get_first().length() == 0)
{ {
std::cout << B_BLUE "-> " RESET; std::cout << B_BLUE "-> " RESET;
std::getline(std::cin, str); if (!std::getline(std::cin, str))
return;
this->_contact[it].add_first(str); this->_contact[it].add_first(str);
} }
std::cout << B_CYAN "enter a last name pliz :" << std::endl; std::cout << B_CYAN "enter a last name pliz :" << std::endl;
while (_contact[it].get_last().length() == 0) while (this->_contact[it].get_last().length() == 0)
{ {
std::cout << B_BLUE "-> " RESET; std::cout << B_BLUE "-> " RESET;
std::getline(std::cin, str); if (!std::getline(std::cin, str))
return;
this->_contact[it].add_last(str); this->_contact[it].add_last(str);
} }
std::cout << B_CYAN "enter a nickname pliz :" << std::endl; std::cout << B_CYAN "enter a nickname pliz :" << std::endl;
while (_contact[it].get_nick().length() == 0) while (this->_contact[it].get_nick().length() == 0)
{ {
std::cout << B_BLUE "-> " RESET; std::cout << B_BLUE "-> " RESET;
std::getline(std::cin, str); if (!std::getline(std::cin, str))
return;
this->_contact[it].add_nick(str); this->_contact[it].add_nick(str);
} }
std::cout << B_CYAN "enter a number pliz :" << std::endl; std::cout << B_CYAN "enter a number pliz :" << std::endl;
while (_contact[it].get_num().length() == 0) while (this->_contact[it].get_num().length() == 0)
{ {
std::cout << B_BLUE "-> " RESET; std::cout << B_BLUE "-> " RESET;
std::getline(std::cin, str); if (!std::getline(std::cin, str))
return;
this->_contact[it].add_num(str); this->_contact[it].add_num(str);
} }
std::cout << B_CYAN "enter a secret of this contact pliz :" << std::endl; std::cout << B_CYAN "enter a secret of this contact pliz :" << std::endl;
while (_contact[it].get_secret().length() == 0) while (this->_contact[it].get_secret().length() == 0)
{ {
std::cout << B_BLUE "-> " RESET; std::cout << B_BLUE "-> " RESET;
std::getline(std::cin, str); if (!std::getline(std::cin, str))
return;
this->_contact[it].add_secret(str); this->_contact[it].add_secret(str);
} }
this->_contact_count++;
} }
std::string truncate(std::string str, size_t len) { std::string truncate(std::string str, size_t len) {
@@ -117,19 +121,22 @@ void PhoneBook::print_contact( int id ) {
void PhoneBook::search_contact() { void PhoneBook::search_contact() {
std::string index; std::string index;
int id; long id;
char *ret;
if (this->print_phonebook() == 0) if (this->print_phonebook() == 0)
return; return;
id = -1;
std::cout << std::endl << B_CYAN "choose an index :" << std::endl; std::cout << std::endl << B_CYAN "choose an index :" << std::endl;
while (id < 0 || id > this->_contact_count - 1 || id > 7) while (1)
{ {
std::cout << B_BLUE "-> " RESET; std::cout << B_BLUE "-> " RESET;
std::getline(std::cin, index); if (!std::getline(std::cin, index))
id = std::atoi(index.c_str()); return;
if (id < 0 || id > this->_contact_count - 1 || id > 7) id = std::strtol(index.c_str(), &ret, 10);
if (*ret || id < 0 || id > this->_contact_count - 1 || id > 7)
std::cout << B_RED "sorry, not a valid index" RESET << std::endl; std::cout << B_RED "sorry, not a valid index" RESET << std::endl;
else
break;
} }
this->print_contact(id); this->print_contact(id);

Binary file not shown.

Binary file not shown.

View File

@@ -9,7 +9,7 @@ int main() {
std::string cmd; std::string cmd;
PhoneBook YellowPage; PhoneBook YellowPage;
while (1) while (std::cin)
{ {
std::cout << std::endl; std::cout << std::endl;
std::cout << B_YELLOW "enter a command: ADD / SEARCH / EXIT" << std::endl; std::cout << B_YELLOW "enter a command: ADD / SEARCH / EXIT" << std::endl;

Binary file not shown.