“'x' 未在此范围内声明”的问题
Issues with "'x' was not declared in this scope"
我试图编写一个简单的代码来尝试求解矩阵,但遇到了错误。很明显,我只是在做一些愚蠢的事情,所以我希望你能来救援!每当我 运行 代码并输入值时,它 returns 'x' 未在此范围内声明。有什么想法吗?
#include <iostream>
using namespace std;
int main() {
// Row 1
cout << "Please input the first row, first column (R1:C1)" << endl;
int R1C1;
cin >> R1C1;
cout << "Please input the first row, second column (R1:C2)" << endl;
int R1C2;
cin >> R1C2;
cout << "Please input the first row, third column (R1:C3)" << endl;
int R1C3;
cin >> R1C3;
cout << "Please input the first row, fourth column (R1:C4)" << endl;
int R1C4;
cin >> R1C4;
// Row 2
cout << "Please input the second row, first column (R2:C1)" << endl;
int R2C1;
cin >> R2C1;
cout << "Please input the second row, second column (R2:C2)" << endl;
int R2C2;
cin >> R2C2;
cout << "Please input the second row, third column (R2:C3)" << endl;
int R2C3;
cin >> R2C3;
cout << "Please input the second row, fourth column (R2:C4)" << endl;
int R2C4;
cin >> R2C4;
// Row 3
cout << "Please input the third row, first column (R3:C1)" << endl;
int R3C1;
cin >> R3C1;
cout << "Please input the third row, second column (R3:C2)" << endl;
int R3C2;
cin >> R3C2;
cout << "Please input the third row, third column (R3:C3)" << endl;
int R3C3;
cin >> R3C3;
cout << "Please input the third row, fourth column (R2:C4)" << endl;
int R3C4;
cin >> R3C4;
if (R1C1 > 1)
int x = R1C1 * (1/R1C1);
cout << x;
return 0;
}
这里是
if (R1C1 > 1)
int x = R1C1 * (1/R1C1);
cout << x;
基本上是这个意思:
if (R1C1 > 1){
int x = R1C1 * (1/R1C1);
}
cout << x;
如您所见,打印时 x
已不在范围内。它的范围仅限于 if
表达式的主体。为了减少混淆,人们通常会缩进 int x...
行,这样很明显它属于 if
语句并且与 cout
行不在同一范围内。
也许您想这样做:
if (R1C1 > 1){
int x = R1C1 * (1/R1C1);
cout << x;
}
我试图编写一个简单的代码来尝试求解矩阵,但遇到了错误。很明显,我只是在做一些愚蠢的事情,所以我希望你能来救援!每当我 运行 代码并输入值时,它 returns 'x' 未在此范围内声明。有什么想法吗?
#include <iostream>
using namespace std;
int main() {
// Row 1
cout << "Please input the first row, first column (R1:C1)" << endl;
int R1C1;
cin >> R1C1;
cout << "Please input the first row, second column (R1:C2)" << endl;
int R1C2;
cin >> R1C2;
cout << "Please input the first row, third column (R1:C3)" << endl;
int R1C3;
cin >> R1C3;
cout << "Please input the first row, fourth column (R1:C4)" << endl;
int R1C4;
cin >> R1C4;
// Row 2
cout << "Please input the second row, first column (R2:C1)" << endl;
int R2C1;
cin >> R2C1;
cout << "Please input the second row, second column (R2:C2)" << endl;
int R2C2;
cin >> R2C2;
cout << "Please input the second row, third column (R2:C3)" << endl;
int R2C3;
cin >> R2C3;
cout << "Please input the second row, fourth column (R2:C4)" << endl;
int R2C4;
cin >> R2C4;
// Row 3
cout << "Please input the third row, first column (R3:C1)" << endl;
int R3C1;
cin >> R3C1;
cout << "Please input the third row, second column (R3:C2)" << endl;
int R3C2;
cin >> R3C2;
cout << "Please input the third row, third column (R3:C3)" << endl;
int R3C3;
cin >> R3C3;
cout << "Please input the third row, fourth column (R2:C4)" << endl;
int R3C4;
cin >> R3C4;
if (R1C1 > 1)
int x = R1C1 * (1/R1C1);
cout << x;
return 0;
}
这里是
if (R1C1 > 1)
int x = R1C1 * (1/R1C1);
cout << x;
基本上是这个意思:
if (R1C1 > 1){
int x = R1C1 * (1/R1C1);
}
cout << x;
如您所见,打印时 x
已不在范围内。它的范围仅限于 if
表达式的主体。为了减少混淆,人们通常会缩进 int x...
行,这样很明显它属于 if
语句并且与 cout
行不在同一范围内。
也许您想这样做:
if (R1C1 > 1){
int x = R1C1 * (1/R1C1);
cout << x;
}