This is a short example of what I was saying.
#include <iostream>
using namespace std;
bool q1()
{
static const char question[] = "What are the colors of the United States Flag?";
static const char *answer[] =
{
"Black, Gold, Green",
"Red, White",
"Blue, Black, Red, White",
"Red, White, Blue",
};
cout << question << "\n\n";
for ( size_t i = 0; i < sizeof answer / sizeof *answer; ++i )
{
cout << i + 1 << ". " << answer[i] << '\n';
}
cout << "\nanswer? ";
int response;
cin >> response;
cin.ignore();
return response == 4;
}
bool q2()
{
static const char question[] = "What do the stars of the flag mean?";
static const char *answer[] =
{
"One for each State",
"One for each President",
"One for each Senator",
"The amount of wars the USA won",
};
cout << '\n' << question << "\n\n";
for ( size_t i = 0; i < sizeof answer / sizeof *answer; ++i )
{
cout << i + 1 << ". " << answer[i] << '\n';
}
cout << "\nanswer? ";
int response;
cin >> response;
cin.ignore();
return response == 1;
}
int main()
{
while ( q1() == 0)
{
cout << "\nIncorrect, please try again.\n";
}
cout << "\nCorrect!\n";
while ( q2() == 0)
{
cout << "\nIncorrect, please try again.\n";
}
cout << "\nCorrect!\n";
return 0;
}
When there is repetitive code, like there is here, I prefer to go after a better solution. But hopefully it will demonstrate some basics in a direction away fromgotos.
[edit]Maybe like this.
#include <iostream>
using namespace std;
struct Question
{
const char *question;
const char *answer[4];
int correct;
};
static Question Quiz[] =
{
{ // Question 1
"What are the colors of the United States Flag?",
{
"Black, Gold, Green",
"Red, White",
"Blue, Black, Red, White",
"Red, White, Blue",
},
4
},
{ // Question 2
"What do the stars of the flag mean?",
{
"One for each State",
"One for each President",
"One for each Senator",
"The amount of wars the USA won",
},
1
},
};
bool ask(size_t j)
{
cout << Quiz[j].question << "\n\n";
for ( size_t i = 0; i < sizeof Quiz[j].answer / sizeof *Quiz[j].answer; ++i )
{
cout << i + 1 << ". " << Quiz[j].answer[i] << '\n';
}
cout << "\nanswer? ";
int response;
cin >> response;
cin.ignore();
return response == Quiz[j].correct;
}
int main()
{
for ( size_t i = 0; i < sizeof Quiz / sizeof *Quiz; ++i )
{
while ( ask(i) == 0 )
{
cout << "\nIncorrect, please try again.\n\n";
}
cout << "\nCorrect!\n";
}
return 0;
}