Проблемы с "x" не были объявлены в этой области "

Я пытался написать простой код, чтобы попытаться решить матрицы, и натолкнулся на ошибку. Очевидно, я просто делаю что-то глупое, поэтому я надеялся, что вы можете прийти на помощь! Всякий раз, когда я запускаю код и ввожу значения, он возвращает '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;
}

1 ответ

Решение

Это здесь

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;
}
Другие вопросы по тегам