【发布时间】:2017-11-21 10:37:30
【问题描述】:
我正在编写一个蒙特卡洛模拟,我的第一个函数是一个输入,但它不断返回一个错误,指出变量“未在此范围内声明”,我尝试在 main 中添加变量类型,它仍然不建立。然后我在函数中添加了变量类型(cin>>rounds 到 cin>> int rounds),错误发生了变化但仍然不起作用。谁能告诉我发生了什么以及我需要做什么才能使该功能正常工作。
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
int getInput();
using namespace std;
int main (void){
//set up random
srand(time(NULL));
//1st function
getInput();
}
/* @description gets a valid user input
* @param output and input
*/
int getInput(){
cout<<"enter amount of rounds";
cin>> rounds; **(error is here on line 24 ("rounds not declared in
this scope")**
}
【问题讨论】:
-
我不敢相信你真的忘了声明变量,因为那很明显。可能是你忘了在你的问题中提到它吗?如果你真的忘记了,那可以解释为什么找不到它。
-
请不要发布错误的解释部分;那没用。发布整个内容,包括编译器为您提供的行号和列号。我投票决定关闭它,因为它没有包含足够的信息来形成有用的答案,因为您没有显示任何关于您正在编译多少文件以及如何编译等的迹象。