Restarting ex01

This commit is contained in:
Raphael 2024-10-18 15:17:20 +02:00
parent 77c225d31e
commit e657cd8012
8 changed files with 182 additions and 183 deletions

8
cpp00/ex01/.clangd Normal file
View file

@ -0,0 +1,8 @@
CompileFlags:
Compiler: clang
Add:
- "-xc"
- "-I/usr/local/include"
- "-I/nix/store/pzmpyb9hxfv60vh0l67avr94h71nip1b-llvm-16.0.6-dev/include"
- "-I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include"
- "-I/Users/raphael/Documents/piscine/cpp/cpp00/ex01/includes/"

View file

@ -6,7 +6,7 @@
# By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ # # By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2023/11/12 11:05:05 by rparodi #+# #+# # # Created: 2023/11/12 11:05:05 by rparodi #+# #+# #
# Updated: 2024/10/14 23:43:03 by rparodi ### ########.fr # # Updated: 2024/10/17 15:36:20 by rparodi ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -21,14 +21,14 @@ RM = rm -rf
# Flags # Flags
# Mandatory flags for 42 cpp # Mandatory flags for 42 cpp
CXXFLAGS = -Werror -Wextra -Wall -std=c++98 CXXFLAGS = -Werror -Wextra -Wall -std=c++98 -I./includes/
# Flags to debug and have the dependences (can be removed for correction) # Flags to debug and have the dependences (can be removed for correction)
CXXFLAGS += -MMD -g3 CXXFLAGS += -MMD -g3
# Flag to debug (TO REMOVE) # Flag to debug (TO REMOVE)
CXXFLAGS += -D DEBUG=1 CXXFLAGS += -D DEBUG=1
# Sources # Sources
SRC = ./phonebook.cpp \ SRC = ./sources/phonebook.cpp \
./main.cpp ./sources/main.cpp
# Objects # Objects
OBJDIRNAME = ./build OBJDIRNAME = ./build

View file

@ -0,0 +1,47 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* color.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/17 15:27:54 by rparodi #+# #+# */
/* Updated: 2024/10/17 15:48:22 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef COLOR_HPP
#define COLOR_HPP
#define CLR_RESET "\033[0m"
#define CLR_BLACK "\033[0;30m"
#define CLR_RED "\033[0;31m"
#define CLR_GREEN "\033[0;32m"
#define CLR_YELLOW "\033[0;33m"
#define CLR_BLUE "\033[0;34m"
#define CLR_MAGENTA "\033[0;35m"
#define CLR_CYAN "\033[0;36m"
#define CLR_WHITE "\033[0;37m"
#define CLR_GOLD "\033[38;5;220m"
#define CLR_GREY "\033[38;5;240m"
#define CLR_LIGHT_BLACK "\033[0;90m"
#define CLR_LIGHT_RED "\033[0;91m"
#define CLR_LIGHT_GREEN "\033[0;92m"
#define CLR_LIGHT_YELLOW "\033[0;93m"
#define CLR_LIGHT_BLUE "\033[0;94m"
#define CLR_LIGHT_MAGENTA "\033[0;95m"
#define CLR_LIGHT_CYAN "\033[0;96m"
#define CLR_LIGHT_WHITE "\033[0;97m"
#define CLR_BOLD_BLACK "\033[1;30m"
#define CLR_BOLD_RED "\033[1;31m"
#define CLR_BOLD_GREEN "\033[1;32m"
#define CLR_BOLD_YELLOW "\033[1;33m"
#define CLR_BOLD_BLUE "\033[1;34m"
#define CLR_BOLD_MAGENTA "\033[1;35m"
#define CLR_BOLD_CYAN "\033[1;36m"
#define CLR_BOLD_WHITE "\033[1;37m"
#endif

View file

@ -5,42 +5,51 @@
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/20 18:59:14 by rparodi #+# #+# */ /* Created: 2024/10/17 14:56:49 by rparodi #+# #+# */
/* Updated: 2024/10/15 00:26:50 by rparodi ### ########.fr */ /* Updated: 2024/10/18 12:18:04 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef PHONEBOOK_H #ifndef PHONEBOOK_HPP
#define PHONEBOOK_H # define PHONEBOOK_HPP
#include <iostream> #include <iostream>
#include <ostream> #include <iomanip>
#include <string> #include <string>
#include <stddef.h>
#include <cstdlib>
#include <cstring> #include <cstring>
#include <ctime> #include "color.hpp"
#define MENU_TEXT "You're in the main menu:\nHere's the command:\n\t- " << BLUE << "ADD: " << RESET << "To add a new contact\n\t- " BLUE << "SEARCH: " << RESET "To search a contact\n\t- " BLUE << "EXIT: " << RESET "to exit the program" #define ERROR 1
#define GOLD "\033[38;5;220m" #define NO_ERROR 0
#define GREY "\033[0;90m"
#define RED "\033[31m"
#define GREEN "\033[32m"
#define BLUE "\033[34m"
#define RESET "\033[0m"
#ifndef DEBUG std::string get_input(const char *str);
# define DEBUG 0
#endif
class contact { class Contact
{
public: public:
int id; /*Contact();*/
std::string number; /*~Contact();*/
std::string first_name; void init_new(int id);
std::string last_name; void print();
std::string nickname; private:
std::string dark_secret; int _id;
struct tm *creation_time; std::string _first_name;
void _debug(void); std::string _last_name;
std::string _nickname;
std::string _number;
std::string _secret;
}; };
class PhoneBook
{
public:
void add();
void search();
private:
Contact _array_contact[8];
};
#endif #endif

