我幾乎已經完成了該菜單並按照我希望的方式工作。然而,當我按Enter鍵而沒有輸入任何內容時,我得到斷言錯誤。下面是代碼斷言錯誤,字符串下標超出範圍
#include <iostream>
#include <cstdlib>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;
bool menu()
{
string input = "";
bool exitVar;
do
{
system("cls");
cout << " _ _ _ _ _ _ _ _ " << endl
<< " |_|_ _|_| |_| |_|_ |_| |_|_|_| " << endl
<< " |_|_|_|_|_| |_| |_|_|_ |_| |_|_ " << endl
<< " |_| |_| |_| |_| |_| |_|_|_| |_|_| " << endl
<< " |_| |_| |_| |_| |_|_| |_|_ _ " << endl
<< " |_| |_| |_| |_| |_| |_|_|_| " << endl
<< " _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ " << endl
<< " _|_|_| |_| |_| |_|_|_| |_|_|_| |_|_|_ |_|_|_| |_|_|_ " << endl
<< " |_|_ |_| _ |_| |_|_ |_|_ |_|_|_| |_|_ |_|_|_| " << endl
<< " |_|_ |_|_|_|_|_| |_|_| |_|_| |_|_| |_|_| |_|_|_ " << endl
<< " _ _|_| |_|_| |_|_| |_|_ _ |_|_ _ |_| |_|_ _ |_| |_|_ " << endl
<< " |_|_| |_| |_| |_|_|_| |_|_|_| |_| |_|_|_| |_| |_| " << endl;
cout << "\n Welcome to Psuedo Mine Sweeper!!\n\n\n\n";
cout << " <S>TART"
<< "\n\n <E>XIT\n\n";
cout << "\t\t\tPlease enter a valid menu option: ";
getline(cin,input);
input[0] = toupper(input[0]);
}while(input[0] != 'S' && input[0] != 'E' || input.length() != 1 || cin.peek() != '\n');
if (input[0] == 'S')
exitVar = true;
else
exitVar = false;
return exitVar;
}
我不是太調試斷言值經歷。我嘗試運行菜單單機版
嘗試在getline後打印輸入,您可能會看到問題! – Mario