Finishing the ex01

This commit is contained in:
Raphael 2024-10-20 21:38:50 +02:00
parent 1e42383953
commit e299deef16
5 changed files with 122 additions and 50 deletions

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/17 14:55:18 by rparodi #+# #+# */
/* Updated: 2024/10/18 15:58:15 by rparodi ### ########.fr */
/* Updated: 2024/10/20 21:38:07 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
@ -18,7 +18,7 @@
* @param err_msg The message return in case of error
* @return the value of input
*/
std::string get_input(const char *err_msg)
std::string get_input(const char *err_msg, std::string file, int line)
{
std::string input;
@ -26,10 +26,12 @@ std::string get_input(const char *err_msg)
{
if (!std::getline(std::cin, input))
throw std::runtime_error("getline can't read the input !\n");
else if (input.empty())
throw std::runtime_error("The input can't be empty !\n");
}
catch (const std::exception& err)
{
std::cerr << CLR_BOLD_RED << "\nError:\t" << err.what() << CLR_RESET << CLR_RED << "(" << err_msg << ")" << CLR_RESET << std::endl;
std::cerr << CLR_BOLD_RED << "\nError:\t" << err.what() << CLR_RESET << CLR_RED << "(" << err_msg << " [" << file << ":" << line << "]" << ")" << CLR_RESET << std::endl;
exit(1);
}
return (input);
@ -44,13 +46,18 @@ int main()
while (true)
{
std::cout << MENU << std::endl;
input = get_input("SELECT MENU [main.cpp:38]");
input = get_input("Main menu", __FILE_NAME__, __LINE__);
if (strcmp(input.c_str(), "ADD") == 0)
{
phonebook.array_contact[i].init_new(i);
i++;
if (phonebook.array_contact[i].init_new(i) == true)
i++;
}
if (strcmp(input.c_str(), "SEARCH") == 0)
phonebook.search(i);
if (strcmp(input.c_str(), "EXIT") == 0)
{
std::cout << CLR_LIGHT_RED << "Leaving the program..." << CLR_RESET << std::endl;
exit(1);
}
}
}