(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 我的 C++ 什么 问题 调用      更新时间:2023-10-16

我几乎什么都试过了,但我不明白为什么这个函数在调用它时什么都不做。这个函数被正确地调用了:saveGame(hscore, selectedSaveSlot);hscoreselectedSaveSlot也被正确地定义为ints)。此外,此函数在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

更改大小写以使用实际整数。