C++ - "My_Array"未在此范围内声明 - 简易程序
C++ - "My_Array" is not declared in this scope - Easy Program
所以我正在编写一个非常基本的代码,它应该遍历一个数组(尽管我实现这个的效率很低),并查看数组中的任何两个元素是否等于第三个预定义的和。然而,我一直得到一个"未在此范围内声明"的问题,我不知道为什么,因为这都在同一个函数中。
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
int n=0, sum = 0, key = 1, counter = 0;
cout << "Please enter the number of variables you wish to compare:" <<" ";
cin >> n ;
if (n < 2)
{
do
{
cout<<"Error! -- you must enter 2 or more variables to continue!"<< endl<< "Please enter the number of variables you wish to compare:" <<" ";
cin >> n;
} while (n <2);
}
else
{
int My_Array[n];
cout << "Please Enter "<< n <<" numbers" <<endl;
for (int i=0; i<n; i++)
{
cin >> My_Array[i];
cout << "You've entered"<<" "<<i+1<<" numbers"<<endl<<endl;
}
}
cout << "Please enter the number to which you believe the addens are present in the previously entered number(s):"<<" ";
cin >> sum;
for (int i=0; i<n; i++)
{
key = sum - My_Array[i];
for (int j = 0; j<n; j++)
{
if(key - My_Array[j] == 0)
{
counter++;
}
}
}
cout <<"There are "<< counter << " numbers that equal "<< sum<<" in this array";
}
您在else
块中声明了My_Array
。一旦控制从该块中传递出去,就堆栈而言,它不再存在,您可以不引用它。
相关文章:
- 为什么在全局范围内使用"extern int a"似乎不行?
- 错误:未在此范围内声明'reverse'
- 内联程序集printf将整数解释为地址
- 并行用于C++17中数组索引范围内的循环
- 求出有多少个数字是完美平方,而sqrt()是L,R范围内的素数
- 不计算一个范围内的完美数
- 错误:"imread"未在此范围内声明
- 我在范围内未声明的错误类有问题
- 如何在cpp中使用地图显示给定日期范围内(在下面的问题中)的费率?
- 我有一个数组,我想输入一个范围,然后找到范围内所有偶数的总和?
- 未在此范围内声明错误 'xy'
- 全局范围内的对象会导致程序退出时崩溃
- 以下程序中最大 int 1000 000(在 int 范围内)的整数溢出
- 编写一个程序,该程序在0-99范围内生成10000个随机整数,并从随机数据中产生直方图
- 内联程序集分支目标超出范围
- RSA程序只对特定范围内的数字进行加密和解密
- 编写程序生成1000个0-49范围内的随机整数.(c++)
- 这个程序出了什么问题?说a和b不在范围内
- 当C++试图访问向量的元素(在范围内)时,Qt Quick Windows应用程序崩溃
- C++ - "My_Array"未在此范围内声明 - 简易程序