main.cpp
1 /* ************************************************************************** */ 2 /* */ 3 /* ::: :::::::: */ 4 /* main.cpp :+: :+: :+: */ 5 /* +:+ +:+ +:+ */ 6 /* By: gychoi <gychoi@student.42seoul.kr> +#+ +:+ +#+ */ 7 /* +#+#+#+#+#+ +#+ */ 8 /* Created: 2023/11/10 22:20:33 by gychoi #+# #+# */ 9 /* Updated: 2023/11/27 22:44:21 by gychoi ### ########.fr */ 10 /* */ 11 /* ************************************************************************** */ 12 13 #include <sstream> 14 #include "Bureaucrat.hpp" 15 16 int main() 17 { 18 std::string nameInput; 19 std::string gradeInput; 20 std::string name; 21 int grade; 22 23 while (true) 24 { 25 std::cout << "Enter the bureaucrat name: "; 26 std::getline(std::cin, nameInput); 27 28 if (nameInput.empty()) 29 { 30 std::cout << "Invalid input. Name cannot be empty.\n"; 31 } 32 else 33 { 34 name = nameInput; 35 break; 36 } 37 } 38 39 while (true) 40 { 41 std::cout << "Enter the bureaucrat grade (1 to 150): "; 42 std::getline(std::cin, gradeInput); 43 44 bool isAllDigits = true; 45 46 for (size_t i = 0; i < gradeInput.length(); ++i) 47 { 48 if (!std::isdigit(gradeInput[i])) { 49 isAllDigits = false; 50 break; 51 } 52 } 53 54 std::istringstream iss(gradeInput); 55 56 if (!isAllDigits || gradeInput.empty() || (!(iss >> grade))) 57 { 58 std::cout << "Invalid input. Please enter a valid integer.\n"; 59 } 60 else 61 { 62 break; 63 } 64 } 65 66 try 67 { 68 Bureaucrat bureaucrat(name, grade); 69 std::cout << bureaucrat << std::endl; 70 71 std::cout << "[Increase Grade]" << std::endl; 72 bureaucrat.increaseGrade(); 73 std::cout << bureaucrat << std::endl; 74 75 std::cout << "[Decrease Grade]" << std::endl; 76 bureaucrat.decreaseGrade(); 77 std::cout << bureaucrat << std::endl; 78 79 std::cout << "[Decrease Grade Again]" << std::endl; 80 bureaucrat.decreaseGrade(); 81 std::cout << bureaucrat << std::endl; 82 83 std::cout << "[Increase Grade Again]" << std::endl; 84 bureaucrat.increaseGrade(); 85 std::cout << bureaucrat << std::endl; 86 87 std::cout << "------------------" << std::endl; 88 std::cout << "Hello, James!" << std::endl; 89 90 Bureaucrat James("James", 9); 91 std::cout << James << std::endl; 92 93 James = bureaucrat; 94 std::cout << James << std::endl; 95 std::cout << "James! don't do that!" << std::endl; 96 } 97 catch (std::exception & e) 98 { 99 std::cout << "Caught exception: " << e.what() << std::endl; 100 } 101 102 return 0; 103 }