Write a program in C++that allows students to answer multiple-choice and fill in the blank questions in the console window, and ultimately display their scores
时间: 2024-02-29 09:52:44 浏览: 98
C# - CSharp 12 in a Nutshell The Definitive Reference
Sure, here's an example program in C++ that allows students to answer multiple-choice and fill in the blank questions in the console window, and ultimately display their scores:
```c++
#include <iostream>
#include <string>
using namespace std;
int main() {
// declare variables for questions and answers
string q1 = "What is the capital of France?\n";
string q2 = "What is the largest planet in our solar system?\n";
string q3 = "What is the formula for water?\n";
string a1 = "Paris";
string a2 = "Jupiter";
string a3 = "H2O";
// declare variables for student answers and score
string ans1, ans2, ans3;
int score = 0;
// ask first question and get answer
cout << q1;
cin >> ans1;
if (ans1 == a1) {
cout << "Correct!" << endl;
score++;
} else {
cout << "Incorrect. The correct answer is " << a1 << "." << endl;
}
// ask second question and get answer
cout << q2;
cin >> ans2;
if (ans2 == a2) {
cout << "Correct!" << endl;
score++;
} else {
cout << "Incorrect. The correct answer is " << a2 << "." << endl;
}
// ask third question and get answer
cout << q3;
cin >> ans3;
if (ans3 == a3) {
cout << "Correct!" << endl;
score++;
} else {
cout << "Incorrect. The correct answer is " << a3 << "." << endl;
}
// display final score
cout << "Your final score is " << score << "/3." << endl;
return 0;
}
```
In this program, we first declare strings for the questions and their corresponding answers. Then we declare variables for the student's answers and their score. We ask each question with `cout` and get the student's answer with `cin`. If the answer is correct, we increment the score by 1 and output "Correct!" with `cout`. If the answer is incorrect, we output the correct answer with `cout`. Finally, we display the student's final score with `cout`.
阅读全文