From 0096589d41c0a0bdf4faa9c7756911a36ba0e6e4 Mon Sep 17 00:00:00 2001 From: David Aryasetia <99853366+davidaryasetia@users.noreply.github.com> Date: Fri, 1 Sep 2023 10:49:38 +0700 Subject: [PATCH] Update sortinghat.cpp Fixing project code sortinghat in codecademy to make good for readable --- .../harry-potter-sorting-hat/sortinghat.cpp | 168 ++++++++---------- 1 file changed, 73 insertions(+), 95 deletions(-) diff --git a/3-conditionals-and-logic/harry-potter-sorting-hat/sortinghat.cpp b/3-conditionals-and-logic/harry-potter-sorting-hat/sortinghat.cpp index 25599a0..b276626 100644 --- a/3-conditionals-and-logic/harry-potter-sorting-hat/sortinghat.cpp +++ b/3-conditionals-and-logic/harry-potter-sorting-hat/sortinghat.cpp @@ -1,153 +1,131 @@ +// Project Case C++ (Sorting Hat Program Harry Potter) #include -int main() { - +int main (){ + // declare & initialize 4 varible to receive point int gryffindor = 0; int hufflepuff = 0; int ravenclaw = 0; int slytherin = 0; + // declare 4 answer variable to store their anwer for question int answer1, answer2, answer3, answer4; - std::cout << "===============\n"; - std::cout << "The Sorting Hat\n"; - std::cout << "===============\n\n"; - - // ~~~~~~~~~~ Question 1 ~~~~~~~~~~ - - std::cout << "Q1) When I'm dead, I want people to remember me as:\n\n"; - - std::cout << " 1) The Good\n"; - std::cout << " 2) The Great\n"; - std::cout << " 3) The Wise\n"; - std::cout << " 4) The Bold\n\n"; - - std::cout << "Enter your answer (1-4): "; + std::cout << "=========================\n"; + std::cout << "The sorting Hat Quiz \n"; + std::cout << "=========================\n"; + + // make instrument for question3 + std::cout << "Q1) When I'm dead, I want people to remember me as :\n"; + std::cout << "1) The Good \n"; + std::cout << "2) The Great \n"; + std::cout << "3) The Wise \n"; + std::cout << "4) The Bold \n"; + std::cout << "Answer Q1) : " ; std::cin >> answer1; + std::cout << "\n"; - if (answer1 == 1) + // Make conditional logic decision for answer1 + if (answer1 == 1){ hufflepuff++; - else if (answer1 == 2) + } else if (answer1 == 2){ slytherin++; - else if (answer1 == 3) + } else if (answer1 == 3){ ravenclaw++; - else if (answer1 == 4) + } else if (answer1 == 4){ gryffindor++; + } else { + std::cout << "Invalid input \n"; + } - // ~~~~~~~~~~ Question 2 ~~~~~~~~~~ - - std::cout << "\nQ2) Dawn or Dusk?\n\n"; - - std::cout << " 1) Dawn\n"; - std::cout << " 2) Dusk\n\n"; - - std::cout << "Enter your answer (1-2): "; + // Make instrument for question 2 + std::cout << "Q2) Dawn or Dusk \n"; + std::cout << "1) Dawn\n"; + std::cout << "2) Dusk\n"; + std::cout << "Answer Q2) : "; std::cin >> answer2; + std::cout << "\n"; - if (answer2 == 1) - { - + // Make conditional logic decision for answer2 + if (answer2 == 1){ gryffindor++; ravenclaw++; - - } - else if (answer2 == 2) - { - + } else if (answer2 == 2){ hufflepuff++; slytherin++; - + } else { + std::cout << "Invalid input \n"; } - else - { - - std::cout << "Invalid input\n"; - - } - - // ~~~~~~~~~~ Question 3 ~~~~~~~~~~ - - std::cout << "\nQ3) Which kind of instrument most pleases your ear?\n\n"; - std::cout << " 1) The violin\n"; - std::cout << " 2) The trumpet\n"; - std::cout << " 3) The piano\n"; - std::cout << " 4) The drum\n\n"; - - std::cout << "Enter your answer (1-4): "; + // Make something for question3 + std::cout << "Q3) Which kind of instrument most pleases your ear? \n"; + std::cout << "1) The violin \n"; + std::cout << "2) The trumpet \n"; + std::cout << "3) The piano \n"; + std::cout << "4) The drum \n"; + std::cout << "Answer Q3 : \n"; std::cin >> answer3; + std::cout << "\n"; - if (answer3 == 1) + // Make conditional logic decision3 for answer3 + if(answer3 == 1){ slytherin++; - else if (answer3 == 2) + } else if (answer3 == 2){ hufflepuff++; - else if (answer3 == 3) + } else if (answer3 == 3){ ravenclaw++; - else if (answer3 == 4) + } else if (answer3 == 4){ gryffindor++; + } else { + std::cout << "Invalid input \n"; + } - // ~~~~~~~~~~ Question 4 ~~~~~~~~~~ - - std::cout << "\nQ4) Which road tempts you the most?\n\n"; - - std::cout << " 1) The wide, sunny grassy lane\n"; - std::cout << " 2) The narrow, dark, lantern-lit alley\n"; - std::cout << " 3) The twisting, leaf-strewn path through woods\n"; - std::cout << " 4) The cobbled street lined (ancient buildings)\n\n"; - - std::cout << "Enter your answer (1-4): "; - std::cin >> answer4; + // Make something for question 4 + std::cout << "Q4) Which road tempts you most?\n"; + std::cout << "1) The wide, sunny grassy lane \n"; + std::cout << "2) The narrow, dark, lantern-litalley \n"; + std::cout << "3) The twisting, leaf-strewn path through woods \n"; + std::cout << "4) The cobbled street lined (ancient buildings) \n"; + std::cout << "\n"; - if (answer4 == 1) + // make conditional logic decision4 answer4 + if (answer4 == 1){ hufflepuff++; - else if (answer4 == 2) + } else if (answer4 == 2){ slytherin++; - else if (answer4 == 3) + } else if (answer4 == 3){ gryffindor++; - else if (answer4 == 4) - ravenclaw++; - - // ========== Sorting ========== - - std::cout << "\nCongrats on being sorted into... "; + } else { + std::cout << "Invalid input \n"; + } + // Find out which if the four houses has heigh answer int max = 0; std::string house; - if (gryffindor > max) - { - + // Make a logical conditional to find maximum + if (gryffindor > max){ max = gryffindor; house = "Gryffindor"; - } - if (hufflepuff > max) - { - + if (hufflepuff > max){ max = hufflepuff; house = "Hufflepuff"; - } - if (ravenclaw > max) - { - + if (ravenclaw > max){ max = ravenclaw; house = "Ravenclaw"; - } - if (slytherin > max) - { - + if (slytherin > max){ max = slytherin; house = "Slytherin"; - } std::cout << house << "!\n"; + std::cout << "Thank You For Your attention \n"; - return 0; - +return 0; }