错误预期声明c ++

时间:2012-08-13 10:12:41

标签: c++ function

更新2:我修好了,原来这是一个简单的问题,即半柱进入它不属于的地方,正在寻找完全错误的地方。

更新:删除一些注释修复了cin cout错误,现在唯一的错误是在开始和结束括号上的一个声明,一个出现在else语句中。

错误“期望声明”出现在函数playeroneturn on the opening and closing {}以及函数中if语句的结束},同样在if语句中cin和cout都给出错误“this 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; 
}

4 个答案:

答案 0 :(得分:7)

我看到几个问题(来自我的评论):

  • 你不能拥有像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); { ... }&lt; - 在函数正文之前删除;
  • 请注意,在playeroneturn中,int p1score = 0;会影响该函数的参数,它具有相同的名称
  • 再次在playeroneturn - cout 必须<<一起使用,而不是>>
  • a';'必须在错误的cout之后添加(从上一点开始)
  • playeroneturn(再次)中,reroll = end;很糟糕 - 这个end是什么,它没有被定义。

答案 1 :(得分:3)

continue是一个关键字,不能用作变量名。

答案 2 :(得分:1)

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

分号和左值是哪里?

答案 3 :(得分:0)

继续是keyword / reserved word。因此它不应该用作变量名或其他文字规范。为了感觉差异,只需将变量'continue'重命名为'continue1'并查看错误。希望这会有所帮助。