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.