markosss Temat założony przez niniejszego użytkownika |
Program c++. Funkcja void push_back » 2017-05-12 10:22:41 Witam, jestem aktualnie w trakcie tworzenia kolejnego programu, lecz napotkałem mały problem. Z góry przepraszam za to że kod jest w języku angielskim, ale aktualnie jestem za granicą i jakoś tak wyszło.. Posiadam program coś w rodzaju "testu", zadaje pytania itd, sumuje pkt. Wszystko działa. Oto mój kod #include <iostream> #include <string> #include <vector> 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; int load; 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. Will i pass an exam?", "Yes", "Yes of course", "Absolutely yes", "Without any problems", "For sure", 'c', 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( "3. Which of the planets is closest to the Sun?", "Jupiter", "Venus", "Saturn", "Mercury", "Neptune", 'd', 5 ); q4.setValues( "4. When did the Second World War end?", "1944", "1945", "1942", "1943", "1946", 'b', 5 ); q5.setValues( "5. Where is the tallest peak in Europe?", "France", "Italy", "Spain", "Austria", "Russia", 'e', 5 ); q6.setValues( "6. What is the most spoken Language in the World?", "English", "Spanish", "Chinese", "Hindi", "Arabic", 'c', 5 ); q7.setValues( "7. What is the poorest country in the world?", "Central African Republic", "Burundi", "Liberia", "Niger", "Mozambique", 'a', 5 ); q8.setValues( "8. What is the richest country in the world", "Luxembourg", "Kuwait", "Qatar", "Ireland", "Norway", 'c', 5 ); q9.setValues( "9. Who have largest army in the world?", "China", "United States", "Russia", "North Korea", "India", 'a', 5 ); q10.setValues( "10. Where is the hottest place on Earth?", "Arabian Desert", "Sahara", "Gobi Desert", "Kalahari Desert", "Death Valley", 'e', 5 ); vector < Question > questions = { q1, q2, q3, q4, q5, q6, q7, q8, q9, q10 }; cout << "Which question do you want to load?"; cin >> load; questions[ load - 1 ].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(); } }
No ale skoro wszystko działało, chcialem pójść za ciosem i dodać kolejne funkcje. Pierwszą rzeczą która zrobiłem, to spakowałem wszystkie pytania do wektora i stworzyłem że program mnie pyta które pytanie chce wyświetlić, po wpisaniu liczby program wyświetla odpowiednie pytanie, kod do tego wygląda tak: vector < Question > questions = { q1, q2, q3, q4, q5, q6, q7, q8, q9, q10 }; cout << "Which question do you want to load?"; cin >> load; questions[ load - 1 ].askQuestion();
I tutaj zaczynają się schodki. Chciałem stworzyć funkcje która będzie dawała mi możliwość stworzenia pytania z klawiatury i dodanie jej na koniec wektora, napisałem coś takiego: void addQuestion( vector < Question >& questions ) { Question q( "11. Pytanie", "odp1", "odp2", "odp3", "odp4", "odp5", 'a', 5 ); questions.push_back( q );
i mam error "154 [Error] a function-definition is not allowed here before '{' token. Czy mógłby ktoś mi pomóc rozwiązać problem? Z góry bardzo dziękuje. |