Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;变量初始化_C++_Variables - Fatal编程技术网

C++ c++;变量初始化

C++ c++;变量初始化,c++,variables,C++,Variables,当我运行代码时,即使数字不存在,它也会输出yes int a[6] = {1,2,3,4,5,6}; int n,x,i=0; cin >> x; n = 6; a[n] = x; while(a[i]!=x) i++; if(i==n) cout << "no" ; else cout << "yes&

当我运行代码时,即使数字不存在,它也会输出yes

    int a[6] = {1,2,3,4,5,6};
    int n,x,i=0;

    cin >> x;
    n = 6;
    a[n] = x;
    

    while(a[i]!=x)
        i++;
        
    if(i==n)
        cout << "no" ;
    else cout << "yes" << i;
inta[6]={1,2,3,4,5,6};
int n,x,i=0;
cin>>x;
n=6;
a[n]=x;
while(a[i]!=x)
i++;
如果(i==n)

如果
n==6
,则cout
a[n]=x
超出范围。对于大小为6的数组,有效索引包括
0
5
。数组没有变大来存储新元素。哦,好吧……谢谢
    int n,x,i=0;
    int a[6] = {1,2,3,4,5,6};
    
    cin >> x;
    n = 6;
    a[n] = x;
    

    while(a[i]!=x)
        i++;
        
    if(i==n)
        cout << "no" ;
    else cout << "yes" << i;