Harry Potter Sorting Hat Quiz

I did the task as instructions and as per my knowledge. However, at the end, I would not get sorted into any house. Comparing my code to the sample on github, the difference I saw is that my version had a + 1 on each answer, i.e:

if (answer4 == 1) {
hufflepuff + 1;
}

However, in the github version, it is:

if (answer4 == 1) {
hufflepuff++;
}

Why does ++; work and + 1; doesn’t?

hufflepuff + 1 is an expression but isn’t a reassignment to the variable.
All of the following would work:

hufflepuff = hufflepuff + 1;
hufflepuff += 1;
hufflepuff++;

The third option has the limit of only adding 1. The first two could increment the variable by any value.
Hope this helps!

2 Likes

Hi,

Thanks for the reply. But wouldn’t the + 1 be counted as Arithmetic? Like, hufflepuff + 8; wouldnt work as well? what about hufflepuff + ravenclaw?

hufflepuff + 8 would evaluate to whatever the value of hufflepuff is + 8, but where would that value be assigned? hufflepuff += 8 is shorthand for hufflepuff = hufflepuff + 8 Without the assignment operator = hufflepuff would still point to whatever value it did before. hufflepuff++ is shorthand for hufflepuff = hufflepuff + 1.
For example:

int hufflepuff = 2;
cout << hufflepuff + 8;
cout << hufflepuff;
hufflepuff += 8;
cout << hufflepuff;
hufflepuff++;
cout << hufflepuff;

Output:

10 <<This is the value of the evaluated expression: hufflepuff + 8
2 <<hufflepuff is still 2
10 <<hufflepuff’s value was changed by hufflepuff += 8
11 <<hufflepuff’s value was changed again by hufflepuff++

The value of hufflepuff didn’t change until after a reassignment using = or ++.

I got it now, the = sign makes it stick, just like it was explained to me but I didnt think it would apply in this case.

Thanks for the info.

2 Likes

I got this error:

sortinghat.cpp: In function 'int main()':
sortinghat.cpp:49:3: error: expected ';' before if'
      if (answer2 == 1) {
      ^~
sortinghat.cpp:53:3: error: 'else' without a previous 'if'
      else if (answer2 == 2) {
      ^~
sortinghat.cpp:69:3: error: expected ';' before if'
      if (answer3 == 1) {
      ^~
sortinghat.cpp:72:3: error: 'else' without a previous 'if'
      else if (answer3 == 2) {
      ^~~~

This is my current code:

#include <iostream>

int main() {
  int gryffindor = 0;
  int hufflepuff = 0;
  int ravenclaw = 0;
  int slytherin = 0;

  int answer1 = 0;
  int answer2 = 0;
  int answer3 = 0;
  int answer4 = 0;

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

  std::cout << "Q2) Dawn or Dusk?:\n\n";
  std::cout << "  1) Dawn\n";
  std::cout << "  2) Dusk\n\n";

  std::cin >> answer2
  
  if (answer2 == 1) {
    gryffindor++;
    ravenclaw++;
  }
  else if (answer2 == 2) {
    hufflepuff++;
    slytherin++;
  }
  else {
    std::cout << "Invalid input\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 drum\n\n";

  std::cin >> answer3

  if (answer3 == 1) {
    slytherin++;
  }
  else if (answer3 == 2) {
    hufflepuff++;
  }
  else if (answer3 == 3) {
    ravenclaw++;
  }
  else if (answer3 == 4) {
    gryffindor++;
  }
  else {
    std::cout << "Invalid input\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) {
    gryffindor++;
  }
  else if (answer4 == 4) {
    ravenclaw++;
  }
  else {
    std::cout << "Invalid input\n";
  }

  int max;
  std::string house;

  if (gryffindor > max) {
    max = gryffindor;
    house = "Gryffindor";
  }
  else if (hufflepuff > max) {
    max = hufflepuff;
    house = "Hufflepuff";
  }
  else if (ravenclaw > max) {
    max = ravenclaw;
    house = "Ravenclaw";
  }
  else if (slytherin >= max) {
    max = slytherin;
    house = "Slytherin";
  } 
  else {
    std::cout << house << "!\n";
  }
}

Explain what I should change to make the code work.

Check your error message, it’s pretty descriptive

sortinghat.cpp:49:3: error: expected ';' before if'
      if (answer2 == 1) {
      ^~

So it says in line 49: expected ';' before if', if you check a line before your if, you have std::cin >> answer3 without the ;

The rest of the else ifs in that clause don’t work because the first if isn’t considered valid.

1 Like

Thanks, bro. Completely missed that.

My program runs but does not output the house at the end.
#include

int main() {

// The magic starts here

int gryffindor = 0;

int hufflepuff = 0;

int ravenclaw = 0;

int slytherin = 0;

int answer1 = 0;

int answer2 = 0;

int answer3 = 0;

int answer4 = 0;

std::cout << “=====================\n”;

std::cout << “The Sorting Hat Quiz!\n”;

std::cout << “=====================\n\n”;

std::cout << “Q1) When I am dead, I want people to remeber 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";

}

std::cout << “Q2) Dwan or Dusk?\n\n”;

std::cout << “1) Dwan\n”;

std::cout << “2) Dusk\n”;

std::cin >> answer2;

if (answer2 == 1) {

gryffindor++;

ravenclaw++;

}

else if (answer2 == 2) {

hufflepuff++;

slytherin++;

}

else {

std::cout << "Invald inout\n";

}

std::cout << “Q3) What kind instrument most pleases your ear?\n\n”;

std::cout << “1) The viloin\n”;

