2015-05-26 69 views
-25

我正在製作一個隨機模擬遊戲節目的程序。該程序選擇三個門中的一個,以隨機隱藏獎品。電腦選擇一扇門,看看它是否贏了。如果我改變我的選擇而不改變它,這個循環超過10000次,看看我贏了多少次。哪裏有語法錯誤?

我收到了一堆的語法

#include <path-spec> 
#include <stdio.h> 
#include <time.h> 

void count(Status result, Door* pLast_pick, Door* pPick, int* pWin_unchanged, int* pWin_changed); 

void randomize_door(Door* pJackpot); 

void pick_door(Door* pPick); 

Status check(Door* pPick, Door* pJackpot); 

enum status {WIN=1,LOSE}; 

enum door { FIRST=1, SECOND, THIRD }; 

typedef enum door Door; 
typedef enum status Status; 

int main(int argc, char* argv[]){ 
    int i = 0; 
    srand(time); 
    Status result; 
    Status* pResult = &result ; 
    Door jackpot, pick, last_pick=NULL; 
    Door* pJackpot = &jackpot, * pPick=&pick, *pLast_pick; 
    int win_unchanged = 0, win_changed=0; 
    int* pWin_unchanged = &win_unchanged, *pWin_changed=&win_changed; 

    while (i < 10000){ 
     last_pick = NULL; 
     randomize_door(pJackpot); 
     pick_door(pPick); 
     result = check(pPick, pJackpot); 
     count(result, pLast_pick, pPick, pWin_unchanged, pWin_changed); 

     i++; 
    } 

    printf("Wins when changed choice: %d , wins when choice is unchanged: %d", win_changed, win_unchanged); 
    return 0; 
} 

void randomize_door(Door* pJackpot){ 

    *pJackpot = rand() % 3 + 1; 
} 

void pick_door(Door* pPick){ 

    *pPick = rand() % 3 + 1; 
} 

Status check(Door* pPick, Door* pJackpot){ 
    if (*pPick == *pJackpot){ 
     return WIN; 
    } 
    else{ 
     return LOSE; 
    } 
} 

void count(Status result, Door* pLast_pick, Door* pPick, int* pWin_unchanged, int* pWin_changed) { 
    if (*pLast_pick == *pPick){ 
     if (result == WIN){ 
      *pWin_unchanged++; 
     } 
    } 
    else{ 
     if (result == WIN){ 
      *pWin_changed++; 
     } 
    } 

    *pLast_pick = *pPick; 
} 

下面都是錯誤和線事情發生在。他們中的大多數被忘記了};,這在功能標題中沒有任何意義。

