Hello everyone! I am currently doing the harry potter sorting hat exercise and finished the exercise but wanted to add to it. I tried to add the possibility of there being a tie between two of the houses and how to work through the outcome. For some reason though, when I input specific answers into the terminal that will wind up as a tie based on the code that I have written, the answer comes out wrong. For instance, when I do Q1 - 4, Q2 - 1, Q3 - 3, Q4 - 1… It should give me a tie between Gryffindor and Ravenclaw and then execute the last if statement, but it is returning Slytherin as the winner instead. Can someone please give me some advice? The link is provided after this paragraph. Thank you!
https://www.codecademy.com/courses/learn-c-plus-plus/projects/harry-potter-sorting-hat
CODE:
#include <iostream>
int main() {
int gryffindor, hufflepuff, ravenclaw, slytherin;
int answer1, answer2, answer3, answer4, answer5;
std::cout << "-=-=-=-=-=-=-=-=-=-=-=-=-\n";
std::cout << " The Sorting Hat Quiz\n";
std::cout << "-=-=-=-=-=-=-=-=-=-=-=-=-\n\n";
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";
std::cin >> answer1;
if (answer1 == 1){
hufflepuff++;
}
else if (answer1 == 2){
slytherin++;
}
else if (answer1 == 3){
ravenclaw++;
}
else if (answer1 == 4){
gryffindor++;
}
else {
std::cout << "Invalid input.\n\n";
}
std::cout << "Q2) Dawn or Dusk?\n\n";
std::cout << " 1) Dawn\n";
std::cout << " 2) Dusk\n";
std::cin >> answer2;
if (answer2 == 1){
gryffindor++;
ravenclaw++;
}
else if (answer2 == 2){
hufflepuff++;
slytherin++;
}
else {
std::cout << "Invalid input.\n\n";
}
std::cout << "Q3) 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 drumn\n";
std::cin >> answer3;
if (answer3 == 1){
hufflepuff++;
}
else if (answer3 == 2){
slytherin++;
}
else if (answer3 == 3){
ravenclaw++;
}
else if (answer3 == 4){
gryffindor++;
}
else {
std::cout << "Invalid input.\n\n";
}
std::cout << "Q4) Which road tempts you 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";
std::cin >> answer4;
if (answer4 == 1){
hufflepuff++;
}
else if (answer4 == 2){
slytherin++;
}
else if (answer4 == 3){
ravenclaw++;
}
else if (answer4 == 4){
gryffindor++;
}
else {
std::cout << "Invalid input.\n\n";
}
int max;
std::string house;
if (gryffindor > max){
max = gryffindor;
house = "Gryffindor";
}
if (hufflepuff > max){
max = hufflepuff;
house = "Hufflepuff";
}
if (ravenclaw > max){
max = ravenclaw;
house = "Ravenclaw";
}
if (slytherin > max){
max = slytherin;
house = "Slytherin";
}
if (gryffindor == max & ravenclaw == max){
std::cout << "There is a tie between gryffindor and ravenclaw. We are in need of a tie breaker\n\n";
std::cout << "~~~~~~~~~~~~~~~~\n";
std::cout << " TIE BREAKER\n";
std::cout << "~~~~~~~~~~~~~~~~\n";
std::cout << "Extra Point: Would you rather fly or be invisible?\n\n";
std::cout << " 1) Fly\n";
std::cout << " 2) Invisible\n";
std::cin >> answer5;
if (answer5 == 1){
gryffindor++;
}
else if (answer5 == 2){
ravenclaw++;
}
else {
std::cout << "Invalid entry.";
}
if (gryffindor > max){
max = gryffindor;
house = "Gryffindor";
}
if (ravenclaw > max){
max = ravenclaw;
house = "Ravenclaw";
}
}
std::cout << house << "!\n";
}