std::cout << “2) The trumpet\n”;

std::cout << “3) The Piano\n”;

std::cout << “4) The drum\n”;

std::cin >> answer3;

if (answer3 == 1) {

slytherin++;

}

else if (answer3 == 2) {

hufflepuff++;

}

else if (answer3 == 3) {

ravenclaw++;

}

else if (answer3 == 4) {

gryffindor++;

}

else {

std::cout << "Invalid input\n";

}

std::cout << “Q4) Which road temtps 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 the woods\n”;

std::cout << “4) The cobbled street lind with ancient buildings\n”;

std::cin >> answer4;

if (answer4 == 1) {

hufflepuff++;

}

else if (answer4 == 2) {

slytherin++;

}

else if (answer4 == 3) {

gryffindor++;

}

else if ( answer4 == 4) {

ravenclaw++;

}

else {

std::cout << "Invalid input\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”;

}

std::cout << house << “!\n”;

}
This is my code. Can anyone help with why it is not adding the house?

anybody know why this always gives hufflepuff?

#include <iostream>
#include <vector>

int main() {

  //The magic starts here

  int gryffindor = 0, hufflepuff = 0, ravenclaw = 0, slytherin = 0, Q1a, Q2a, Q3a, Q4a, max = 0;
  std::vector<int> points = {gryffindor, hufflepuff, ravenclaw, slytherin};
  std::vector<int> answers = {Q1a, Q2a, Q3a, Q4a};
  std::string house;

  std::cout << "The Sorting Hat Quiz!\n";

  std::cout << "Q1) When I'm dead, I want people to remember me as:\n\n  1) The Good\n  2) The Great\n  3) The Wise\n  4) The Bold\n\n";
  std::cin >> Q1a;

  std::cout << "Q2) What is your favorite time of day?\n\n  1) Dusk\n  2) Night\n  3) Dawn\n  4) Day\n\n";
  std::cin >> Q2a;

  std::cout << "Q3) Which instrument most pleases your ears?\n\n  1) The fiddle\n  2) The trumpet\n  3) The piano\n  4) The drum\n\n";
  std::cin >> Q3a;

  std::cout << "Q4) Which road tempts you most?\n\n  1) The wide, sunny, grassy lane\n  2) The narrow, dark, lantern-lit alley\n  3) The cobbled street lined with ancient bulidings\n  4) The twisting, leaf-strewn path through woods\n\n";
  std::cin >> Q4a;

  for (int i = answers.size(); i > 0; i--) {
    
    if (answers[i] = 1) {
      
      hufflepuff++;
    
    }
    
    else if (answers[i] = 2){
      
      slytherin++;
    
    }
    
    else if (answers[i] = 3){
      
      ravenclaw++;
    
    }

    else {
      
      gryffindor++;

    }

  }
  
  if (hufflepuff > max) {

    max += hufflepuff - max;
    house = "hufflepuff";

  }

  else if (slytherin > max) {

    max += slytherin - max;
    house = "slytherin";

  }

  else if (ravenclaw > max) {

    max += ravenclaw - max;
    house = "ravenclaw";

  }

  else if (gryffindor > max) {

    max += gryffindor - max;
    house = "gryffindor";

  }

  std::cout << "Your house is " << house << ".\n";

}

