更新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;
}
答案 0 :(得分:7)
我看到几个问题(来自我的评论):
1 + (rand() % 6); = roll
这样的陈述。这该怎么办?如果您希望roll
为1 + (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
必须与<<
一起使用,而不是>>
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'并查看错误。希望这会有所帮助。