I want to write a program on where the user gets asked a quiz and then they get assigned on one of the Harry Potter houses, but in the end the output for the house doesn’t show. I am using c++ for this project and I also using Visual Studio Code as my IDE. I want to be able to put the alternatives and when the user inputs them, it takes an average and assigns them a house in the end.
On the code displayed here, I used … to show repetition and to shorten the code a lot. So in the answer variables, there are 10 variables, from one to 10 with a value of 0 for each of them. On the questions, there are ten, but I have only displayed the first and the last. There are 4 choices and each is 1, 2, 3 and 4. There is one if statement representing the corresponding answer(question1, answer1; question2, answer2 and so on and so forth). Also, sorry if the code is a little unclear, because I have to show most of the code, but it doesn’t let me post if there is more code than description. How can I fix the issue where the house doesn’t display now? Any tips would be appreciated a lot, thanks!
This is the code:
#include <iostream>
using namespace std;
int main() {
int gryffindor = 0;
...
int slytherin = 0;
int answer1 = 0;
...
int answer10 = 0;
cout << "Let's test which house you belong to. Test beginning in: \n 3 \n 2 \n 1 \n 0 \n Good luck \n";
cout << "What colour do you like most? \n 1. Red ... \n 4. Yellow \n";
cin >> answer1;
if (answer1 == 1) {
gryffindor++;
} else if (answer1 == 2) {
hufflepuff++;
} else if (answer1 == 3) {
ravenclaw++;
} else if (answer1 == 4) {
slytherin++;
} else {
cout << "Try again, answer invalid: ";
cin >> answer1;
}
...
cout << "If the sorting hat put you in what you think is the wrong house you would \n 1. Softly make a comment \n ... 4. Go happily to the house you were assigned to \n";
cin >> answer10;
if (answer10 == 1) {
gryffindor++;
}
...
else {
cout << "Try again, answer invalid: ";
cin >> answer10;
}
// Finding the houses
int MaxPoints;
string YourHouse;
if (gryffindor > MaxPoints) {
MaxPoints = gryffindor;
YourHouse = "Gryffindor";
}
if (hufflepuff > MaxPoints) {
MaxPoints = hufflepuff;
YourHouse = "Hufflepuff";
}
if (ravenclaw > MaxPoints) {
MaxPoints = ravenclaw;
YourHouse = "Ravenclaw";
}
if (slytherin > MaxPoints) {
MaxPoints = slytherin;
YourHouse = "Slytherin";
}
cout << "The Hat has decided...Your house is: " << YourHouse << "! \n";
system("pause");
return 0;
}
>Solution :
When you write "but in the end the output for the house doesn’t show", do you mean the other text does show, but specifically not the house name? Or does none of the text show.
In the former case, you might want to give the variable MaxPoints an initial value, because currently its value is undefined:
int MaxPoints = -1;