It’s a reasonable guess to assume that for every return to be hufflepuff then the condition of the if statement always evaluates to true. When you reach your if statement what value might hufflepuff have at that point? What value would max have?

Unsure? Output those values and check.

1 Like

The value of max is what it’s supposed to be, 0, but even with no answers to add to the value of hufflepuff it gives me a value of 4. All the other points variables get a value of 0, so I’d guess that it’s giving all the points to hufflepuff even if they don’t match with hufflepuff.

Sounds like you’re on the a good trail for your bug then. Next stop, where is hufflepuff added to and why might it be unexpectedly increasing.

There appear to be issues in both the for loop and if statement. Currently, I have this:

for (int i = answers.size(); i > 0; i--) {
    
    std::cout << answers[i];
    if (answers[i] = 1) {
      
      hufflepuff++;
    
    }
    
    else if (answers[i] = 2){
      
      slytherin++;
    
    }
    
    else if (answers[i] = 3){
      
      ravenclaw++;
    
    }

    else {
      
      gryffindor++;

    }

  }

set up, and I have checked answers before as well and in loop and if statement. It gives me 032512-73429 and so on and so on for the loop answers[i], and in the if statements it gives them all as 1.

Can somebody explain to me how the part works exactly?

int max;

std::string house;

My question here is, before I store the points on answer1, answer2, answer3, answer4.
And then what happen exactly in this line?
if (gryffindor > max) {

max = gryffindor;

house = “Gryffindor”;

I mean since I set max to 0, shoudn’t every House (gryffindor,hufflepuff,ravenclaw,slytherin) be above 0? I managed to complete it but I want to understand what happens here exactly and how the system allocate the highest value/number.

Thank in advance.

You’ve not provided much of your code so your order isn’t clear, please see- How to ask good questions (and get good answers) especially any sections about formatting code. I’d assume that your answers and point calculations come before this step, no?

Under that assumption just follow the logic, if gryffindor is indeed larger than max then that section will run. Make up some values (or run the quiz with some outputs) and follow what the next steps are. Does max behave like you want (largest value is the one assigned at the end)?

Can you think of another way to do this?

thank you for your swift response.
Indeed it was my first question I’ve ever written.

I’m talking about this exercise:
sortinghat exercise

Exercise 16.
I’m not sure if I just don’t get it, but on the following code I don’t get the behaviour of the max function:

int max = 0;
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 = "Slythering";
}

std::cout << house << "!\n";
```
How does it know which one has the highest value? I'm sure im missing a big part of it but I just don't know what it is.
2 Likes

It’s always much simpler with code you wrote yourself but digging through unfamiliar code is a necessary skill so lets continue a little. I’d highly suggest cracking out a pen and pencil or similar and just following that flow. Consider starting for example with the following values-
max=0, hufflepuff=22, gryffindor=10, slytherin=17, ravenclaw=20

The first step is if gryffindor > max which would be 10 > 0, which evaluates to true. So, we then execute that code blocks reassigning max to the value of gryffindor, max = 10, assign house to "Gryffindor" and move onto the next test. So is hufflepuff greater than max (remember that max has changed)? Just follow that through and the logic should be quite clear.

2 Likes

Ah yeah now I understand it, the max will change until it reaches the highest value and will remain with the value and “house” string if there is no condition that’s output is true.

Thank you very much for your quicke support, really appreciate it!

2 Likes