Identifier undefined- Please help.

I want to make a summary of the program I've made. And I need to call for the variable/identifier again to use them at the end of the program. But when I run the program, there is an error that said that the identifier was undefined. The variable I am calling is from a switch statement, how can I properly have the review using the variables?
The variable must no longer be in scope.

The lifetime of a variable is determined by the {braces} it sits in.

1
2
3
4
5
6
7
void func()
{
  {
    int foo = 5;
  }  // foo leaves scope here
  cout << foo;  // error, foo no longer exists
}


If you want your variable to be visible in a broader scope, you'll need to move the declaration of it to that scope:

1
2
3
4
5
6
7
8
void func()
{
  int foo = 5;
  {
    //...
  }
  cout << foo;  // now this is OK, foo exists in this scope
}




If your variable is in a separate function, your best bet is to pass it to the new function as a parameter.
Topic archived. No new replies allowed.