2017-10-11 97 views
0

我正在編寫一個C++程序來搜索數組中的給定整數,但是,當我嘗試調試程序時,visual studio(我正在使用vs 2015 pro版本)抱怨調試斷言失敗: enter image description here調試斷言失敗!表達式:result_pointer!= nullptr

這是我的代碼,它的相當簡單:

int main() { 
int searchArray[10] = { 324,4567,6789,5421345,7,65,8965,12,342,485 }; 
//use searchKey for the number to be found 
//use location for the array index of the found value 
int searchKey, location; 

//write code to determine if integers entered by 
//the user are in searchArray 
//initiate searchKey and location 
searchKey = 0; 
location = 0; 
int n = sizeof(searchArray)/sizeof(searchArray[0]); 
//let user define the search key, give -1 to quit 
while (true) 
{ 
    std::cout << "Enter an integer ('-1') to quit: "; 
    scanf_s("%d", searchKey); 
    std::cout << searchKey << "\n"; 
    if (searchKey == -1) 
    { 
     break; 
    } 
    for (location; location < n; location++) 
    { 
     if (searchArray[location] == searchKey) 
     { 
      break; 
     } 
     location = -1; 
    } 
    if (location != -1) 
    { 
     std::cout << searchKey << " is at location " << location << " in the array.\n"; 
    } 
    else 
    { 
     std::cout << searchKey << " is not in the array.\n"; 
    } 
} 
return 0; 
} 
+0

你解決了這個問題嗎?如果您在代碼行中的「searchKey」之前添加「&」,結果如何? –

回答

0

每個參數必須是一個指針,指向對應于格式的類型說明符的類型的變量。

只需更改代碼 「scanf_s(」 %d 「searchKey)」 到:

scanf_s("%d", &searchKey); 

這將很好地工作。