Thread: how to access local variables outside loops?

  1. #1
    Registered User cdkiller's Avatar
    Join Date
    Feb 2006
    Posts
    20

    Question how to access local variables outside loops?

    Hi again to all. I tried to use google before asking dumb questions but i still need to ask one. I just did a lot of reading about pointers and referencing to try help my cause but my efforts were futile. i have a do while loop and within that has a while loop. now part of the condition for the outer do while loop lies within the interior while loop and the outer loop cant see the variables inside the while loop. i will paste part of the code here:

    Code:
    do
    some other code here, bla bla dont want to waste forum space, lol.
    while (pos != string::npos) 
    		{
    			cout << "Found space or dot at: " << pos << endl;
    			pos = name.find_first_of(" .", pos + 1);
    			  
    		}
    	} while ((name_length=0) && (pos !=0));
    that tells me pos undeclared identifier. i noticed that every example i saw of pointers etc used integers and not strings so i do not know what to do.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The inner loop isn't the problem. The variable you use to control the do while loop needs to be declared outside of that loop. You didn't post the declaration of pos, but presumably it is inside the bla bla part. Move it to before the do.

    Don't forget to re-initialize it at its current location if necessary for each iteration of the do while loop.

  3. #3
    Registered User cdkiller's Avatar
    Join Date
    Feb 2006
    Posts
    20
    thanks, that solved my problem. after a while though i realised it was a bad idea to use pos as the condition so instead i put in another var to indicate the number of invalid characters, but the same principle i applied to it by making sure to declare it before the do.

    but i am still curious as to if its possible to use something like pointers so as to not have too many global vars and thus have more efficient code?

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    If I understand you correctly, the variables don't need to be "global" They can be local to the function that you're in. You can also add an extra scope if you wish, eg:
    Code:
    {
      int pos = 0;
      
      do
      {
        //...
      } while(/*...*/);
    
    }
    //pos out of scope
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best way to avoid using global variables
    By Canadian0469 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2008, 12:02 PM
  2. Loops & variables
    By SLM in forum C++ Programming
    Replies: 7
    Last Post: 03-20-2008, 02:43 PM
  3. Headers and Global Variables
    By ajoo in forum C++ Programming
    Replies: 1
    Last Post: 05-15-2004, 04:49 AM
  4. local variables
    By Unregistered in forum C Programming
    Replies: 14
    Last Post: 03-20-2002, 02:55 PM
  5. global and local variables
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2001, 01:17 PM