尝试做多项选择题
Trying to make a multiple choice question
#include <iostream>
using namespace std;
int main() {
/**
4 variants
3 correct
a b c d
d is incorrecy
if an answer is reapeted, it needs to say
"Answer *a/b/c/d* has been already introduced"
**/
cout << "Which ones are correct?: a) b) c) d) \n";
// for the sake of the example, only A B and C are correct
string answer;
int numberOfCorrectAnswers = 0;
bool correct = true;
while (true) {
cin >> answer;
if (answer == "a" || answer == "b" || answer == "c")
numberOfCorrectAnswers++;
else if (answer == "d")
correct = false;
if (answer == "Done")
break;
if (answer != "a" && answer != "b" && answer != "c" && answer != "d") {
cout << "Input available answer!\n";
}
}
if (correct == true && numberOfCorrectAnswers == 3)
cout << numberOfCorrectAnswers;
else
cout << "Incorrect answers!";
return 0;
}
我正在尝试做一个多项选择题,但我不知道如何做,所以当我输入两次答案时,它不认为它是正确的,而是要求第二次输入。
示例:a a b Done (incorrect)
由于在这种情况下您以前见过或未见过该选项,因此它很适合用于选项的二进制标志,例如 bool。但是由于有多个选项,为每个变量设置标志并不是很容易维护。想象一下,如果您的选项增加到 5 或 6。您不会希望维护 6 个不同的变量来跟踪您是否已经看到该选项。
这个用例再次适用于 arrays/vectors 布尔值。您甚至可以使用 std::bitset
来跟踪变量,因为最后您只需要 0 表示未见,1 表示已见。
所以,你可以使用这样的东西:
std::bitset<NUM_OPTIONS> optionFlags; //NUM_OPTIONS = 4 (a, b, c, d) for your example
要根据答案设置标志,您可以执行以下操作
size_t idx = answer[0] - 'a';
optionFlags.set(idx); //Set the bit for this option to test later
然后稍后,在检查答案是否已被看到时,您可以这样做
size_t idx = answer[0] - 'a';
if(optionFlags.test(idx)) { //Check if bit for the option is already set
//Tell user that this option has already been entered earlier
}
有多种方法可以使用其他数据结构实现此目的,但通常在这些方面有些相似。
#include <iostream>
using namespace std;
int main() {
/**
4 variants
3 correct
a b c d
d is incorrecy
if an answer is reapeted, it needs to say
"Answer *a/b/c/d* has been already introduced"
**/
cout << "Which ones are correct?: a) b) c) d) \n";
// for the sake of the example, only A B and C are correct
string answer;
int numberOfCorrectAnswers = 0;
bool correct = true;
while (true) {
cin >> answer;
if (answer == "a" || answer == "b" || answer == "c")
numberOfCorrectAnswers++;
else if (answer == "d")
correct = false;
if (answer == "Done")
break;
if (answer != "a" && answer != "b" && answer != "c" && answer != "d") {
cout << "Input available answer!\n";
}
}
if (correct == true && numberOfCorrectAnswers == 3)
cout << numberOfCorrectAnswers;
else
cout << "Incorrect answers!";
return 0;
}
我正在尝试做一个多项选择题,但我不知道如何做,所以当我输入两次答案时,它不认为它是正确的,而是要求第二次输入。
示例:a a b Done (incorrect)
由于在这种情况下您以前见过或未见过该选项,因此它很适合用于选项的二进制标志,例如 bool。但是由于有多个选项,为每个变量设置标志并不是很容易维护。想象一下,如果您的选项增加到 5 或 6。您不会希望维护 6 个不同的变量来跟踪您是否已经看到该选项。
这个用例再次适用于 arrays/vectors 布尔值。您甚至可以使用 std::bitset
来跟踪变量,因为最后您只需要 0 表示未见,1 表示已见。
所以,你可以使用这样的东西:
std::bitset<NUM_OPTIONS> optionFlags; //NUM_OPTIONS = 4 (a, b, c, d) for your example
要根据答案设置标志,您可以执行以下操作
size_t idx = answer[0] - 'a';
optionFlags.set(idx); //Set the bit for this option to test later
然后稍后,在检查答案是否已被看到时,您可以这样做
size_t idx = answer[0] - 'a';
if(optionFlags.test(idx)) { //Check if bit for the option is already set
//Tell user that this option has already been entered earlier
}
有多种方法可以使用其他数据结构实现此目的,但通常在这些方面有些相似。