2013-01-04 182 views
0

所以我真的不知道我的代碼有什麼問題。任何建議都會有幫助。看看我評論的代碼部分(朝中間)。電腦給我一個錯誤,說預計會有一個「;」。有些東西是錯誤的支架或我搞砸了一些其他地方,只是無法找到它。C++編譯錯誤/語法錯誤?錯誤原因不明

//Experiment2 
//Creating functions and recalling them. 
#include <iostream> 
using namespace std; 

void a() 
{ 
cout<<"You try running but trip and fall and the crazy man kills you!!!!      HAAHAHAHHAHA."; 
} 


void b() 
{ 
cout<<"You stop drop and roll and the crazy man is confused by this and leaves you alone!"; 
} 

void c() 
{ 
cout<<"you try fighting the man but end up losing sorry!"; 
} 




int main() 
{ 
int a; 
int b; 
int c; 
int d; 
a=1; 
b=2; 
c=3; 

cout<< "Once upon a time you was walking to school,\n"; 
cout<< " when all of the sudden some crazy guy comes running at you!!!!"<<endl; 
cout<< " (This is the begining of your interactive story)"<<endl; 
cout<< "Enter in the number according to what you want to do.\n"; 
cout<< " 1: run away, 2:stop drop and roll, or 3: fight the man."<<endl; 
cin>>d; 
void checkd() 
//i dont know whats wrong with the bracket! the computer gives me an error saying expected a";" 
{ 
    if(d==1) 

    { 
     void a(); 
    } 

    if(d==2) 

    { 
     void b(); 
    } 

    if(d==3) 

    { 
     void c(); 
    } 
} 
} 

回答

1

您不能在另一個函數中定義函數。您在main函數中定義了函數checkd()

移動函數體main之外,剛剛從main調用該函數爲:

checkd(d); 

也許,你還想要的功能把它需要比較的參數。

此外,

void a(); 

不會調用函數a()它只是聲明函數,調用你需要的功能:

a(); 

void checkd(int d) 

{ 
    if(d==1) 

    { 
     a(); 
    } 

    if(d==2) 

    { 
     b(); 
    } 

    if(d==3) 
    { 
     c(); 
    } 
} 
int main() 
{ 
    .... 
    .... 
    cout<< " 1: run away, 2:stop drop and roll, or 3: fight the man."<<endl; 
    cin>>d; 
    checkd(); 

    return 0; 
} 
+0

謝謝!!!!! !!!!!!!!! –

+0

@Constantinius:函數不帶參數,但它應該。 –