&引用;“提神”;一个字符串变量,使该多选while循环不';不要无限重复 所以我是C++新手,我是一个入门课程,明天的作业是写一个程序,有5个问题,一些选择,一些是真的或假的。p>
现在我正在做第一道选择题,它必须有4个可能的答案(a,b,c,d),条件是:&引用;“提神”;一个字符串变量,使该多选while循环不';不要无限重复 所以我是C++新手,我是一个入门课程,明天的作业是写一个程序,有5个问题,一些选择,一些是真的或假的。p>,c++,variables,while-loop,infinite-loop,C++,Variables,While Loop,Infinite Loop,现在我正在做第一道选择题,它必须有4个可能的答案(a,b,c,d),条件是: 如果用户输入了正确的字母选择,它将继续下一个问题 如果用户输入了错误的字母选择,它会告诉用户他们错了,并再次向用户提问 如果用户输入4个字母以外的任何内容,它将显示一个无效的输入错误,并再次向他们提问 在输入正确答案之前,将出现2和3 我尝试使用while循环(我认为这是一种方法?),然后在其中使用嵌套的if/else语句 我遇到的问题是,Q1字符串变量在输入时是在开始时实例化的,之后不能更改。使循环要么通过(如果回答
#include<iostream>
#include<string>
using namespace std;
int main()
{
string Q1;
cout << "How many ounces are in 1 cup?" << endl;
cout << "A. 12 \nB. 8 \nC. 16 \nD. 4" << endl;
cin >> Q1;
while (Q1 != "B") {
if (Q1 == "A" || Q1 == "C" || Q1 == "D"){
cout << "Your selected answer " << Q1 << " is incorrect, please select another answer" << endl;
}
else {
cout << "Your answer is invalid, please enter either A, B, C, or D." << endl;
}
}
cout << "Correct! There are 8 ounces in one cup" << endl;
}
#包括
#包括
使用名称空间std;
int main()
{
字符串Q1;
cout在您的代码中有一个无限循环。您不再要求用户输入。您需要将cin>>Q1
添加到循环末尾:
while (Q1 != "B") {
if (Q1 == "A" || Q1 == "C" || Q1 == "D"){
cout << "Your selected answer " << Q1 << " is incorrect...
}
else {
cout << "Your answer is invalid, please enter either...
}
cin >> Q1;
}
while(Q1!=“B”){
如果(Q1==“A”| | Q1==“C”| | Q1==“D”){
coutwhile(cin>>Q1)
{
如果(Q1=“B”)
{
能不能嗯cin>>Q1;
在循环的最后?@NathanOliver哇,我觉得真的很愚蠢,真的很简单。出于某种原因,我在想象当被重新问这个问题时,它也必须重新问可能的答案,但这真的毫无意义。谢谢!
while (cin>>Q1)
{
if(Q1 == "B")
{
cout << "Next question\n";
break;
}
if (Q1 == "A" || Q1 == "C" || Q1 == "D"){
cout << "Your selected answer " << Q1 << " is incorrect, please select another answer" << endl;
}
else {
cout << "Your answer is invalid, please enter either A, B, C, or D." << endl;
}
}