0

更新 2: 私はそれを修正しました。セミ カラムが属していない場所に入るという単純な問題であり、完全に間違った場所を探していたことがわかりました。

更新: いくつかのコメントを削除すると、cin cout エラーが修正されました。現在、唯一のエラーは、開き括弧と閉じ括弧の宣言であることが予想され、else ステートメントに表示されています。

エラー「宣言が必要です」は、関数 playeroneturn の開閉 {} および関数内の if ステートメントの終了 } に表示されます。if ステートメントでも、cin と cout の両方で「この宣言には no があります」というエラーが表示されます。ストレージ クラスまたは型指定子"

#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; 
}
4

4 に答える 4

8

(私のコメントから)いくつかの問題があります:

  • のようなステートメントを持つことはできません1 + (rand() % 6); = roll。これは何をすることになっていますか?あなたがなりたいならroll1 + (rand() % 6);それを書いてくださいroll = 1 + (rand() % 6);
  • 変数として使用することはできませんcontinue、それは予約されたキーワードです
  • >><<使用する場合は混合できませんcout。のみを使用してください<<
  • p1scoreは初期化されません(コンパイラのエラーではありませんが、それでも-初期化せずにif-statement(最初の条件(if(roll > 1))が真である、の本体で)使用するため、ガベージ値が取得されます
  • 含めました#include <string>か?
  • 持っていません。持ってusing std::stringいない場合はusing namespace std;、これについてもコンパイラエラーが発生します。

  • 答えを待っています-最初の前に何がありますか{


編集:あなたが編集しているのを見て、あなたは他のいくつかの変更を行う必要があります:

  • まず、の定義playeroneturnが間違っています: <-関数の本体の前にint playeroneturn(int p1score); { ... }削除します;
  • で、関数のパラメータをシャドウし、同じ名前であるplayeroneturnことに注意してくださいint p1score = 0;
  • 再びplayeroneturn-で使用するcout 必要<<があります>>
  • ';' 間違った後に追加する必要がありますcout(前のポイントから)
  • playeroneturn(再び)で、悪いreroll = end;です-これは何endですか、それはどこにも定義されていません。
于 2012-08-13T10:17:55.203 に答える
3

continueはキーワードであり、変数名として使用することはできません。

于 2012-08-13T10:14:16.143 に答える
1
while(continue == "roll")
{
    1 + (rand() % 6); = roll //<--- what is this? no ; and no l value
        if(roll > 1)

セミコロンと左辺値はどこにありますか?

于 2012-08-13T10:15:12.880 に答える
0

continue はキーワード/予約語です。したがって、変数名またはその他のリテラル仕様として使用しないでください。違いを感じるには、変数「continue」の名前を「continue1」に変更して、エラーが発生するのを確認してください。これが役立つことを願っています。

于 2012-08-13T10:20:17.700 に答える