这是我到目前为止所写的内容。我认为它会完美地工作,因为我大部分从我的旧Plinko实验室代码中转录它,我没有使用函数,更改它以便模拟器块成为它自己的功能。但是,我得到了五个错误。
第17行(ran = ran()%2):"表观调用的括号前面的表达式必须具有(指针 - )函数类型"。这是什么意思?
在第10行((srand(time(0));):"'参数&#39 ;:从&time; t'转换为' unsigned int&#39 ;,可能丢失数据"。这是什么意思?
再次在第17行:"术语不评估为采用0参数的函数"。这是什么意思?
第99行(drop_simulator(slot_number,1);):"'参数':来自' double'到' int',可能丢失数据"。我该如何防止这种情况?
第129行(total_rewards + = drop_simulator(slot_number,0);):与上述相同。
我将不胜感激任何帮助。我觉得我已经做了我可能做的一切,但感觉好像我已经遇到了这个问题。
#include <iostream>
#include <cstdlib>
#include <math.h>
#include <time.h>
using namespace std;
double drop_simulator(int slot_number, int number_of_chips_dropped)
{
srand(time(0));
double plink;
double ran;
double location;
location = slot_number;
for (int i = 0; i < 12; i++)
{
ran = (ran() % 2);
plink = ran;
plink -= 0.5;
location += plink;
if (location >= 8)
{
location -= 1;
}
else if (location <= 0)
{
location += 1;
}
if (number_of_chips_dropped == 1)
{
cout << location << endl;
}
}
int reward = 0;
if (location == 0)
{
reward = 100;
}
else if (location == 1)
{
reward = 500;
}
else if (location == 2)
{
reward = 1000;
}
else if (location == 3)
{
reward = 0;
}
else if (location == 4)
{
reward = 10000;
}
else if (location == 5)
{
reward = 0;
}
else if (location == 6)
{
reward = 1000;
}
else if (location == 7)
{
reward = 500;
}
else if (location == 8)
{
reward = 100;
}
return reward;
}
int main()
{
int input = 0;
while (input != 3)
{
cout << "Please select one of these three options." << endl;
cout << "1 - Drop a single chip into one slot" << endl;
cout << "2 - Drop multiple chips into one slot" << endl;
cout << "3 - Quit the program" << endl;
cin >> input;
cout << endl;
if (input == 1)
{
double slot_number;
cout << "Pick a slot (0 - 8) into which you'd like to drop your chip: ";
cin >> slot_number;
cout << endl;
if (slot_number < 0 || slot_number > 8)
{
cout << "INVALID SELECTION" << endl << endl;
}
else if (slot_number >= 0 && slot_number <= 8)
{
double reward = 0;
drop_simulator(slot_number, 1);
cout << "You won $" << reward << "!" << endl;
}
}
else if (input == 2)
{
int number_of_chips_dropped;
cout << "How many chips would you like to drop?" << endl;
cin >> number_of_chips_dropped;
cout << endl;
if (number_of_chips_dropped <= 0)
{
cout << "INVALID SELECTION. Please enter a positive number." << endl << endl;
}
else if (number_of_chips_dropped > 0)
{
double slot_number;
cout << "Pick a slot (0 - 8) into which you'd like to drop your chips.";
cin >> slot_number;
cout << endl;
if (slot_number < 0 || slot_number > 8)
{
cout << "INVALID SELECTION" << endl << endl;
}
else if (slot_number >= 0 && slot_number <= 8)
{
double total_rewards = 0;
for (int i = 0; i < number_of_chips_dropped; i++)
{
total_rewards += drop_simulator(slot_number, 0);
}
double average_winnings = total_rewards / number_of_chips_dropped;
cout << "The average rewards per chip was $" << average_winnings << "." << endl;
cout << "Your total rewards were $" << total_rewards << "." << endl;
}
}
}
else if (input < 1 || input > 3)
{
cout << "INVALID SELECTION. Please enter 1, 2 or 3." << endl << endl;
}
else if (input == 3)
{
cout << "GOODBYE" << endl << endl;
system("pause");
}
}
return 0;
}
答案 0 :(得分:1)
错误消息确实试图告诉问题:
第17行: ran =(ran()%2);“明显调用的括号前面的表达式必须具有(指向 - ) - 函数类型”。
这里ran()
(parantheses之前的标记)被标记为不是函数或指向函数的类型。
类似
“术语不评估为采用0参数的函数”
表示ran()
在当前范围内不知道是声明的函数。
可能您打算在这里使用rand()
来获取随机数。
第10行: 函数srand(时间(0));
参数':“从'time_t'转换为'unsigned int',可能会丢失数据”
srand
以unsigned int
作为参数类型。您正在将time(0)
的返回值设为它。这是time_t
。 (可能是一种long
。)这可能会失去一些精确度(long
到int
)。由于您可能不关心初始化RNG,您可以使用明确的演员:
srand((unsigned int)time(0));
第99行: drop_simulator(slot_number,1);
和
第129行: total_rewards + = drop_simulator(slot_number,0);使
“'参数':从'double'转换为'int',可能会丢失数据”
将slot_number
定义为double
main
,而drop_simulator is being declared as
的正式参数. As double can represent more values that int, you get the problem indicated. For fixing this you could just change the declaration of
slot_number`:
int slot_number;
主页上的。
答案 1 :(得分:0)
在第17行(ran = ran()%2):“明显调用的括号前面的表达式必须具有(指向 - )函数类型”。这是什么意思?
这是因为ran
不是函数名,您可能想要
ran = rand() % 2
这使得run成为调用函数rand
2的模数的结果。
这将修复第17行的第二个错误。
第99行(drop_simulator(slot_number,1);):“'参数':从'double'转换为'int',可能丢失数据”。我该如何防止这种情况?
第129行(total_rewards + = drop_simulator(slot_number,0);):与上述相同。
这是因为你不必要地将“slot_number”声明为double。这意味着用户可以输入您不想要的值,例如“1.2345”。只需将slot_number
的声明从double slot_number
更改为int slot_number
。
第10行((srand(time(0));):“'argument':从'time_t'转换为'unsigned int',可能会丢失数据”。这是什么意思?
这意味着srand
需要无符号整数,但time(0)
会返回time_t
。你可以用cast来解决这个问题:
srand(unsigned int(time(0)));
完整代码:
#include <iostream>
#include <cstdlib>
#include <math.h>
#include <time.h>
using namespace std;
double drop_simulator(int slot_number, int number_of_chips_dropped)
{
srand(static_cast<unsigned int>(time(0)));
double plink;
double ran;
double location;
location = slot_number;
for (int i = 0; i < 12; i++)
{
ran = (rand() % 2);
plink = ran;
plink -= 0.5;
location += plink;
if (location >= 8)
{
location -= 1;
}
else if (location <= 0)
{
location += 1;
}
if (number_of_chips_dropped == 1)
{
cout << location << endl;
}
}
int reward = 0;
if (location == 0)
{
reward = 100;
}
else if (location == 1)
{
reward = 500;
}
else if (location == 2)
{
reward = 1000;
}
else if (location == 3)
{
reward = 0;
}
else if (location == 4)
{
reward = 10000;
}
else if (location == 5)
{
reward = 0;
}
else if (location == 6)
{
reward = 1000;
}
else if (location == 7)
{
reward = 500;
}
else if (location == 8)
{
reward = 100;
}
return reward;
}
int main()
{
int input = 0;
while (input != 3)
{
cout << "Please select one of these three options." << endl;
cout << "1 - Drop a single chip into one slot" << endl;
cout << "2 - Drop multiple chips into one slot" << endl;
cout << "3 - Quit the program" << endl;
cin >> input;
cout << endl;
if (input == 1)
{
int slot_number;
cout << "Pick a slot (0 - 8) into which you'd like to drop your chip: ";
cin >> slot_number;
cout << endl;
if (slot_number < 0 || slot_number > 8)
{
cout << "INVALID SELECTION" << endl << endl;
}
else if (slot_number >= 0 && slot_number <= 8)
{
double reward = 0;
drop_simulator(slot_number, 1);
cout << "You won $" << reward << "!" << endl;
}
}
else if (input == 2)
{
int number_of_chips_dropped;
cout << "How many chips would you like to drop?" << endl;
cin >> number_of_chips_dropped;
cout << endl;
if (number_of_chips_dropped <= 0)
{
cout << "INVALID SELECTION. Please enter a positive number." << endl << endl;
}
else if (number_of_chips_dropped > 0)
{
int slot_number;
cout << "Pick a slot (0 - 8) into which you'd like to drop your chips.";
cin >> slot_number;
cout << endl;
if (slot_number < 0 || slot_number > 8)
{
cout << "INVALID SELECTION" << endl << endl;
}
else if (slot_number >= 0 && slot_number <= 8)
{
double total_rewards = 0;
for (int i = 0; i < number_of_chips_dropped; i++)
{
total_rewards += drop_simulator(slot_number, 0);
}
double average_winnings = total_rewards / number_of_chips_dropped;
cout << "The average rewards per chip was $" << average_winnings << "." << endl;
cout << "Your total rewards were $" << total_rewards << "." << endl;
}
}
}
else if (input < 1 || input > 3)
{
cout << "INVALID SELECTION. Please enter 1, 2 or 3." << endl << endl;
}
else if (input == 3)
{
cout << "GOODBYE" << endl << endl;
system("pause");
}
}
return 0;
}