[英]Segmentation fault 11: while trying to input B[1] using cin
I'm having a problem in running the following code. 我在运行以下代码时遇到问题。 It is giving me a segmentation fault as a runtime error. 这给了我一个运行时错误的分段错误。
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter n: ";
cin >> n;
float A[n][n], x[n], B[n], L[n][n], U[n][n], m[n][n], Aug[n][n + 1];
//Initializing matrix A,L,U
cout << "Enter A: \n";
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> A[i][j];
U[i][j] = A[i][j];
Aug[i][j] = A[i][j];
if (i == j) {
L[i][j] = 1;
}
else {
L[i][j] = 0;
}
}
}
//Initialising matrix B
cout << "Enter B: \n";
for (int i = 0; i < n; i++) {
cin >> B[i];
cout << "done" << i;
Aug[i][n] = B[i];
}
// ...
return 0;
}
Input: 输入:
n=2, A={2,5,-3,-4}, B={0,0} n = 2,A = {2,5,-3,-4},B = {0,0}
The error occurs when I try to input B[1]
so done0 gets printed however done1 doesn't. 当我尝试输入B[1]
时会发生错误,所以done0被打印出来了,但done1没有。 I just can't figure out what the reason of this error is as I don't see any reason for B[1]
to be inaccessible. 我只是无法弄清楚此错误的原因是什么,因为我看不到B[1]
无法访问的任何原因。
You've misdiagnosed the problem. 您误诊了问题。 The segmentation fault occurs after the for
loop finishes, in the code you haven't shown us (the // ...
part). 在for
循环结束之后,在未显示给我们的代码中( // ...
部分),发生了分段错误。 The done1
doesn't get printed because your code faults before it has a chance to flush the output buffer. 因为您的代码在有机会刷新输出缓冲区之前发生了错误,所以done1
不会被打印。 Your cout << "done" << i;
您的cout << "done" << i;
just puts things in the output buffer, there's nothing there to flush the buffer. 只是将东西放在输出缓冲区中,就没有东西可以冲洗缓冲区了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.