这是什么意思? 我的程序是这样的:(注意:出现错误的行是在案例2之前的行。)
case 1:
{
cout<< "C * H * E * M * I * S * T * R * Y \n\n";
cout<< "1) What is the valence electron configuration of Selenium (Se)?\n\n";
cout<< "\na) 1s2 2s2 2p6 3s2\n\n";
cout<< "\nb) 1s2 2s2 2p2\n\n";
cout<< "\nc)4s2 4p4\n\n";
cout<< "\nd) 1s2 2s2 2p6 3s2 3p6 4s2 3d10 4p6 5s2 4d10 5p5\n\n";
cout<< "Enter your answer:\n";
cin>> answer;
if (answer == 'c')
{
cout<<"Your answer is correct. Please press the enter key to proceed to the next question.\n\n";
}
else
cout<< "The correct answer is C. Please press the enter key to proceed to the next question.\n\n";
getch ();
}
getch ();
cout<< "2) Which element yields the biggest atomic radius?\n\n";
cout<< "\na) Ca\n\n";
cout<< "\nb) Xe\n\n";
cout<< "\nc) B\n\n";
cout<< "\nd) Cs\n\n";
cout<< "Enter your answer:\n";
cin>> answer;
if (answer == 'd')
{
cout<< "Your answer is correct. Please press the enter key to proceed to the next question.\n\n";
}
else
cout<< "The correct answer is D. Please press the enter key to proceed to the next question.\n\n";
getch ();
}
cout<< "3) Name the ionic compound K2 Cr2 O7\n\n";
cout<< "\na) potassium chloride\n\n";
cout<< "\nb) potassium carbonate\n\n";
cout<< "\nc) potassium chromite\n\n";
cout<< "\nd) potassium chromate\n\n";
cout<< "Enter your answer:\n";
cin>> answer;
if (answer == 'd')
{
cout<< "Your answer is correct. Please press the enter key to proceed to the next question.\n\n";
}
else
cout<< "The correct answer is D. Please press the enter key to proceed to the next question.\n\n";
getch ();
}
}
case 2:
{
cout<< "G * E * O * M * E * T * R * Y \n\n";
标题中提到的错误是我在开头段落中注明的行expected declaration before '}' token
。
答案 0 :(得分:3)
我想你想要这个:
if (answer == 'd')
{
cout<< "Your answer is correct. Please press the enter key to proceed to the next question.\n\n";
}
else
cout<< "The correct answer is D. Please press the enter key to proceed to the next question.\n\n";
getch ();
}
}
看起来像这样:
if (answer == 'd')
{
cout<< "Your answer is correct. Please press the enter key to proceed to the next question.\n\n";
} else {
cout<< "The correct answer is D. Please press the enter key to proceed to the next question.\n\n";
getch ();
}
您似乎在else
之后立即忘记了您的范围。
答案 1 :(得分:0)
在case
之前有两个}。相应的{是什么?特别是最后else
看起来有些孤独。
答案 2 :(得分:0)
在我重新格式化您的代码后,它会在网站上显示出来,错误非常明显。
我建议,当你在if
子句周围使用括号时,也要在else
子句周围使用它们:
if (foo) {
/* ... */
} else {
/* ... */
}
如果两个案例只是一个陈述,那么离开括号是很好的。虽然如果只有一个需要,但没有要求在两个子句中放置括号,我发现if
语句周围有很多错误,只有一个路径括号。这令人惊讶。 :)
另外请注意,具有括号匹配功能的文本编辑器将是一个巨大的帮助。许多编辑器将突出显示未正确嵌套的括号,有些编辑器提供了快速跳转它们的关键。 (在vi
和克隆中,%
键会在()
,{}
,[]
和<>
对之间反弹。非常方便。)< / p>
答案 3 :(得分:0)
在每次演出后,你似乎都有一个超级的“}”:
getch ();
}
^^^