Hello!
This is my first post and i’m new to coding.
I’m having trouble implimenting this feature.
in the course project “Harry Potter sorting hat quiz”. I have gotten it to work if you use it correctly.
but if you don’t, all it does is say “invalid input” and moves on.
How do i get it to repeat the question where the missinput occurred?
My code:
#include <iostream>
using namespace std;
int main(){
int gryffindor = 0, hufflepuff = 0, ravenclaw = 0, slytherin = 0;
int answer1, answer2, answer3, answer4;
cout << "=====================\n";
cout << "The Sorting Hat Quiz!\n";
cout << "=====================\n";
//Question 1
cout << "Question 1) When I'm dead, I want peaple to remember me as: \n\n";
cout << "1) The Good\n";
cout << "2) The Great\n";
cout << "3) The Wise\n";
cout << "4) The Bold\n";
cin >> answer1;
if (answer1==1){
hufflepuff++;
}
else if(answer1==2){
slytherin++;
}
else if(answer1==3){
ravenclaw++;
}
else if(answer1==4){
gryffindor++;
}
else{
cout << "Invalid input\n";
}
//Question 2
cout << "Question 2) Dawn or Dusk\n\n";
cout << "1) Dawn\n";
cout << "2) Dusk\n";
cin >> answer2;
if (answer2==1){
hufflepuff++;
ravenclaw++;
}
else if(answer2==2){
slytherin++;
gryffindor++;
}
else{
cout << "Invalid input\n";
}
//Question 3
cout << "Question 3) Which kind of instrument most pleases your ear?\n\n";
cout << "1) The Violin\n";
cout << "2) The Trumpet\n";
cout << "3) The Piano\n";
cout << "4) The Drum\n";
cin >> answer3;
if (answer3==1){
slytherin++;
}
else if(answer3==2){
hufflepuff++;
}
else if(answer3==3){
ravenclaw++;
}
else if(answer3==4){
gryffindor++;
}
else{
cout << "Invalid input\n";
}
//Question 4
cout << "Question 4) Which road tempts you the most?\n\n";
cout << "1) The wide, sunny grassy lane\n";
cout << "2) The narrow, dark, lantern-lit alley\n";
cout << "3) The twisting, leaf-strewn path thorugh woods\n";
cout << "4) The cobbled street lined (ancient buildings)\n";
cin >> answer4;
if (answer4==1){
hufflepuff++;
}
else if(answer4==2){
slytherin++;
}
else if(answer4==3){
gryffindor++;
}
else if(answer4==4){
ravenclaw++;
}
else{
cout << "Invalid input\n";
}
cout << "\nCongrats on being sorted into... ";
string house;
int max = 0;
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";
}
cout << house << "!\n\n";
}