DEV C ++ Error: expected declaration before '}' token
What does this mean? My program goes like this: (NOTE: The line that has the error was the line coming before case 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";
The error, as noted in the title is expected declaration before '}' token
at the line noted in my opening paragraph.
I think you want this:
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 ();
}
}
To look like this:
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 ();
}
It looks like you lost track of your scope immediately following the else
.
After I reformatted your code so it would display tolerably on the website, the errors stand out pretty clearly.
I would recommend, when you use brackets around the if
clause, also use them around the else
clause:
if (foo) {
/* ... */
} else {
/* ... */
}
Leaving off the brackets is quite fine if both cases are just one statement. And while there's no requirement about putting brackets around both clauses if only one needs them, I have found plenty of bugs around if
statements with brackets around only one path. It's surprising. :)
On a further note, a text editor with bracket-matching capabilities would be a huge assistance. Many editors will highlight brackets that aren't properly nested, and some provide a key to jump between them quickly. (In vi
and clones, the %
key will bounce between ()
, {}
, []
and <>
pairs. Very handy.)
There are two }'s right before the case
. Where are the corresponding {'s? Especially the last else
looks a bit lonely.
Seems you have a superflous "}" after each getch:
getch ();
}
^^^
精彩评论