2 Questions about sorting hat quiz for C++

So the first thing i want to ask about is this part right here:

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

So how would this print the house with the highest value stored in it? The only condition that needs to be met here is that the house is greater than 0, how does the program know which is the highest number then?

Also, i’m a little confused as to the error message i was getting in the end. Each answer is supposed to be structured like this:

,

  cout << "When I'm dead, I want to be remembered as:\n";
  cout << "1) The Good\n";
  cout << "2) The Great\n";
  cout << "3) The Wise\n";
  cout << "4) The Bold\n";
  
  cout << "Enter 1,2,3 or 4 as your answer: ";
  cin >> answer1;
  cout << "\n\n";
  
  if (answer1 == 1) {
    gryffindor++;
  }
  
  else if(answer1 == 2) {
    hufflepuff++;
  }
  
  else if(answer1 == 3) {
    ravenclaw++;
  }
  
  else if(answer1 == 4) {
    slytherin++;
  }
  
 else {
   cout << "Wrong answer, try again.\n";
 }

,

In my original program, i simply had easch answer as slytherin + 1, which i figured should work the same way. However, it didn’t, at the end of the program it was not outputting house, and for whatever reason when i went back to each one and replaced ++ with + 1 it finally worked the way it was supposed to.

Hello, great questions.

To your first question, we are basically checking fo the highest number using a couple of if statements.

We start with max being 0. If gryffindor points is more than 0, then the winning house is gryffindor. So that’s the new max.

Then we compare the new max with hufflepuff, if hufflepuff beats gryffindor, then the hufflepuff is the new max. If not, then gryffindor is still the new max.

And we do that down the list. Let me know if that makes any sense!

To your second question, did you do gryffindor = gryffindor +1, and hufflepuff = huffpuff +1, and so on?

2 Likes

oh it absolutely does, it’s kinda like a loop except it’s not. Thanks for clearing that up for me. The highest variable is printed after it has gone through each test.

oh and to the second topic: i’m pretty sure i did, but i don’t remember.