2017-04-18 84 views
-7

這是我代碼標識「N」是未定義

#include "stdafx.h" 
#include <iostream> 

using namespace std; 

int main() 
{ 
int array[100], beg, mid, end, i, num; 

cout << "Enter the value of an array" << endl; 
cin >> n; 

cout << "Enter the values in sorted order (asc or desc)" << endl; 

for (i = 0; i < n; i++) 
{ 
    cin >> array[i]; 

} 

beg = 0; 
end = n - 1; 
cout << "Enter the value to searched in an array" << endl; 
cin >> num; 

while (beg <= end) 
{ 
    mid = (beg + end)/2; 
    if (array[mid] == num) 
    { 
     cout << "Item found at this position" << (mid + 1); 
     exit(0); 
    } 
    else if (num > array[mid]) 
    { 
     beg = mid + 1; 

    } 
    else if (num < array[mid]) 
     end = mid - 1; 
} 
cout << "Number not found." << endl; 

return 0; 
} 

我不是能找到什麼是我的錯誤。 它總是顯示

標識 'N' 是未定義 'N':未聲明的標識符

請任何人給的建議我。提前致謝。

+1

declare n。像int n。在cin >> n; –

+1

難道你不覺得你需要像'beg,mid,end,i,num'那樣將'n'聲明爲int嗎?我的意思是這個錯誤是非常確定的。 – iqstatic

+0

將n聲明爲int – Abi

回答

0

開始時您必須聲明變量:

int array[100], beg, mid, end, i, num, n; 

是您不聲明但使用的唯一變量。

0

由於在聲明之前使用變量n而引發錯誤。所以先聲明變量n像其他mid,num,i

改變你的宣言如下

int array[100], beg, mid, end, i, num ,n; // declare n here before using 

cout << "Enter the value of an array" << endl; 
cin >> n; 
0

聲明nint,你是好去(僅適用於這個bug):

int array[100], beg, mid, end, i, num, n;