(C++)我的saveGame()函数有什么问题?当我调用函数时,它中没有任何东西运行,但没有错误
(C++) What is the issue with my saveGame() function? When I call the function, nothing within it runs but there is no error?
我几乎什么都试过了,但我不明白为什么这个函数在调用它时什么都不做。这个函数被正确地调用了:saveGame(hscore, selectedSaveSlot);
(hscore
和selectedSaveSlot
也被正确地定义为int
s)。此外,此函数在switch
语句中的另一个函数中调用。有人知道为什么它不起作用吗?
(即,当调用此函数时,cout
不会说任何内容,也不会创建任何保存文件,代码只是跳过它并继续无缝运行)
void saveGame(int highscore, int saveSlot) {
ofstream saveFile1;
ofstream saveFile2;
ofstream saveFile3;
switch (saveSlot) {
case '1':
saveFile1.open("SaveFile1.txt", ios::out);
saveFile1 << highscore;//writing highsore to a file
saveFile1.close();
cout << "Your game has been saved successfully!" << endl;
delayScroll(10, 50);
break;
case '2':
saveFile2.open("SaveFile2.txt", ios::out);
saveFile2 << highscore; //writing highsore to a file
saveFile2.close();
cout << "Your game has been saved successfully!" << endl;
delayScroll(10, 50);
break;
case '3':
saveFile3.open("SaveFile3.txt", ios::out);
saveFile3 << highscore; //writing highsore to a file
saveFile3.close();
cout << "Your game has been saved successfully!" << endl;
delayScroll(10, 50);
break;
}
inMenu = true;
}
您可能使用整数1、2、3等调用saveGame
。但是,'1'
和1
不相同。第一个(带引号)是ASCII值为49的字符,第二个是整数1。在开关中,您使用的是字符'1', '2', '3'
。如果您分别调用saveGame(highscore, 49)
、saveGame(highscore, 50)
或saveGame(highscore, 51)
,它们将匹配。但它们与saveGame(highscore, 1)
、saveGame(highscore, 2)
或saveGame(highscore, 3)
不匹配。
简而言之,这些都是真的:
'1' != 1
'2' != 2
'1' == 49
更改大小写以使用实际整数。
相关文章:
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 在运算符重载定义中使用成员函数(const错误)
- 模板,函数使用错误的构造函数来复制我的对象
- 这个返回元素位置的基于循环的函数有什么问题?
- 我的代码中有错误,未声明的标识符
- 使用返回对象的函数处理错误
- 如何从返回有符号整数的函数返回错误
- Microsoft Visual Studio发现我所有的CUDA设备端函数都有错误
- C++ ExpandEnvironmentStringsForUser 函数有时会返回错误的值
- 继承默认构造函数在 gcc 中失败并在 clang 中工作,哪个有错误?
- 在c++中有错误的;未实现的构造函数
- OPNET:在OPNET的过程模型中写了一个函数,但有错误
- C ++ porgram 不会运行,我的构造函数是否有错误
- 当设置-fno-elide构造函数时,clang Xcode 4.4.1是否有错误
- realloc C 函数的可能包装函数有什么错误?
- 错误到底是什么,对于错误地调用可移动和不可复制成员的复制构造函数有什么解决方法
- (C++)我的saveGame()函数有什么问题?当我调用函数时,它中没有任何东西运行,但没有错误
- 在对象上调用静态成员函数——有没有办法将其转化为编译器错误
- 当函数有多个参数时读取表返回错误
- 我所有的函数都有错误 C2084:函数'function'已经有一个主体