【发布时间】:2010-04-20 14:51:23
【问题描述】:
我有一个保存游戏分数的变量。
我的变量在 if 语句之外是可访问和正确的,但在内部却不是,如下所示
score 在 main.cpp 的顶部声明并在 display 函数中计算,该函数也包含以下代码
cout << score << endl; //works
if(!justFinished){
cout << score << endl; // doesn't work prints a large negative number
endTime = time(NULL);
ifstream highscoreFile;
highscoreFile.open("highscores.txt");
if(highscoreFile.good()){
highscoreFile.close();
}else{
std::ofstream outfile ("highscores.txt");
cout << score << endl;
outfile << score << std::endl;
outfile.close();
}
justFinished = true;
}
cout << score << endl;//works
编辑: 已经意识到我的问题,当我打印出来时,它循环了很多次,所以我没有看到第一次循环的所有这些都不起作用,因此我认为其他人正在工作,而实际上它们不是第一次迭代.
【问题讨论】:
-
变量
score在哪里声明,你给它分配了什么? -
什么编译器/操作系统?你能说明分数是在哪里/如何宣布的吗?
-
score是整数类型还是类的实例?