C++ 调试在计算机上运行正常但在SPOJ上运行不正常的代码

C++ 调试在计算机上运行正常但在SPOJ上运行不正常的代码,c++,debugging,segmentation-fault,C++,Debugging,Segmentation Fault,这段代码在我的计算机上运行良好,但SPJ告诉我它有一个分段错误。你能帮我调试一下吗 #include <iostream> using namespace std; int main() { int i, arr[5]; int j = 0; for (i = 0; i<5; i++) { cout << "Enter number between 0 to 99 \n"; cin >>

这段代码在我的计算机上运行良好,但SPJ告诉我它有一个分段错误。你能帮我调试一下吗

#include <iostream> 
using namespace std;
int main() {
    int i, arr[5];
    int j = 0;
    for (i = 0; i<5; i++) 
    {
        cout << "Enter number between 0 to 99 \n";
        cin >> arr[i]        
    }

    while (arr[j] != 42) { 
        cout << arr[j] << "\n";
        j++;
    }
    return 0;
}
#包括
使用名称空间std;
int main(){
int i,arr[5];
int j=0;
对于(i=0;i arr[i]
}
而(arr[j]!=42){
cout您的循环:

while (arr[j] != 42) { 
  cout << arr[j] << "\n";
  j++;
}

仅仅因为你为类似SPOJ的东西编写了代码并不意味着你不应该正确格式化它。至于问题,想想如果数组中没有值
42
,会发生什么。
while (j<5 && arr[j] != 42) { 
  cout << arr[j] << "\n";
  j++;
}