I am a beginner at c++ and I want to ask about this specific error. I don't know why it always appears that I didn't declare the variable wherein in fact, I did (see my code below).
Asked
Active
Viewed 1,015 times
-2
-
Add complete code as text, refer [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – TruthSeeker Feb 05 '20 at 07:04
-
Not an image. Just post the code as text. You can get downvotes just for not doing so. – mfnx Feb 05 '20 at 07:47
-
It is very rare to see so many wrong things in one SO question. – Chef Gladiator Feb 05 '20 at 08:45
-
Welcome to Stack Overflow! Your image of text [isn't very helpful](//meta.unix.stackexchange.com/q/4086). It can't be read aloud or copied into an editor, and it doesn't index very well, meaning that other users with the same problem are less likely to find the answer here. Please [edit] your post to incorporate the relevant text directly (preferably using copy+paste to avoid transcription errors). – Toby Speight Feb 05 '20 at 13:19
3 Answers
0
You initiated the decoder array in both cases in an if statement. So the compiler does not know it outside of the if. Write the variable before your first if-else statement and than change it, if needed, within the if statement.

palme
- 2,499
- 2
- 21
- 38
0
The problem is in the array declaration, it is declared inside of a scope:
#include <iostream>
using namespace std;
int main()
{
int a = 2; // local variable
if (true)
{
int a = 4; // local if statement variable
cout << a << endl; // here the output will be 4
}
cout << a << endl; // here the output will be 2, also here 'a' from the if statement, does not longer exist, when the if statement is ended all the variables declared inside will be deleted
return 0;
}
You can also create your own scope with out 'if' statement:
int a = 2; // local variable
{
int a = 4; // local scope statement variable
cout << a << endl; // here the output will be 4
}
cout << a << endl; // here the output will be 2, also 'a' from the previous scope does not exist

Kiseea
- 53
- 7
0
Here is a useful link to learn about local variables and scope.
Your particular problem is that your variable decoder
is local to the block scope (you declared it within a if
statement), and you try to use it outside that scope. However, once exited the scope, all variables local to that scope are destroyed.
if (number = 987)
{
int decoder[3] = {0, 1, 2};
} // and here decoder is destroyed
You could declare decoder
outside the block and define it inside the block:
int decoder[3];
if (number = 987)
{
// populate decoder
}
// decoder still exists

mfnx
- 2,894
- 1
- 12
- 28