2011-03-12 72 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(); 
} 
^^^ 
相關問題