I was trying to write up a simple code to try and solve matrices and came across an error. Obviously, I am just doing something stupid, so I was hoping you could come to the rescue! Whenever I run the code and input the values, it returns 'x' was not declared in this scope. Any ideas?
#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;
}
This here
if (R1C1 > 1)
int x = R1C1 * (1/R1C1);
cout << x;
Basically means this:
if (R1C1 > 1){
int x = R1C1 * (1/R1C1);
}
cout << x;
As you can see, the x
isn't in the scope anymore when you print it. Its scope is limited to the body of the if
expression. To make it less confusing, people would usually indent the int x...
line so it is clear that it belongs to the if
statement and isn't in the same scope as the cout
line.
Maybe you wanted to do this instead:
if (R1C1 > 1){
int x = R1C1 * (1/R1C1);
cout << x;
}