2011-03-12 73 views
2

这是什么意思? 我的计划是这样的:(注:有误差的情况下2.前行未来线)DEV C++错误:'}'令牌之前的预期声明

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

请重新格式化您的代码。 – quasiverse 2011-03-12 06:14:50

+0

最有可能意味着你的“{”,“}”不匹配。我建议使用可以理解C语法并突出显示匹配大括号的编辑器。 – 2011-03-12 06:18:35

+2

这意味着你的代码是一个庞大的缩进块。重构。 – 2011-03-12 06:25:13

回答

0

case之前有两个}。相应的{'s?特别是最后的else看起来有点孤单。

0

当我重新格式化你的代码后,它会在网站上显示出来,这些错误非常清楚。

我建议,当你周围使用if条款支架,还用身边的else条款:

if (foo) { 
    /* ... */ 
} else { 
    /* ... */ 
} 

娅括号是相当好的,如果这两种情况下都只是一个声明。尽管如果只有一个需要使用这两个子句,并不需要在两个子句之间放置括号,但我发现大量的if语句周围只有一个路径的括号。这很令人惊讶。 :)

进一步说明,具有括号匹配功能的文本编辑器将是一个巨大的帮助。许多编辑会突出显示没有正确嵌套的括号,有些提供了快速跳转的关键。 (。在vi和克隆,在%键将(){}[]<>对之间弹跳非常方便)

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之后立即丢失了您的范围。

0

好像你有superflous “}” 每个残培后:

getch(); 
} 
^^^ 
相关问题