gpt4 book ai didi

c++ - C++中一些无法解释的错误

转载 作者:太空宇宙 更新时间:2023-11-04 14:50:16 24 4
gpt4 key购买 nike

我有以下代码:

#include <cstdlib>
#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
int a,n,count;
count=0; randomize();
a=1+random(100);
cout<<"Enter A No. Between 1 to 100";
do
{
cin>>n;
count++;
if(n>a)
cout<<"Enter a lower no.";
else if(n<a)
cout<<"Enter a higher no.";
}while(n!=a);
cout<<count;

system("PAUSE");
return EXIT_SUCCESS;
}

错误是:

  • E:\c++\main.cpp 在函数`int main()'中:
  • 10 E:\c++\main.cpp `randomize' undeclared(先用这个函数)
  • (每个未声明的标识符在其出现的每个函数中只报告一次。)
  • 11 E:\c++\main.cpp `random' undeclared (先用这个函数)

谁能帮我理解为什么会出现这些错误?

最佳答案

randomize() 不是标准的 C++ 函数,您必须使用 srand(something) 为随机数生成器提供种子,其中 something 通常是当前时间 (time(0))。

此外,random() 不是标准函数,您必须使用 rand()

所以,像这样的东西(稍微清理了一下):

#include <ctime>
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
srand(time(0));
int n, count = 0;
int a = 1 + (rand() % 100);
cout << "Enter A No. Between 1 to 100";
do
{
cin >> n;
count++;
if (n>a)
cout << "Enter a lower no.";
else if (n<a)
cout << "Enter a higher no.";
} while(n!=a);
cout << count;

system("PAUSE");
return EXIT_SUCCESS;
}

关于c++ - C++中一些无法解释的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12114186/

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