correct cpp00

This commit is contained in:
Alexandre POMMIER 2022-07-22 12:47:34 +02:00
parent 2e27e82baa
commit 22c98ea257
7 changed files with 58 additions and 52 deletions

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/15 01:47:56 by apommier #+# #+# */ /* Created: 2022/04/15 01:47:56 by apommier #+# #+# */
/* Updated: 2022/04/20 05:18:29 by apommier ### ########.fr */ /* Updated: 2022/07/22 12:00:25 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -28,9 +28,10 @@ int main(int ac, char **av)
} }
i = 0; i = 0;
while (av[++i]) while (av[++i])
std::cout << av[i] << std::endl; std::cout << av[i];
} }
else else
std::cout << "* LOUD AND UNBEARABLE FEEDBACK NOISE *" << std::endl; std::cout << "* LOUD AND UNBEARABLE FEEDBACK NOISE *";
std::cout << std::endl;
return (0); return (0);
} }

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/15 10:37:20 by apommier #+# #+# */ /* Created: 2022/04/15 10:37:20 by apommier #+# #+# */
/* Updated: 2022/07/21 20:05:15 by apommier ### ########.fr */ /* Updated: 2022/07/22 12:41:59 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,10 +14,8 @@
PhoneBook::PhoneBook(void) PhoneBook::PhoneBook(void)
{ {
this->_NbrContact = -1;
while (this->_NbrContact < 8)
this->_Contact[this->_NbrContact++] = 0;
this->_NbrContact = 0; this->_NbrContact = 0;
this->_nbrContactSet = 0;
} }
PhoneBook::~PhoneBook(void) PhoneBook::~PhoneBook(void)
@ -26,27 +24,26 @@ PhoneBook::~PhoneBook(void)
void PhoneBook::StoreContact(Contact *NewContact) void PhoneBook::StoreContact(Contact *NewContact)
{ {
if (_Contact[this->_NbrContact]) if (this->_NbrContact >= 8)
delete _Contact[this->_NbrContact] ;
_Contact[this->_NbrContact] = NewContact;
if (this->_NbrContact == 8)
this->_NbrContact = 0; this->_NbrContact = 0;
else _Contact[this->_NbrContact] = *NewContact;
this->_NbrContact++; this->_NbrContact++;
this->_nbrContactSet++;
} }
void PhoneBook::PrintContact(int index) void PhoneBook::PrintContact(int index)
{ {
if (!this->_Contact[index]) if (index >= 8 || index >= this->_nbrContactSet)
{ {
std::cout << "There is no contact at this index" << std::endl; std::cout << "There is no contact at this index" << std::endl;
return ; return ;
} }
std::cout << std::setw(10) << "First name: " << this->_Contact[index]->getOneInfo(0) << std::endl; std::cout << std::setw(10) << "First name: " << this->_Contact[index].getOneInfo(0) << std::endl;
std::cout << std::setw(10) << "Last name: " << this->_Contact[index]->getOneInfo(1) << std::endl; std::cout << std::setw(10) << "Last name: " << this->_Contact[index].getOneInfo(1) << std::endl;
std::cout << std::setw(10) << "Nickname: " << this->_Contact[index]->getOneInfo(2) << std::endl; std::cout << std::setw(10) << "Nickname: " << this->_Contact[index].getOneInfo(2) << std::endl;
std::cout << std::setw(10) << "Phone number: " << this->_Contact[index]->getOneInfo(3) << std::endl; std::cout << std::setw(10) << "Phone number: " << this->_Contact[index].getOneInfo(3) << std::endl;
std::cout << std::setw(10) << "Darkest secret: " << this->_Contact[index]->getOneInfo(4) << std::endl; std::cout << std::setw(10) << "Darkest secret: " << this->_Contact[index].getOneInfo(4) << std::endl;
} }
void PhoneBook::PrintIndex() void PhoneBook::PrintIndex()
@ -55,12 +52,12 @@ void PhoneBook::PrintIndex()
std::string nbr; std::string nbr;
while (index < 8 && this->_Contact[index]) while (index < 8 && this->_nbrContactSet > index)
{ {
std::cout << std::setw(10) << index + 1 << "|"; std::cout << std::setw(10) << index + 1 << "|";
std::cout << std::setw(10) << this->_Contact[index]->GetInfo(0) << "|"; std::cout << std::setw(10) << this->_Contact[index].GetInfo(0) << "|";
std::cout << std::setw(10) << this->_Contact[index]->GetInfo(1) << "|"; std::cout << std::setw(10) << this->_Contact[index].GetInfo(1) << "|";
std::cout << std::setw(10) << this->_Contact[index]->GetInfo(2) << std::endl; std::cout << std::setw(10) << this->_Contact[index].GetInfo(2) << std::endl;
index++; index++;
} }
if (!index) if (!index)
@ -70,7 +67,12 @@ void PhoneBook::PrintIndex()
while (1) while (1)
{ {
std::cout << "Enter index of contact: "; std::cout << "Enter index of contact: ";
std::cin >> nbr; std::getline(std::cin, nbr);
if (std::cin.eof())
{
std::cout << "Error: end of file\n";
exit(0);
}
if (nbr.size() != 1 || !isdigit(nbr[0])) if (nbr.size() != 1 || !isdigit(nbr[0]))
std::cout << "Bad entry" << std::endl; std::cout << "Bad entry" << std::endl;
else else
@ -80,7 +82,6 @@ void PhoneBook::PrintIndex()
std::cout << "Bad entry" << std::endl; std::cout << "Bad entry" << std::endl;
else else
{ {
std::cout << index << "index \n";
this->PrintContact(index - 1); this->PrintContact(index - 1);
return ; return ;
} }
@ -88,9 +89,3 @@ void PhoneBook::PrintIndex()
} }
} }
} }
void PhoneBook::deleteContact()
{
for (int i = 0; this->_Contact[i] && i < 8; i++)
delete this->_Contact[i];
}

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/15 10:37:24 by apommier #+# #+# */ /* Created: 2022/04/15 10:37:24 by apommier #+# #+# */
/* Updated: 2022/07/21 19:58:59 by apommier ### ########.fr */ /* Updated: 2022/07/22 12:32:50 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -24,12 +24,13 @@ class PhoneBook {
void StoreContact(Contact *NewContact); void StoreContact(Contact *NewContact);
void PrintContact(int index); void PrintContact(int index);
void PrintIndex(); void PrintIndex();
void deleteContact(); //void deleteContact();
private: private:
int _NbrContact; int _NbrContact;
class Contact *_Contact[8]; int _nbrContactSet;
Contact _Contact[8];
}; };
#endif #endif

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/17 15:55:40 by apommier #+# #+# */ /* Created: 2022/04/17 15:55:40 by apommier #+# #+# */
/* Updated: 2022/07/21 20:01:53 by apommier ### ########.fr */ /* Updated: 2022/07/22 12:46:19 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -33,20 +33,25 @@ void add_contact(PhoneBook *Book )
int index = 0; int index = 0;
std::string line; std::string line;
std::string info[5]; std::string info[5];
Contact *Contact = new class Contact; Contact Contact;
while (index < 5) while (index < 5)
{ {
PrintMessage(index); PrintMessage(index);
std::getline(std::cin, line); std::getline(std::cin, line);
if (std::cin.eof())
{
std::cout << "\nError: end of file\n";
exit(0);
}
info[index] = line; info[index] = line;
if (index == 3 && line.find_first_not_of("0123456789") != std::string::npos) if (index == 3 && line.find_first_not_of("0123456789") != std::string::npos)
index = 3; index = 3;
else if (!line.empty() && line.find_last_not_of(" ") != std::string::npos ) else if (!line.empty() && line.find_last_not_of(" ") != std::string::npos )
index++; index++;
} }
Contact->FillContact(info); Contact.FillContact(info);
Book->StoreContact(Contact); Book->StoreContact(&Contact);
} }
int main(int ac, char **av) int main(int ac, char **av)
@ -61,21 +66,21 @@ int main(int ac, char **av)
std::cout << "Too much arguments\n"; std::cout << "Too much arguments\n";
return (0); return (0);
} }
std::cout << "Enter a command : ADD | SEARCH | EXIT" << std::endl;
while (line != "EXIT") while (line != "EXIT")
{ {
std::cout << "Enter a command : ADD | SEARCH | EXIT" << std::endl;
std::getline(std::cin, line); std::getline(std::cin, line);
if (line != "SEARCH") if (std::cin.eof())
std::cout << "Enter a command : ADD | SEARCH | EXIT1" << std::endl; {
std::cout << "Error: end of file\n";
exit(0);
}
if (line == "ADD") if (line == "ADD")
add_contact(&book); add_contact(&book);
else if (line == "SEARCH") else if (line == "SEARCH")
book.PrintIndex(); book.PrintIndex();
else if (line != "EXIT" && !line.empty()) else if (line != "EXIT" && !line.empty())
std::cout << "Invalid command" << std::endl; std::cout << "Invalid command" << std::endl;
if (line != "SEARCH" && line.size())
std::cout << "Enter a command : ADD | SEARCH | EXIT2" << std::endl;
} }
book.deleteContact();
return (0); return (0);
} }

View File

@ -125,7 +125,7 @@ void Account::_displayTimestamp( void )
time (&rawtime); time (&rawtime);
timeinfo = localtime(&rawtime); timeinfo = localtime(&rawtime);
strftime (buffer, 20, "[%G%m%d_%H%M%S]", timeinfo); strftime (buffer, 20, "[%G%m%d_%H%M%S]", timeinfo);
/std::cout << buffer;
//const char *buffeer = "[19920104_091532]"; //const char *buffeer = "[19920104_091532]";
//std::cout << buffeer; //std::cout << buffeer;
std::cout << buffer;
} }

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/21 23:39:20 by apommier #+# #+# */ /* Created: 2022/06/21 23:39:20 by apommier #+# #+# */
/* Updated: 2022/07/22 09:52:45 by apommier ### ########.fr */ /* Updated: 2022/07/22 11:22:21 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -50,20 +50,24 @@ int Harl::chooseComplain(std::string level)
void Harl::debug() void Harl::debug()
{ {
std::cout << "I love having extra bacon for my 7XL-double-cheese-triple-pickle-specialketchup burger. I really do!\n"; std::cout << "[ DEBUG ]\n";
std::cout << "I love having extra bacon for my 7XL-double-cheese-triple-pickle-specialketchup burger. I really do!\n\n";
} }
void Harl::info() void Harl::info()
{ {
std::cout << "I cannot believe adding extra bacon costs more money. You didnt putenough bacon in my burger! If you did, I wouldnt be asking for more!\n"; std::cout << "[ INFO ]\n";
std::cout << "I cannot believe adding extra bacon costs more money. You didn't putenough bacon in my burger! If you did, I wouldn't be asking for more!\n\n";
} }
void Harl::warning() void Harl::warning()
{ {
std::cout << "I think I deserve to have some extra bacon for free. Ive been coming for years whereas you started working here since last month.\n"; std::cout << "[ WARNING ]\n";
std::cout << "I think I deserve to have some extra bacon for free. I've been coming for years whereas you started working here since last month.\n\n";
} }
void Harl::error() void Harl::error()
{ {
std::cout << "This is unacceptable! I want to speak to the manager now.\n"; std::cout << "[ ERROR ]\n";
std::cout << "This is unacceptable! I want to speak to the manager now.\n\n";
} }

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/18 21:34:42 by apommier #+# #+# */ /* Created: 2022/07/18 21:34:42 by apommier #+# #+# */
/* Updated: 2022/07/19 12:55:48 by apommier ### ########.fr */ /* Updated: 2022/07/22 11:13:32 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */