From 0458a2f2fb93abcbf5f400e5eb1121a24e752ae1 Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Mon, 9 Nov 2020 12:35:58 +0100 Subject: Fixing cpp00/ex01 --- cpp00/ex01/Contact.cpp | 98 ++++++++++++++++++++++++++++++++---------------- cpp00/ex01/Contact.hpp | 3 +- cpp00/ex01/PhoneBook.cpp | 4 +- cpp00/ex01/main.cpp | 7 ++-- cpp00/ex01/utils.cpp | 8 ++-- 5 files changed, 78 insertions(+), 42 deletions(-) (limited to 'cpp00') diff --git a/cpp00/ex01/Contact.cpp b/cpp00/ex01/Contact.cpp index 12f06db..e8a9c0c 100644 --- a/cpp00/ex01/Contact.cpp +++ b/cpp00/ex01/Contact.cpp @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/13 09:05:59 by charles #+# #+# */ -/* Updated: 2020/04/13 09:17:57 by charles ### ########.fr */ +/* Updated: 2020/11/09 12:30:12 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,7 +21,8 @@ Contact Contact::prompt() Contact::promptString("nickname: ", c.m_nickname); Contact::promptString("login: ", c.m_login); - std::cout << "address:" << std::endl; + if (std::cin.good()) + std::cout << "address:" << std::endl; Contact::promptInt("| house number: ", c.m_address.m_houseNum); Contact::promptInt("| post code: ", c.m_address.m_postCode); Contact::promptString("| street name: ", c.m_address.m_street); @@ -30,7 +31,8 @@ Contact Contact::prompt() Contact::promptString("email: ", c.m_email); Contact::promptString("phone: ", c.m_phone); - std::cout << "birthday:" << std::endl; + if (std::cin.good()) + std::cout << "birthday:" << std::endl; Contact::promptInt("| day: ", c.m_birthday.m_day); Contact::promptInt("| month: ", c.m_birthday.m_month); Contact::promptInt("| year: ", c.m_birthday.m_year); @@ -43,19 +45,47 @@ Contact Contact::prompt() void Contact::promptString(std::string promptString, std::string &s) { - std::cout << promptString << std::flush; - getline(std::cin, s); + do + { + if (!std::cin.good()) + { + s = ""; + return; + } + std::cout << promptString << std::flush; + std::getline(std::cin, s); + if (!std::cin.good()) + { + s = ""; + return; + } + if (s.empty()) + std::cout << "Error: input can't be empty" << std::endl; + } + while (s.empty()); + } void Contact::promptInt(std::string promptString, int &i) { - std::cout << promptString << std::flush; - i = getInt(); - while (i == -1) + do { - std::cout << "Error: Not a valid number, please try again" << std::endl; + if (!std::cin.good()) + { + i = -1; + return; + } + std::cout << promptString << std::flush; i = getInt(); + if (!std::cin.good()) + { + i = -1; + return; + } + if (i == -1) + std::cout << "Error: Not a valid number, please try again" << std::endl; } + while (i == -1); } std::string Contact::trimedName(std::string name) @@ -68,32 +98,34 @@ std::string Contact::trimedName(std::string name) void Contact::preview() const { std::cout << std::setw(10) << trimedName(m_firstName) << "|" - << std::setw(10) << trimedName(m_lastName) << "|" - << std::setw(10) << trimedName(m_nickname); + << std::setw(10) << trimedName(m_lastName) << "|" + << std::setw(10) << trimedName(m_nickname); } void Contact::put() const { - std::cout << m_firstName << std::endl; - std::cout << m_lastName << std::endl; - std::cout << m_nickname << std::endl; - std::cout << m_login << std::endl; - - std::cout << "Street " << m_address.m_street - << " Nb " << m_address.m_houseNum - << " " << m_address.m_postCode - << " " << m_address.m_city - << std::endl; - - std::cout << m_email << std::endl; - std::cout << m_phone << std::endl; - - std::cout << m_birthday.m_day - << "/" << m_birthday.m_month - << "/" << m_birthday.m_year - << std::endl; - - std::cout << m_favMeal << std::endl; - std::cout << m_underwareColor << std::endl; - std::cout << m_darkestSecret << std::endl; + std::cout << "first name: " << m_firstName << std::endl; + std::cout << "last name: " << m_lastName << std::endl; + std::cout << "nickname: " << m_nickname << std::endl; + std::cout << "login: " << m_login << std::endl; + + std::cout << "address: " + << "Street " << m_address.m_street + << " Nb " << m_address.m_houseNum + << " " << m_address.m_postCode + << " " << m_address.m_city + << std::endl; + + std::cout << "email: " << m_email << std::endl; + std::cout << "phone: " << m_phone << std::endl; + + std::cout << "birthday: " + << m_birthday.m_day << "/" + << m_birthday.m_month << "/" + << m_birthday.m_year + << std::endl; + + std::cout << "favourite meal: " << m_favMeal << std::endl; + std::cout << "underware color: " << m_underwareColor << std::endl; + std::cout << "dardest secret: " << m_darkestSecret << std::endl; } diff --git a/cpp00/ex01/Contact.hpp b/cpp00/ex01/Contact.hpp index 4717e55..a82da86 100644 --- a/cpp00/ex01/Contact.hpp +++ b/cpp00/ex01/Contact.hpp @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/13 07:23:11 by charles #+# #+# */ -/* Updated: 2020/11/09 09:30:12 by cacharle ### ########.fr */ +/* Updated: 2020/11/09 12:04:19 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,6 +37,7 @@ private: std::string m_street; std::string m_city; }; + struct Date { int m_day; diff --git a/cpp00/ex01/PhoneBook.cpp b/cpp00/ex01/PhoneBook.cpp index a28ce36..7ce3df0 100644 --- a/cpp00/ex01/PhoneBook.cpp +++ b/cpp00/ex01/PhoneBook.cpp @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/13 07:23:13 by charles #+# #+# */ -/* Updated: 2020/11/09 09:29:37 by cacharle ### ########.fr */ +/* Updated: 2020/11/09 12:33:21 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,7 +36,7 @@ std::ostream& operator<<(std::ostream& out, PhoneBook const& p) { for (size_t i = 0; i < p.getSize(); i++) { - out << i << "|"; + out << std::setw(10) << i << "|"; p.get(i).preview(); std::cout << std::endl; } diff --git a/cpp00/ex01/main.cpp b/cpp00/ex01/main.cpp index 8b04fe2..c415cbb 100644 --- a/cpp00/ex01/main.cpp +++ b/cpp00/ex01/main.cpp @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/13 07:23:15 by charles #+# #+# */ -/* Updated: 2020/11/09 09:22:22 by cacharle ### ########.fr */ +/* Updated: 2020/11/09 12:17:23 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,7 +22,8 @@ int main() while (true) { - std::cout << "> " << std::flush; + if (std::cin.good()) + std::cout << "> " << std::flush; if (!std::getline(std::cin, input)) break; std::cout.flush(); @@ -43,7 +44,7 @@ int main() if (tmp < 0 || size_t(tmp) >= phoneBook.getSize()) std::cout << "Error: Not valid index: " << tmp << std::endl; else - phoneBook.get(tmp).put(); + phoneBook.get(size_t(tmp)).put(); std::cout << std::flush; } } diff --git a/cpp00/ex01/utils.cpp b/cpp00/ex01/utils.cpp index 27c7daa..93b916d 100644 --- a/cpp00/ex01/utils.cpp +++ b/cpp00/ex01/utils.cpp @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/13 07:31:30 by charles #+# #+# */ -/* Updated: 2020/04/13 08:43:18 by charles ### ########.fr */ +/* Updated: 2020/11/09 12:10:25 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,9 +15,11 @@ int getInt() { std::string s; - int tmp; + int tmp; - std::cin >> s; + std::getline(std::cin, s); + if (s.empty()) + return (-1); for (size_t i = 0; i < s.length(); i++) if (!isdigit(s[i])) return (-1); -- cgit