1>------ Build started: Project: Program4.1, Configuration: Debug Win32 ------ 
1> daily4.c 
daily4.c(4): error C2146: syntax error : missing ')' before identifier 'result' 
daily4.c(4): error C2061: syntax error : identifier 'result' 
daily4.c(4): error C2059: syntax error : ';' 
daily4.c(4): error C2059: syntax error : ',' 
daily4.c(4): error C2059: syntax error : ')' 
daily4.c(6): error C2143: syntax error : missing ')' before '*' 
daily4.c(6): error C2143: syntax error : missing '{' before '*' 
daily4.c(6): error C2059: syntax error : ')' 
daily4.c(8): error C2143: syntax error : missing ')' before '*' 
daily4.c(8): error C2143: syntax error : missing '{' before '*' 
daily4.c(8): error C2059: syntax error : ')' 
daily4.c(10): error C2061: syntax error : identifier 'check' 
daily4.c(10): error C2059: syntax error : ';' 
daily4.c(10): error C2143: syntax error : missing ')' before '*' 
daily4.c(10): error C2143: syntax error : missing '{' before '*' 
daily4.c(10): error C2143: syntax error : missing ';' before '*' 
daily4.c(10): error C2059: syntax error : ')' 
daily4.c(16): error C2370: 'Door' : redefinition; different storage class 
daily4.c(10) : see declaration of 'Door' 
daily4.c(21): warning C4013: 'srand' undefined; assuming extern returning int 
daily4.c(24): error C2146: syntax error : missing ';' before identifier 'jackpot' 
daily4.c(24): error C2065: 'jackpot' : undeclared identifier 
daily4.c(24): error C2065: 'pick' : undeclared identifier 
daily4.c(24): error C2065: 'last_pick' : undeclared identifier 
daily4.c(24): warning C4047: '=' : 'int' differs in levels of indirection from 'void *' 
daily4.c(25): error C2297: '*' : illegal, right operand has type 'int *' 
daily4.c(25): error C2065: 'jackpot' : undeclared identifier 
daily4.c(25): error C2065: 'pick' : undeclared identifier 
daily4.c(25): warning C4047: '=' : 'int' differs in levels of indirection from 'int *' 
daily4.c(25): error C2065: 'pLast_pick' : undeclared identifier 
daily4.c(25): error C2100: illegal indirection 
daily4.c(30): error C2065: 'last_pick' : undeclared identifier 
daily4.c(30): warning C4047: '=' : 'int' differs in levels of indirection from 'void *' 
daily4.c(31): warning C4013: 'randomize_door' undefined; assuming extern returning int 
daily4.c(32): warning C4013: 'pick_door' undefined; assuming extern returning int 
daily4.c(33): warning C4013: 'check' undefined; assuming extern returning int 
daily4.c(34): warning C4013: 'count' undefined; assuming extern returning int 
daily4.c(34): error C2065: 'pLast_pick' : undeclared identifier 
daily4.c(43): error C2143: syntax error : missing ')' before '*' 
daily4.c(43): error C2143: syntax error : missing '{' before '*' 
daily4.c(43): error C2059: syntax error : ')' 
daily4.c(43): error C2054: expected '(' to follow 'pJackpot' 
daily4.c(48): error C2143: syntax error : missing ')' before '*' 
daily4.c(48): error C2143: syntax error : missing '{' before '*' 
daily4.c(48): error C2059: syntax error : ')' 
daily4.c(48): error C2054: expected '(' to follow 'pPick' 
daily4.c(53): error C2143: syntax error : missing ')' before '*' 
daily4.c(53): error C2143: syntax error : missing '{' before '*' 
daily4.c(53): error C2143: syntax error : missing ';' before '*' 
daily4.c(53): error C2059: syntax error : ')' 
daily4.c(53): error C2054: expected '(' to follow 'pJackpot' 
daily4.c(62): error C2143: syntax error : missing ')' before '*' 
daily4.c(62): error C2081: 'Door' : name in formal parameter list illegal 
daily4.c(62): error C2143: syntax error : missing '{' before '*' 
daily4.c(62): error C2143: syntax error : missing ';' before '*' 
daily4.c(62): error C2059: syntax error : 'type' 
daily4.c(62): error C2059: syntax error : ')' 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 
+5

錯誤會清晰地顯示在錯誤堆棧! – tharif

+0

我知道他們在告訴我這些錯誤。但這些錯誤根本不存在。 –

+0

您需要首先定義'enum',然後在函數聲明中使用它們。你已經改變了順序。 –

回答

2

樣品固定

#include <stdio.h> 
#include <time.h> 
#include <stdlib.h> //for srand 

//enum and typedef defined prior to use 
enum status {WIN=1,LOSE}; 
enum door { NONE, FIRST=1, SECOND, THIRD };//Define NONE instead of NULL 

typedef enum door Door; 
typedef enum status Status; 


void count(Status result, Door* pLast_pick, Door* pPick, int* pWin_unchanged, int* pWin_changed); 
void randomize_door(Door* pJackpot); 
void pick_door(Door* pPick); 
Status check(Door* pPick, Door* pJackpot); 

int main(int argc, char* argv[]){ 
    int i = 0; 
    Status result; 
    Status* pResult = &result ; 
    Door jackpot, pick, last_pick=NONE; 
    Door* pJackpot = &jackpot, * pPick=&pick, *pLast_pick; 
    int win_unchanged = 0, win_changed=0; 
    int* pWin_unchanged = &win_unchanged, *pWin_changed=&win_changed; 

    srand(time(NULL));//If you use the equivalent of the C89 compiler can not be mixed the declaration statement and execution statements. 

    ... 
+1

請解釋你解決了什麼問題以及爲什麼。 – dimo414

+2

@ dimo414閱讀我的評論。 – BLUEPIXY