2012-08-13 118 views
0

更新2:以及我修復它,原來這是一個簡單的問題,半列進入它不屬於,在完全錯誤的地方看。錯誤預計聲明C++

更新:刪除一些註釋修復了cin cout錯誤,現在唯一的錯誤是期望在開始和結束括號中聲明,並且在else語句中出現了一個聲明。

錯誤「expected a declaration」出現在函數playeroneturn上的if和if函數的關閉}} if語句中,cin和cout都給出錯誤「此declaraton沒有存儲類或類型說明符」

#include "stdafx.h" 
#include "iostream" 
#include "ctime" 
#include "cstdlib" 
#include "string" 

//prototype function declaration from stackoverflow.com/questions/2575153/must-declare-function-prototype-in-c 
int help(int menu); 
int start(int menu); 
int oneplaymode(); 
int playeroneturn(); 
int playertwoturn(); 

//function start is called to display the games menu screen 
int start(int menu) 
{ 
    using std::cout; 
    using std::cin; 
    using std::endl; 
    cout << "#########################################################################" << endl; 
    cout << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t PIGS\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t1. 1 Player\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t2. 2 Player\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t3. help\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl<< "#\t\t\t\t\t\t\t\t\t#" << endl<< "#\t\t\t\t\t\t\t\t\t#" << endl<< "#\t\t\t\t\t\t\t\t\t#" << endl<< "#\t\t\t\t\t\t\t\t\t#" << endl<< "#\t\t\t\t\t\t\t\t\t#" << endl; 
    cout << "#########################################################################" << endl; 
    cout << "enter number for your selection: "; 
    cin >> menu; 
    if(menu == 1) 
    { 
     cout << "single-player not yet implemented" << endl; 

    } 
    else if(menu == 2) 
    { 
    int twoplayermode(); 
    } 
    else if(menu == 3) 
    { 
    help(menu); 
    } 
    else 
    { 
    cout << "Error: Please choose a valid option" << endl; 

    start(menu); 

    } 
return(menu); 
} 

int help(int menu) 
{ 
    using std::cout; 
    using std::endl;  
    cout << "#########################################################################" << endl; 
    cout << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t HELP\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "# The objective of pigs is to be the first to score 100.\t\t#" << endl << "# Each turn you must roll a die then pass or roll again.\t\t#" << endl << "# You must then choose to ROLL again or END your turn.\t\t\t#" << endl << "# At the end of your turn your total is added to your score.\t\t#" << endl << "# However if you roll a 1 your turn ends and you score 0.\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl<< "#\t\t\t\t\t\t\t\t\t#" << endl<< "#\t\t\t\t\t\t\t\t\t#" << endl<< "#\t\t\t\t\t\t\t\t\t#" << endl<< "#\t\t\t\t\t\t\t\t\t#" << endl<< "#\t\t\t\t\t\t\t\t\t#" << endl; 
    cout << "#########################################################################" << endl; 
    system("pause"); 
    start(menu); 
    return 0; 
} 

int playeroneturn(int p1score); 
{ 
    using namespace std; 
    using std::cout; 
    using std::cin; 
    using std::endl; 
    using std::srand; 
    using std::rand; 
    using std::time; 
    using std::string; 
    srand((unsigned int)time(0)); 
    int roll = 0; 
    int p1score = 0; 
    string reroll = "roll"; 
    while(reroll == "roll") 
    { 
     roll = 1 + (rand() % 6); 
      if(roll > 1) 
      { 
       p1score = p1score+roll; 
       // using " in a string msdn.microsoft.com/en-us/library/267k4fw5.aspx 
       cout << "You rolled a " << roll << endl << "Type roll to roll again or end to end your turn."; // error on cout this declaraton has no storage class or type specifier and error on first << expected a ; 
       cin >> reroll; 
      } 
      else 
      { 
       cout >> "Bad luck! you rolled a 1, your turn is over and you score nothing!" 
       p1score = 0; 
       reroll = end; 
      } 
    } 
    return p1score; 
} 
+5

你有很多錯誤 - '繼續'是一個保留字;你不能在'cout'中混合'>>和'<<',只能使用'<<;你不能像'1 +(rand()%6)這樣的語句; = roll' - 這應該做什麼?什麼是之前的第一個'''等 – 2012-08-13 10:14:27

+0

什麼是函數名稱和參數? – 2012-08-13 10:15:10

+0

除了已經指出的其他人之外,我沒有看到你能夠使用'string'而不是'std :: string'。 – ChiefTwoPencils 2012-08-13 10:20:23

回答

3

continue是一個關鍵字,並且可以不被用作變量名。

1
while(continue == "roll") 
{ 
    1 + (rand() % 6); = roll //<--- what is this? no ; and no l value 
     if(roll > 1) 

哪裏是分號和左值?

7

我看到的幾個問題(從我的意見):

  • 你不能有像1 + (rand() % 6); = roll語句。這應該做什麼?如果你想roll1 + (rand() % 6);,寫roll = 1 + (rand() % 6);
  • 不能使用continue作爲變量,它保留關鍵字
  • 使用cout當你不能混用>><<。只能使用<<
  • p1score未初始化(它不是編譯器的錯誤,但還是 - 你不被初始化,使用它(在if-statement,其中第一個條件(if(roll > 1))爲true)的身體,所以你會得到垃圾值
  • 有你包括#include <string>
  • 你沒有using std::string,如果你沒有using namespace std;做,你會對此有一個編譯器錯誤,太

  • 等待回答 - 什麼是前第一個{


編輯:看着你編輯,你需要做一些其他的變化:

  • 第一,playeroneturn的定義是錯誤的:int playeroneturn(int p1score); { ... } < - 前函數的去除;身體
  • 請注意,在playeroneturn,int p1score = 0;陰影功能的參數,它有山姆Ë名
  • playeroneturn再次
  • - cout必須<<使用,不與>>
  • 一個 ';'必須錯cout之後增加(從之前的點)playeroneturn(再次)
  • reroll = end;是壞的 - 這是什麼end,它不是無處定義。
+0

ok我已經包含了當前代碼完全包含第一個{之前的所有代碼(根據要求在帖子中,我已經在您的建議後取得了一些成功,現在只發生在函數的打開和關閉{}和} if語句。在cin和cout語句的if語句中出現了一個新的錯誤,「這個聲明沒有存儲類或類型說明符」,在這些語句現在期望的「>>和<<旁邊;但是我會冒險猜測修復第一個問題也會解決後者。 – 2012-08-13 12:30:43

+0

另外我付出了很多的錯誤,我想這就是爲什麼我很難找到他們,我只是在尋找一個。 – 2012-08-13 12:38:36

+0

@NathanLaw - 看我的編輯。 – 2012-08-13 13:19:49

0

繼續是keyword/reserved word。因此不應將其用作變量名稱或其他文字說明。爲了感覺不同,只需將變量'continue'重命名爲'continue1'並查看錯誤即可。希望這會有所幫助。