Witam. Piszę swój kolejny program/projekt w jezyku c++(z góry przepraszam za nazwy, cout'y w języku angielskim, ale aktualnie jestem za granica i tak jakoś..) Stworzyłem program w którym posiadam 10 pytań(nie wypisywałem wszystkich aby kod był bardziej przejrzysty), dodałem punkty za prawidłową odpowiedź etc.Aktualnie program wyświetla wszystkie pytania i sprawdza odpowiedzi po czym sumuje liczbę pkt i daje wynik zdany/niezdany.
Teraz chciałbym zrobić aby spakować wszystkie 10 pytań które stworzyłem do wektora, i stworzyć funkcje która mi np wyświetli pytanie po indeksie. Dla przykładu, będę miał 10 pytań w środku wektora, i po wpisaniu liczby 2(zostanę o nią poproszony) zostanie wywołana funkcja
q2.askQuestion();
Czy jest to możliwe? Czy mógłby mi ktoś pomóc?
#include <iostream>
#include <string>
using namespace std;
char guess;
int total;
class Question{
public:
void setValues(string, string, string, string, string, string, char, int);
void askQuestion();
private:
string Question_Text;
string answer_1;
string answer_2;
string answer_3;
string answer_4;
string answer_5;
char correct_answer;
int Question_Score;
};
int main()
{
int R;
string respond;
cout << "Are you ready to start the quiz? Yes/No.\n";
cin >> respond;
if (respond == "Yes" || respond == "yes") {
cout << "\n";
cout << "Good luck!\n";
cout << "\n";
cout << "Press enter to continue.";
cin.get();
cin.ignore();
}else{
cout << "\n";
cout << "Goodbye!\n";
cin.ignore();
cin.get();
return 0;
}
Question q1;
Question q2;
Question q3;
Question q4;
Question q5;
Question q6;
Question q7;
Question q8;
Question q9;
Question q10;
q1.setValues("1. What is the deepest lake in the world?",
"Baikal",
"Great Bear Lake",
"Lake Michigan",
"Lake Victoria",
"Lake Tahoe",
'a',
5);
q2.setValues("2. What is the highest place on Earth?",
"Mount Rainier",
"Mount Everest",
"Mount Kinabalu",
"Mount Damavand",
"Nanga Parbat",
'b',
5);
q3.setValues(...
;
q4.setValues...
;
q5.setValues(...
;
q6.setValues(...
;
q7.setValues(...
;
q8.setValues(...
;
q9.setValues(...
;
q10.setValues(...
;
q1.askQuestion();
q2.askQuestion();
q3.askQuestion();
q4.askQuestion();
q5.askQuestion();
q6.askQuestion();
q7.askQuestion();
q8.askQuestion();
q9.askQuestion();
q10.askQuestion();
cout << "Your Total Score is " << total << " out of 50!\n";
cout << "\n";
if (total > 25) {
cout << R<<"You pass" << "\n";
cout << "\n";
cin.get();
cin.ignore();
return 0;
}
else
{
cout << "You failed... Sorry, better luck next time.\n";
cout << "\n";
}
cin.get();
cin.ignore();
return 0;
}
void Question::setValues(string q, string a1, string a2, string a3, string a4, string a5, char ca, int pa)
{
Question_Text = q;
answer_1 = a1;
answer_2 = a2;
answer_3 = a3;
answer_4 = a4;
answer_5 = a5;
correct_answer = ca;
Question_Score = pa;
}
void Question::askQuestion()
{
cout << "\n";
cout << Question_Text << "\n";
cout << "a. " << answer_1 << "\n";
cout << "b. " << answer_2 << "\n";
cout << "c. " << answer_3 << "\n";
cout << "d. " << answer_4 << "\n";
cout << "e. " << answer_5 << "\n";
cout << "\n";
cout << "What is your answer?" << "\n";
cin >> guess;
if (guess == correct_answer) {
cout << "\n";
cout << "Correct!" << "\n";
total = total + Question_Score;
cout << "\n";
cout << "Press enter to continue." << "\n";
cin.get();
cin.ignore();
}
else
{
cout << "\n";
cout << "Sorry, you're wrong..." << "\n";
cout << "The correct answer is " << correct_answer << "." << "\n";
cout << "\n";
cout << "Press enter to continue." << "\n";
cin.get();
cin.ignore();
}
}