View file

@ -1,127 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/20 19:42:14 by rparodi #+# #+# */
/* Updated: 2024/10/15 00:50:51 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "phonebook.hpp"
#include <ctime>
#include <iostream>
int get_older_contact(contact array_contact[8])
{
int ret = 0;
time_t remain = 0;
for (int i = 0; i < 8; i++)
{
time_t tmp = mktime(array_contact[i].creation_time);
if (tmp == -1)
{
std::cerr << RED << "\nError: during the time process\n" << RESET << std::endl;
exit(1);
}
if (tmp > remain)
{
remain = mktime(array_contact[i].creation_time);
if (remain == -1)
{
std::cerr << RED << "\nError: during the time process\n" << RESET << std::endl;
exit(1);
}
ret = i;
}
}
return (ret);
}
/**
* @brief Adding a new contact
*
* @param index index for the new contact
* @param array_contact the array asked by the subject
* @return true on error
*/
bool adding(int index, contact array_contact[8])
{
std::string input;
if (index > 8 || index < 0)
return (true);
std::cout << "Thanks to give the number of the new contact" << std::endl;
std::getline(std::cin, input);
if (input.length() != 10)
return (true);
array_contact[index].number = input;
std::cout << "Thanks to give the nickname of the new contact" << std::endl;
std::getline(std::cin, input);
array_contact[index].nickname = input;
std::cout << "Thanks to give the first_name of the new contact" << std::endl;
std::getline(std::cin, input);
array_contact[index].first_name = input;
std::cout << "Thanks to give the last_name of the new contact" << std::endl;
std::getline(std::cin, input);
array_contact[index].last_name = input;
std::cout << "Thanks to give the dark secret of the new contact" << std::endl;
std::getline(std::cin, input);
array_contact[index].dark_secret = input;
array_contact[index].id = index;
time_t now = time(0);
array_contact[index].creation_time = localtime(&now);
if (DEBUG)
array_contact[index]._debug();
return (false);
}
bool searching(contact contact[8], int index)
{
if (index == 0)
return (true);
for (int i = 0; i < index; i++)
contact[i]._debug();
return (false);
}
int main(void)
{
contact array_contact[8];
int i;
std::string input;
i = 0;
while (true)
{
std::cout << MENU_TEXT << std::endl;
std::getline(std::cin, input);
if (strcmp(input.c_str(), "ADD") == 0)
{
if (adding(i, array_contact))
std::cerr << RED << "\nError: during the adding process\n" << RESET << std::endl;
else
i++;
}
else if (strcmp(input.c_str(), "SEARCH") == 0)
{
if (searching(array_contact, i))
std::cerr << RED << "\nError: during the searching process\n" << RESET << std::endl;
}
else if (strcmp(input.c_str(), "EXIT") == 0)
return (0);
else
std::cerr << RED << "\nError: no option reconnise\n" << RESET << std::endl;
input.clear();
}
}

View file

@ -1,27 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* phonebook.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/20 19:50:03 by rparodi #+# #+# */
/* Updated: 2024/10/15 00:32:03 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "phonebook.hpp"
void contact::_debug() {
std::cout << GOLD << "\n\n\tDEBUG MODE ENABLE\n" << RESET << std::endl;
std::cout << GREY;
std::cout << "First Name:\t" << first_name << std::endl;
std::cout << "Last Name:\t" << last_name << std::endl;
std::cout << "Nickname:\t" << nickname << std::endl;
std::cout << "Number:\t\t" << number << std::endl;
std::cout << "Dark Secret:\t" << dark_secret << std::endl;
std::cout << "ID:\t\t" << id << std::endl;
std::cout << "Creation Hour:\t" << creation_time->tm_hour << ":" << creation_time->tm_min << ":" << creation_time->tm_sec << std::endl;
std::cout << RESET << std::endl;
}

View file

@ -0,0 +1,56 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/17 14:55:18 by rparodi #+# #+# */
/* Updated: 2024/10/18 12:19:07 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "phonebook.hpp"
/**
* @brief return the input with std::getline()
*
* @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 input;
try
{
if (!std::getline(std::cin, input))
throw std::runtime_error("getline can't read the input !\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;
exit(1);
}
return (input);
}
int main()
{
std::string input;
Contact array_contact[8];
size_t i = 0;
while (true)
{
input = get_input("SELECT MENU [main.cpp:38]");
if (strcmp(input.c_str(), "ADD"))
{
std::cout << "adding" << std::endl;
array_contact[i].init_new(i);
i++;
}
if (strcmp(input.c_str(), "EXIT"))
exit(1);
}
}

View file

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* phonebook.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/17 15:09:21 by rparodi #+# #+# */
/* Updated: 2024/10/17 16:44:08 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "phonebook.hpp"
void Contact::init_new(int id)
{
_id = id;
_first_name = get_input("first_name [phonebook.cpp:18]");
_last_name = get_input("last_name [phonebook.cpp:19]");
_nickname = get_input("nickname [phonebook.cpp:20]");
_number = get_input("number [phonebook.cpp:21]");
_secret = get_input("secret [phonebook.cpp:22]");
}
void Contact::print()
{
std::cout << "|" << std::setw(10) << _id << "|";
std::cout << "|" << std::setw(10) << _first_name << "|";
std::cout << "|" << std::setw(10) << _last_name << "|";
std::cout << "|" << std::setw(10) << _nickname << "|";
std::cout << "|" << std::setw(10) << _number << "|";
std::cout << "|" << std::setw(10) << _secret << "|";
}