2014-07-10 91 views
0

我正在嘗試學習編碼,並且正在創建一個二進制搜索程序。當試圖運行我的代碼時,當我嘗試調用搜索函數時,出現調試斷言失敗(請參閱下面的調試輸出)。我搞砸了什麼?調試斷言失敗:字符串下標超出範圍

#include<iostream> 
#include<string> 

using namespace std; 

const int N = 100; 

string names[N]; 
string query; 

//function to search for names in the list 

string search(int largestNum, int smallestNum, string names, string query); 


int main(){ 

    cout << "Please enter the names, pressing enter after each entry." << endl 
     << "Type done when you are finished the list" << endl 
     << "1."; 
    cin >> names[0]; 

    int ticker1 = 1; 

    /*this loop outputs a number 1 greater then the last and allows the 
    user to enter a new name until they type "done" which will exit the loop*/ 
    do { 
     cout << endl << ticker1 + 1 << "."; 
     cin >> names[ticker1]; 
     ticker1++; 
    } while ((names[ticker1 - 1] != "done") && (names[ticker1 - 1] != "Done")); 

    cout << endl; 

    int ticker2 = 0; 

    while (ticker2 < (ticker1 - 1)){ 
     cout << ticker2 + 1 << "." << names[ticker2] << endl; 
     ticker2++; 
    } 

    int largestNum = ticker1; 
    int smallestNum = 0; 

    cout << "Please Enter the name that you would like to search for:"; 
    cin >> query; 

    cout << search(largestNum, smallestNum, names[N], query); 
    return 0; 
} 
//function to search for names in the list 

string search(int largestNum, int smallestNum, string names, string query){ 
    string middleName; 

    int middle = ((largestNum - smallestNum)/2); 

    middleName = names[middle]; 
    cout << middleName; 

    if (query == middleName) return { "Present", "Location:" + middle }; 
    else return { "Not around" }; 
} 

調試輸出

'Learning C++.exe' (Win32): Loaded 'C:\Users\Gareth\Programming\Projects\Learning C++\Debug\Learning C++.exe'. Symbols loaded. 
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Cannot find or open the PDB file. 
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Cannot find or open the PDB file. 
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Cannot find or open the PDB file. 
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcp120d.dll'. Cannot find or open the PDB file. 
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcr120d.dll'. Cannot find or open the PDB file. 
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\user32.dll'. Cannot find or open the PDB file. 
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\gdi32.dll'. Cannot find or open the PDB file. 
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\imm32.dll'. Cannot find or open the PDB file. 
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msctf.dll'. Cannot find or open the PDB file. 
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcrt.dll'. Cannot find or open the PDB file. 
Debug Assertion Failed! 

Program: C:\Windows\SYSTEM32\MSVCP120D.dll 
File: c:\program files (x86)\microsoft visual studio 12.0\vc\include\xstring 
Line: 1685 

Expression: string subscript out of range 

For information on how your program can cause an assertion 
failure, see the Visual C++ documentation on asserts. 

(Press Retry to debug the application) 
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\uxtheme.dll'. Cannot find or open the PDB file. 
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\HsSrv.dll'. Cannot find or open the PDB file. 
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\winmm.dll'. Cannot find or open the PDB file. 
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\dsound.dll'. Cannot find or open the PDB file. 
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\version.dll'. Cannot find or open the PDB file. 
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\advapi32.dll'. Cannot find or open the PDB file. 
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ole32.dll'. Cannot find or open the PDB file. 
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\winmmbase.dll'. Cannot find or open the PDB file. 
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\powrprof.dll'. Cannot find or open the PDB file. 
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sechost.dll'. Cannot find or open the PDB file. 
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\rpcrt4.dll'. Cannot find or open the PDB file. 
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\combase.dll'. Cannot find or open the PDB file. 
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\cfgmgr32.dll'. Cannot find or open the PDB file. 
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\devobj.dll'. Cannot find or open the PDB file. 
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sspicli.dll'. Cannot find or open the PDB file. 
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\cryptbase.dll'. Cannot find or open the PDB file. 
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\bcryptprimitives.dll'. Cannot find or open the PDB file. 
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\dwmapi.dll'. Cannot find or open the PDB file. 
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\clbcatq.dll'. Cannot find or open the PDB file. 
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\oleaut32.dll'. Cannot find or open the PDB file. 
'Learning C++.exe' (Win32): Loaded 'C:\Program Files (x86)\DisplayFusion\Hooks\AppHookx86_45641EBC-D115-4D9A-BB79-9CB07566629C.dll'. Module was built without symbols. 
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\shell32.dll'. Cannot find or open the PDB file. 
'Learning C++.exe' (Win32): Loaded 'C:\Windows\WinSxS\x86_microsoft.windows.common-controls_6595b64144ccf1df_5.82.9200.16658_none_bf1359a245f1cd12\comctl32.dll'. Cannot find or open the PDB file. 
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\shlwapi.dll'. Cannot find or open the PDB file. 
The program '[9200] Learning C++.exe' has exited with code 3 (0x3). 
+1

歡迎堆棧溢出,請注意後預覽窗口。我會現在就爲你解決格式問題,但是請你自己在將來再做一次。另外,你是否已經嘗試過了在調試器中的代碼?哪一行產生錯誤? – Angew

+0

錯誤發生在if(query == middleName)return {「Present」,「Location:」+ middle};「 – sparky2012

+1

你可以使用調試器並逐行執行你的程序來解決這個問題 –

回答

3

看來你想讓你的函數在你的名字數組中找到一個特定的名字(query)(names)。但是這個函數的接口是不正確的。

它的參數names目前是string類型,意思是它是單個字符串。如果你想通過在全局變量names,改變函數接受一個指向string,並使其以便它指向數組的第一個元素:

string search(int largestNum, int smallestNum, const string *names, string query) 
{ 
    // as before..., except: 

    if (query == middleName) return "Present\nLocation:" + std::to_string(middle); 
    else return "Not around"; 
} 

在調用點,就通過names代替names[N](其無論如何訪問外越界指數):

cout << search(largestNum, smallestNum, names, query); 

原始斷言的源是一個初始化std::string{ "Present", "Location: " + middle }甚至不能遠程執行您可能認爲它的功能。它調用std::string構造函數,該構造函數需要兩個迭代器,預期會分隔一系列要複製到字符串中的字符。

那麼你得到有效告訴程序:「從第一個字符的地址開始在字面"Present"直到你到達字面"Location: "middle個字符的地址,在間複製到一切去的字符串。當然,這是一個絕對的混亂。

如果你想要一些學習資源,我們保持list of good C++ books這裏堆棧溢出。

+0

非常感謝!漫長的道路前進我猜:p – sparky2012

-1

你傳入names[N]search(..),但最可能的位置100不存在。 N設置爲100.只需傳遞一個有效的數組位置或傳遞整個數組,並且長度爲names即可遍歷names。此外,您正在使用此行middleName = names[middle];,但names不是字符串數組,只是一個string

+0

那我該如何通過整個陣列? – sparky2012