标识符"n"未定义
identifier 'n' is undefined
这是我的代码
#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":未声明的标识符
请任何人给我建议。提前谢谢。
一开始你必须声明变量:
int array[100], beg, mid, end, i, num, n;
N是唯一不声明但使用的变量。
错误引发,因为您在声明之前使用了变量n。所以首先像其他mid,num,i
等一样声明变量n
更改您的声明行,如下所示
int array[100], beg, mid, end, i, num ,n; // declare n here before using
cout << "Enter the value of an array" << endl;
cin >> n;
将n
声明为int
,您就可以开始了(仅适用于此错误):
int array[100], beg, mid, end, i, num, n;
相关文章:
- Visual Studio C++ PlaySound 标识符未定义
- 删除动态数组时未定义标识符
- 未定义的标识符/未声明
- 标识符Class_name未定义C++ oop
- 标识符"hWnd"未定义
- 为什么更改包含 psapi.h 的顺序会产生编译错误?(标识符 BOOL 未定义)
- Visual Studio 2019 概念标识符未定义
- 标识符"DDRB"未定义 - VS code / Visual Studio
- 在头文件中使用opencv类型来实现未定义的标识符
- 标识符"in"未定义
- 智能感知:标识符"node"未定义
- 标识符"__readgsqword"未定义 - Visual Studio 2017
- 标识符"V 未定义
- 标识符"thread"未定义
- 标识符uint8_t未定义 - Visual Studio 2017
- 未定义的标识符 - 缺少哪些头文件?- DX12.
- 调用函数时未定义标识符"function_name"
- Wininet未定义标识符
- 简单代码问题-未定义标识符
- 试图编写函数的未定义标识符