gpt4 book ai didi

c++ - ']' 标记之前的预期主表达式”

转载 作者:行者123 更新时间:2023-11-28 06:57:57 25 4
gpt4 key购买 nike

//Purpose: To simulate the probability that a car will make a decision in a video game.
//40% of the time the car will turn left, 30% right, 20% straight, and 10% explode.

#include <iostream>
#include <cstdlib>
#include <time.h>
#include <iomanip>

using namespace std;

void GetCount( count[] );

int main()
{
int count[4] = {0};
unsigned int k;
float theoretical;

srand( (unsigned) time(NULL) );

GetCount( int count[] );

cout.setf( ios::showpoint | ios::fixed );
cout << setprecision(2);
cout << " Car Simulation" << endl << endl
<< " Number of Experimental Theoretical % Error " << endl
<< " Times Selected Percent Percent " << endl
<< " ---------------- -------------- ------------- ------ " << endl;

for ( k = 0; k < 4; k++ )
{
switch(k)
{
case 0:
cout << "Left: ";
theoretical = 40;
break;
case 1:
cout << "Right: ";
theoretical = 30;
break;
case 2:
cout << "Straight: ";
theoretical = 20;
break;
default:
cout << "Explosion:";
theoretical = 10;
break;
} // end switch

cout << setw( 12 ) << count[ k ]
<< setw( 20 ) << count[ k ] / 10000.0 * 100.0
<< setw( 19 ) << theoretical
<< setw( 13 ) << ( ( count[ k ] - theoretical * 100 ) / (theoretical * 100) * 100 )
<< endl << endl;
} //end for

cout << endl << endl;

system("PAUSE");
return 0;

} //end main

void GetCount( int count[] )
{
int randNum;
unsigned int k;

for( k = 0; k < 10000; k++ )//generates random number 1-100 10,000 times
{
randNum = rand() % 100 + 1;

if( randNum <= 100 && randNum > 60 )
count[0]++;
else if( randNum <= 60 && randNum > 30 )
count[1]++;
else if( randNum <= 30 && randNum > 10 )
count[2]++;
else
count[3]++;
}//end for

}//end function definition

上面的代码是用来模拟汽车在视频游戏中做出决定的。

在十字路口,汽车有 40% 的时间会左转,30% 的时间会右转,20% 的时间会直行,还有 10% 的时间会爆炸。

程序执行 10,000 个这样的决定并输出结果。

除了函数“GetCount()”外,一切都很好。调用此函数时,应该会生成代表决策的随机数,并将每个数字生成的次数存储在它们的数组中。

但是,当我编译程序时,出现错误提示:

"in line 21, expected primary-expression before ']' token".

这是我调用函数的同一行。我已经尝试了一些方法来尝试修复它,但我不断收到一些错误。

如有任何建议,我们将不胜感激,谢谢。

最佳答案

GetCount( int count[] );

应该是

GetCount(count);

类型不必重复;编译器知道 count 与您刚刚声明为 int[] 的变量相同。

关于c++ - ']' 标记之前的预期主表达式”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22917612/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com