Share c++ quiz game source code

truongmichelle

New member
### C ++ Mã nguồn trò chơi Quiz

** hashtags: ** #c ++ #Quiz #Game #SourceCode #Programming

**Giới thiệu**

Bài viết này sẽ cung cấp cho bạn mã nguồn cho một trò chơi đố đơn giản được viết bằng C ++.Trò chơi là một bài kiểm tra trắc nghiệm trong đó người dùng được trình bày với một câu hỏi và bốn câu trả lời có thể.Người dùng phải chọn câu trả lời đúng từ các tùy chọn được cung cấp.Trò chơi theo dõi điểm số của người dùng và cung cấp phản hồi sau mỗi câu hỏi.

** Mã nguồn **

Mã nguồn cho trò chơi đố được cung cấp dưới đây.Mã được thông tin tốt và nên dễ hiểu.

`` `C ++
#include <Istream>
#include <String>

sử dụng không gian tên STD;

// Hàm này hiển thị thông báo chào mừng cho người dùng.
void welcomemessage () {
cout << "Chào mừng bạn đến với trò chơi đố vui!"<< Endl;
cout << "======================" << endl;
}

// Hàm này hiển thị các hướng dẫn cho trò chơi đố cho người dùng.
void DisplayInstructions () {
cout << "Hướng dẫn:" << endl;
cout << "1. Bạn sẽ được trình bày với một câu hỏi."<< Endl;
cout << "2. Bạn phải chọn câu trả lời chính xác từ các tùy chọn được cung cấp."<< Endl;
Cout << "3. Trò chơi sẽ theo dõi điểm số của bạn."<< Endl;
cout << "4. Chúc may mắn!"<< Endl;
}

// Hàm này tải các câu hỏi và câu trả lời từ một tệp.
void LoadQuestions (chuỗi tệp) {
// Mở tập tin.
Tệp ifstream (tên tệp);

// Đọc các câu hỏi và câu trả lời từ tệp.
while (! file.eof ()) {
// Đọc câu hỏi.
câu hỏi chuỗi;
Tệp >> câu hỏi;

// Đọc câu trả lời.
chuỗi câu trả lời [4];
for (int i = 0; i <4; i ++) {
Tệp >> câu trả lời ;
}

// Thêm câu hỏi và câu trả lời cho bài kiểm tra.
câu hỏi.push_back (câu hỏi);
câu trả lời.push_back (câu trả lời);
}

// Đóng tệp.
file.close ();
}

// Hàm này hiển thị một câu hỏi cho người dùng.
void DisplayQuestion (int index) {
// Hiển thị câu hỏi.
cout << câu hỏi [index] << endl;

// Hiển thị câu trả lời.
for (int i = 0; i <4; i ++) {
cout << "[" << i + 1 << "]" << Trả lời [index] << endl;
}
}

// chức năng này nhận được câu trả lời của người dùng cho một câu hỏi.
int getanswer () {
// Nhận đầu vào của người dùng.
int trả lời;
CIN >> Trả lời;

// Xác thực đầu vào của người dùng.
if (Trả lời <1 || Trả lời> 4) {
cout << "Câu trả lời không hợp lệ."<< Endl;
trả lại -1;
}

// Trả về câu trả lời của người dùng.
trả lời trả lời;
}

// Hàm này kiểm tra xem câu trả lời của người dùng có đúng không.
bool isanSwerCorrect (int index, int answer) {
// Nhận câu trả lời chính xác.
int orporManSwer = câu trả lời [index] [Trả lời - 1];

// Kiểm tra xem câu trả lời của người dùng có đúng không.
if (orrectAnswer == answers [index] [answer - 1]) {
trả lại đúng;
} khác {
trả lại sai;
}
}

// Hàm này hiển thị kết quả của bài kiểm tra cho người dùng.
void DisplayResults (int scord) {
// Hiển thị điểm số của người dùng.
cout << "Điểm của bạn là" << điểm << "."<< Endl;

// Kiểm tra xem người dùng đã đạt được điểm hoàn hảo.
if (scors == câu hỏi.size ()) {
Cout << "Xin chúc mừng! Bạn đã đạt được một số điểm hoàn hảo!"<< Endl
=======================================
### C++ Quiz Game Source Code

**Hashtags:** #C++ #Quiz #Game #SourceCode #Programming

**Introduction**

This article will provide you with the source code for a simple quiz game written in C++. The game is a multiple-choice quiz where the user is presented with a question and four possible answers. The user must select the correct answer from the options provided. The game keeps track of the user's score and provides feedback after each question.

**The Source Code**

The source code for the quiz game is provided below. The code is well-commented and should be easy to understand.

```c++
#include <iostream>
#include <string>

using namespace std;

// This function displays the welcome message to the user.
void welcomeMessage() {
cout << "Welcome to the Quiz Game!" << endl;
cout << "======================" << endl;
}

// This function displays the instructions for the quiz game to the user.
void displayInstructions() {
cout << "Instructions:" << endl;
cout << "1. You will be presented with a question." << endl;
cout << "2. You must select the correct answer from the options provided." << endl;
cout << "3. The game will keep track of your score." << endl;
cout << "4. Good luck!" << endl;
}

// This function loads the questions and answers from a file.
void loadQuestions(string filename) {
// Open the file.
ifstream file(filename);

// Read the questions and answers from the file.
while (!file.eof()) {
// Read the question.
string question;
file >> question;

// Read the answers.
string answers[4];
for (int i = 0; i < 4; i++) {
file >> answers;
}

// Add the question and answers to the quiz.
questions.push_back(question);
answers.push_back(answers);
}

// Close the file.
file.close();
}

// This function displays a question to the user.
void displayQuestion(int index) {
// Display the question.
cout << questions[index] << endl;

// Display the answers.
for (int i = 0; i < 4; i++) {
cout << "[" << i + 1 << "] " << answers[index] << endl;
}
}

// This function gets the user's answer to a question.
int getAnswer() {
// Get the user's input.
int answer;
cin >> answer;

// Validate the user's input.
if (answer < 1 || answer > 4) {
cout << "Invalid answer." << endl;
return -1;
}

// Return the user's answer.
return answer;
}

// This function checks if the user's answer is correct.
bool isAnswerCorrect(int index, int answer) {
// Get the correct answer.
int correctAnswer = answers[index][answer - 1];

// Check if the user's answer is correct.
if (correctAnswer == answers[index][answer - 1]) {
return true;
} else {
return false;
}
}

// This function displays the results of the quiz to the user.
void displayResults(int score) {
// Display the user's score.
cout << "Your score is " << score << "." << endl;

// Check if the user has achieved a perfect score.
if (score == questions.size()) {
cout << "Congratulations! You have achieved a perfect score!" << endl
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top