gpt4 book ai didi

c++ - 不理解基本c++程序中的错误

转载 作者:太空狗 更新时间:2023-10-29 23:31:09 25 4
gpt4 key购买 nike

#include <iostream>

using namespace std;

int main(void) {
int number, guess;

srand(time(NULL));
number = rand() % 101;

cout << "Guess a number between 0-100: ";
cin >> guess;

if(number > guess) {
cout << "The number is greater!\n";
}
else if(number < guess) {
cout << "The number is smaller!\n";
}
else {
cout << "Cognratulations! The number is "number"!\n";
}
cin-get();

return 0;
}

error: 'srand' was not declared in this scope
error: 'rand' was not declared in this scope
error :expected ';' before 'number'

最佳答案

你需要添加:

#include <cstdlib>

包括 srand()rand()

当您需要使用这样的功能时,查看手册页(或谷歌搜索)会告诉您需要包含哪些 header 。

http://www.cplusplus.com/reference/clibrary/cstdlib/

关于c++ - 不理解基本c++程序中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8771836/

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