Tie breaker addition for Harry Potter Sorting Hat Quiz

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";
}

Depending on the programming language, uninitialized variables may be assigned default values OR they may contain some garbage value OR your program may crash OR your output may keep changing randomly.

In C++, using a variable before it is used can cause undefined behavior. Have a look at:

https://www.learncpp.com/cpp-tutorial/uninitialized-variables-and-undefined-behavior/

You should consider initializing the variables gryffindor, hufflepuff, ravenclaw, slytherin and max before using them in increment or comparison statements.

Also, you should consider using && (the logical AND operator) instead of & (the bitwise AND operator).

1 Like

Without even looking at the code, I stuck in a tracer:

void trace(int gryffindor, int hufflepuff, int ravenclaw, int slytherin) {
    std::cout
        << "gryffindor = " << gryffindor << ", "
        << "hufflepuff = " << hufflepuff << ", "
        << "ravenclaw= " << ravenclaw << ", "
        << "slytherin = " << slytherin << std::endl;
}

Call that thing after each answer:

Q1) When I'm dead, I want people to remember me as:

 1) The Good
 2) The Great
 3) The Wise
 4) The Bold
4
gryffindor = 32604, hufflepuff = 25325192, ravenclaw= 32603, slytherin = 25326048

Whelp, that’s the answer. You want to initialized those variables. So, before you get rolling:

gryffindor = hufflepuff = ravenclaw = slytherin = 0;

Got to the end and got:

 4) The cobbled street lined (ancient buildings)
1
gryffindor = 2, hufflepuff = 1, ravenclaw= 2, slytherin = 0
!

So something else went wrong. Perhaps max could do with starting at zero?

2 Likes

Right, just realized I showed my lazy way of initializing all things to zero. Technically, you want to initialize on declaration, so:

int n = 0;

Also, C++ has a somewhat quirkier way of doing this which you might run into:

int n(0);

Either way, always set your starting value when you declare the variable, if you can, and you’ll be fine.

2 Likes

Wowwwwwwwww!!! Thank you so much! For starters, I didnt even realize that I did not use the logical and operator I thought I had input that. And secondly, I did not know that not initializing code could cause problems along the road. I thought just because it automatically has a value of zero, it would work the same way. Thank you so much, it worked!!!

Thank you! I was unaware that that would make that big of a difference. I appreciate it!

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.