C ++ Harry Potter Sorting Quiz

Code is here: https://gist.github.com/ad4d69ce82b8fb8a0695759ca899c18b

I am getting an exclamation mark when I should get an answer.

$ g++ sortinghat.cpp

$ ./a.out

=====================

The Sorting Hat Quiz!

=====================

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

  1. The Good
  2. The Great
  3. The Wise
  4. The Bold

1

Q2) Dawn or Dusk?:

  1. Dawn
  2. Dusk

1

Q3) Which kind of instrument most pleases your ear?:

  1. The violin
  2. The trumpet
  3. The Piano
  4. The drum

2

Q4) Which road tempts you most:

  1. The wide, sunny grassy lane
  2. The narrow, dark, lantern-lit alley
  3. The twisting, leaf-strewn path through woods
  4. The cobbled street lined (ancient buildings)

3

!

$

Can anyone tell me what I am missing?

1 Like

I got Hufflepuff! when I ran this code (with random answers).

Try to isolate what answers make it only output the exclamation point. There’s probably a small error around that code.

1 Like

I only get your output when I intentionally give invalid answers causing all four scores to end up with values of 0. That seems to be expected behavior. Using the answers in your example, I get, “Gryffindor!”
There doesn’t appear to be anything wrong that I can see. Are you sure the code you ran is the same as the code in your gist?

2 Likes

Kindly anyone tells me what’s the problem with my code as sometimes it sorts me but sometimes when I choose different options, it does not show anything. And if you have suggestions, for some recommendations for other styles to code Harry Potter Sorting Hat then you are pleasantly and surely invited.

And here is my code-

