-1

As the title suggest how can i declare a variable inside loop and use it outside?

EXAMPLE

void Function(String s)
{
    for(i = 0; s[i] != '\0'; ++i)
    {
        switch(s[i])
        {
            case 'i':int x;
            case 'd':double x;
            case 'c':char x;
        }//end of switch
    }//end of for loop

    //now i want to use 'x' here,i.e.,out side the loop how will i do it?

}//end of void function

EDIT I know the scope thing but i saw some one achieving this with template class/function but I dont know how to use template class/func to do this..so anyone knows?

jww
  • 97,681
  • 90
  • 411
  • 885
Zeus
  • 1,235
  • 12
  • 20
  • 2
    It is quite simple: you can't. – juanchopanza Dec 07 '14 at 10:53
  • ...so define it outside of the loop. And if you want to save memory by not defining the types you won´t need, use a union. The switch only sets some type-flag what to use... – deviantfan Dec 07 '14 at 10:55
  • OK i know the scope thing... Thats why i asked this question ._. but i see somewhere that some one did it using template class/func but idk how to use template class/func to achieve this so anyone knows? – Zeus Dec 07 '14 at 11:02
  • @Zeus Maybe show us the code after the loop and we can suggest an alternative option. The answer could very well involve templates but without seeing how you *use* `x` we can't know. – cdhowie Dec 07 '14 at 11:03
  • read the edit i made pls – Zeus Dec 07 '14 at 11:07
  • @Zeus - Your comments about your last question are irrelevant for this question. Grind you axes elsewhere. – jww Dec 07 '14 at 11:07
  • @Zues - this should cause a compiler error: `case 'i':int x;`. How does that code even compile? – jww Dec 07 '14 at 11:07
  • Well, you can do it by implementing your own compiler (one which violates the C++ language standard). – barak manos Dec 07 '14 at 11:09
  • @jww I see no reason that line wouldn't compile. However, when it runs into `case 'd':double x;` it should complain that `x` already exists. – cdhowie Dec 07 '14 at 11:09
  • @Zeus Your edit still doesn't show us the code you want to write after the loop. I need to see that in order to help you turn it into a template. – cdhowie Dec 07 '14 at 11:10
  • 1
    @cdhowdie - see [Why can't variables be declared in a switch statement?](http://stackoverflow.com/q/92396/608639) – jww Dec 07 '14 at 11:11
  • -jww No need to be rude.. @cdhowie i will edit topic in few mins – Zeus Dec 07 '14 at 11:15
  • 1
    @Zeus - I don't quite follow you. What are you talking about? – jww Dec 07 '14 at 11:18
  • this is probably a x-y problem, but have a look at boost::any – sp2danny Dec 07 '14 at 11:22
  • So, basically, you want ONE variable name to represent different types? Generally, C and C++ has a strict 1:1 relationship between type and name, you can't "change" that based on control flow in the code. The solution is to use for example a `union` or some other type of wrapping the type you actually want inside something. – Mats Petersson Dec 07 '14 at 11:48
  • OK Problem solved i achieved the thing i was trying to do with std::map with struct, a big THANKS to all those tried to help @cdhowie Thnx, u asked for code so i began to write the program and while doing it i faced another problem so googled it and came across std::map with struct which solved my prob.. – Zeus Dec 07 '14 at 11:48

2 Answers2

0

{ } - are scope delimiters, anything you define inside it, cannot be access outside. So if you want, you can declare it outside loop and then you can access it outside as well as inside loop.

Pranit Kothari
  • 9,721
  • 10
  • 61
  • 137
0

No. Simplified, that's what a scope is - the scope within which declared variables are accessible.

You need to declare the variable in the outer scope if you want to access it from there:

{
   int x = 0;
   for (...)
   {
       x= 1;
   }
   if (x ==1)
   {
       printf("it works");
   }
}
Peter
  • 5,608
  • 1
  • 24
  • 43