(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);hscoreselectedSaveSlot 也被正确定义为 int)。此外,此函数在 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

更改您的案例以使用实际整数。