#include <string>
#include <iostream>
int main() {
	int one = 0;
	int two = 0;
	int three = 0;
	int four = 0;
	int five = 0;
	int Gryffindor = 0;
	int Hufflepuff = 0;
	int Slytherin = 0;
	int Ravenclaw = 0;

	std::string name;
	std::cout << "                             Hogwarts Sorting Ceremony\n\n";
	std::cout << "                     Hi there , may I know your name please: ";

	std::cin >> name;

	 std::cout << "                     \n\n Hello " << name << ", I am the sorting hat of this famous Wizarding school - Hogwarts School Of Witch Craft And Wizardry.\nMy job is to sort all the young, sweet witches and wizards into there destined houses based upon their charcter\n";
	 
	 std::cout << "                                \n\n Our school has four houses namely -: \n\nGryffindor , Slytherin , Hufflepuff and Ravenclaw.\n\n They all have different attributes.";
	
	 std::cout << "                                    \n\n Gryffindor - Daring and Courage\n\nSlythering - Ambitious and Cunning\n\nHufflepuff - Kind and Selfless\n\nRavenclaw - Entrepreneul and Smart\n\n ";
	
	 std::cout << "                             \n\n Let us see in which house do you fit in , just select one of the options that relates you and type it's respective numeral. Let's Begin!\n\n";
	
	 std::cout << " \n\n\n\n\n                             1) Which of the following would you most hate people to call you?\n\nOrdinary\n\nIgnorant\n\nCowardly\n\nSelfish";
	
	 std::cout << " \n\n            Your Answer: ";
	
	 std::cin >> one;
	
	 std::cout << " \n\n\n\n\n               How would you like to be known to history?\n\nThe Wise\n\nThe Good\n\nThe Bold\n\nThe Great\n\n\n";
	
	 std::cout << "                         \n\n Your Answer: ";
	
	 std::cin >> two;

	 std::cout << "  \n\n\n                   3) You enter an enchanted garden. What would you be most curious to examine first?\n\n  The silver leafed tree bearing golden apples\n\n The fat red toadstools that appear to be talking to each other\n\n  The bubbling pool, in the depths of which something luminous is swirling\n\n  The statue of an old wizard with a strangely twinkling eye";
	 std::cout << "  \n\n                      Your answer: ";
	 std::cin >> three;

	 std::cout << "                            4) Four boxes are placed before you. Which would you try and open?\n\n The small tortoiseshell box embellished with gold, inside which some small creature seems to be squeaking.\n\n   The gleaming jet black box with a silver lockand key, marked with a mysterious rune that you know to be the mark of Merlin.\n\n  The ornate golden casket, standing on clawed feet, whose inscription warns that both secret knowledgeand unbearable temptation lie within.\n\n  The small pewter box, unassumingand plain, with a scratched message upon it that reads I open only for the worthy.";
	 std::cout << "                            \n\n Your Answer : ";
	 std::cin >> four;

	 std::cout << " \n\n                        5) Which of the following do you find most difficult to deal with?\n\n   Hunger\n\n   Cold\n\n  Loneliness\n\n  Boredom";
	 std::cout << "                            \n\n Your Answer : ";
	 std::cin >> five;
	 if (five == 1) {
		 Ravenclaw++;
	 }
	 else if (five == 2) {
		 Slytherin++;
	 }
	 else if (five == 3) {
		 Gryffindor++;
	 }
	 else if (five == 4) {
		 Hufflepuff++;
	 }
	 if (four == 1) {
			 Ravenclaw++;
		 }
		 else if (four == 2) {
			 Slytherin++;
		 }
		 else if (four == 3) {
			 Gryffindor++;
		 }
		 else if (four == 4) {
			 Hufflepuff++;
		 }
		 
		 if (three == 1) {
		 Ravenclaw++;
	 }
	 else if (three == 2) {
		 Slytherin++;
	 }
	 else if (three == 3) {
		 Gryffindor++;
	 }
	 else if (three == 4) {
		 Hufflepuff++;
	 }
	 if (one == 1) {
		 Ravenclaw++;
	 }
	 else if (one == 2) {
		 Slytherin++;
	 }
	 else if (one == 3) {
		 Gryffindor++;
	 }
	 else if (one == 4) {
		 Hufflepuff++;
		 }
	 if (two == 1) {
		 Ravenclaw++;
	 }
	 else if (one == 2) {
		 Slytherin++;
	 }
	 else if (one == 3) {
		 Gryffindor++;
	 }
	 else if (one == 4) {
		 Hufflepuff++;
	 }
	 if (Gryffindor > Slytherin && Gryffindor > Ravenclaw && Gryffindor > Hufflepuff) {
		 std::cout << "                           Conratulations " << name << " , you are sorted in Gryffindor house , which is denoted by it's boldness and courage.";
	 }
	 else if (Slytherin > Gryffindor && Slytherin > Ravenclaw && Slytherin > Hufflepuff) {
		 std::cout << "                            Conratulations " << name << " , you are sorted in Slyhtherin house , which is denoted by it's cunningness and ambitiousness.";
	 }
	 else if (Ravenclaw > Gryffindor && Ravenclaw > Slytherin && Ravenclaw > Hufflepuff) {
		 std::cout << "                            Conratulations " << name << " , you are sorted in Ravenclaw house , which is denoted by it's entrepreneurship and smartness.";
	 }
	 else if (Hufflepuff > Gryffindor && Hufflepuff > Ravenclaw && Hufflepuff > Slytherin) {
		 std::cout << "                          Conratulations " << name << " , you are sorted in Hufflepuff house , which is denoted by it's kindness and selfness.";
	 
	 }
	 }

Hello, @indianyouthclub94399, and welcome to the forums.

I see a couple of issues. At first glance, it looks like you forgot to make a few changes after copy/pasting some of your code:

Running your code, there are no numbers next to the choices. The instructions would be easier to follow if each choice was preceded by its corresponding number.

Finally, your way of determining which house should be assigned does not follow the instructions in task number 16, and is not an adequate replacement. What if two houses tie for the most points? None of your if conditionals evaluate to true when there is a tie. It is also much less efficient. You really only need to make 3 comparisons to find the largest of 4 numbers. Your code makes as many as 12. (count the >'s). Following the instructions for task 16, you would make 4 comparisons, but you could modify it to only make the necessary 3.

Your Code sometimes not work because you haven’t given your variable max a value. Line 164 in your code, set int max = 0, then run the code or see this solution by me Harry-Porter-Sorting-Hat-Quiz.cpp