2013-03-26 107 views
0

我正在製造一個隨機移動的機器人,但我遇到了問題。隨意移動物體

http://img199.imageshack.us/img199/2599/randommovement.jpg

爲什麼沒有這個代碼運行?它有什麼問題嗎?

# include <iostream.h> 
# include <stdlib.h> 
# include <time.h> 
# include <windows.h> 
# include <conio.h> 

void gotoxy(int x, int y){ 
    COORD coord; 
    coord.X = x; 
    coord.Y = y; 
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); 
} 

void gerakA(int pointX_par, int pointY_par, int stepX_par, int stepY_par); 
void gerakB(int pointX_par, int pointY_par, int stepX_par, int stepY_par); 
void gerakC(int pointX_par, int pointY_par, int stepX_par, int stepY_par); 
void gerakD(int pointX_par, int pointY_par, int stepX_par, int stepY_par); 
void gerakE(int pointX_par, int pointY_par, int stepX_par, int stepY_par); 
void gerakF(int pointX_par, int pointY_par, int stepX_par, int stepY_par); 
void gerakG(int pointX_par, int pointY_par, int stepX_par, int stepY_par); 
void gerakH(int pointX_par, int pointY_par, int stepX_par, int stepY_par); 

int main() 
{ 
    int stepX, stepY; 
    int z; 
    int pointX, pointY; 
    int n; 
    int random; 

    cout <<"Enter your starting point:"<<'\n'; 
    cout <<"x:"; 
    cin >> pointX; 
    cout <<"y:"; 
    cin >> pointY; 


    cout << "number of step:"; 
    cin >> n; 

    cout <<"enter the size of step :"<<'\n'; 
    cout <<"x:"; 
    cin >> stepX; 
    cout <<"y:"; 
    cin >> stepY; 

    for (z=1; z<=n+1; z++) 
    { 
    srand (time(NULL)); 
    random = rand() % 8 + 1; 
    switch (random) { 
     case 1: 
     gerakA(int pointX_par, int pointY_par, int stepX_par, int stepY_par); 
     case 2: 
     gerakB(int pointX_par, int pointY_par, int stepX_par, int stepY_par); 
     case 3: 
     gerakC(int pointX_par, int pointY_par, int stepX_par, int stepY_par); 
     case 4: 
     gerakD(int pointX_par, int pointY_par, int stepX_par, int stepY_par); 
     case 5: 
     gerakE(int pointX_par, int pointY_par, int stepX_par, int stepY_par); 
     case 6: 
     gerakF(int pointX_par, int pointY_par, int stepX_par, int stepY_par); 
     case 7: 
     gerakG(int pointX_par, int pointY_par, int stepX_par, int stepY_par); 
     case 8: 
     gerakH(int pointX_par, int pointY_par, int stepX_par, int stepY_par); 
    } 

    } 

    getch(); 
} 

void gerakA(int pointX_par, int pointY_par, int stepX_par, int stepY_par){ 
    gotoxy (pointX_par,pointY_par); 
    pointX_par = pointX_par + stepX_par; 
    pointY_par = pointY_par; 
    cout << "0"; 
} 

void gerakB(int pointX_par, int pointY_par, int stepX_par, int stepY_par){ 
    gotoxy (pointX_par,pointY_par); 
    pointX_par = pointX_par + stepX_par; 
    pointY_par = pointY_par - stepY_par; 
    cout << "0"; 
} 

void gerakC(int pointX_par, int pointY_par, int stepX_par, int stepY_par){ 
    gotoxy (pointX_par,pointY_par); 
    pointX_par = pointX_par; 
    pointY_par = pointY_par - stepY_par; 
    cout << "0"; 
} 

void gerakD(int pointX_par, int pointY_par, int stepX_par, int stepY_par){ 
    gotoxy (pointX_par,pointY_par); 
    pointX_par = pointX_par - stepX_par; 
    pointY_par = pointY_par - stepY_par; 
    cout << "0"; 
} 

void gerakE(int pointX_par, int pointY_par, int stepX_par, int stepY_par){ 
    gotoxy (pointX_par,pointY_par); 
    pointX_par = pointX_par - stepX_par; 
    pointY_par = pointY_par; 
    cout << "0"; 
} 

void gerakF(int pointX_par, int pointY_par, int stepX_par, int stepY_par){ 
    gotoxy (pointX_par,pointY_par); 
    pointX_par = pointX_par - stepX_par; 
    pointY_par = pointY_par + stepY_par; 
    cout << "0"; 
} 

void gerakG(int pointX_par, int pointY_par, int stepX_par, int stepY_par){ 
    gotoxy (pointX_par,pointY_par); 
    pointX_par = pointX_par; 
    pointY_par = pointY_par + stepY_par; 
    cout << "0"; 
} 

void gerakH(int pointX_par, int pointY_par, int stepX_par, int stepY_par){ 
    gotoxy (pointX_par,pointY_par); 
    pointX_par = pointX_par + stepX_par; 
    pointY_par = pointY_par + stepY_par; 
    cout << "0"; 
} 
+1

你是什麼意思的「不運行」? – antonijn 2013-03-26 16:49:11

+2

您的代碼存在錯誤;編譯器對此有何評論? – trojanfoe 2013-03-26 16:49:33

+0

首先,你沒有指定任何命名空間,比如'using namespace std;'但是這可能是一件好事,不要使用它!喜歡'使用std :: cout'之類的東西,或者只是將命名空間附加到所有函數中。 – 2013-03-26 16:55:01

回答

1

所以掛expected primary-expression before "int"錯誤的問題是因爲你錯誤地調用你的函數:

gerakA(int pointX_par, int pointY_par, int stepX_par, int stepY_par); 
     ^^^  ^^^^ ^^^  ^^^^ ^^^  ^^^^ ^^^  ^^^^  

你不必重述類型和您沒有使用正確的變量名稱,上面的例子應該是,或至少從我可以告訴:

gerakA(pointX, pointY, stepX, stepY);