Thread: local variables

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    57

    local variables

    if i declare a variable in a while statement, can that variable only be used by the whiel statement? i tried doing that and then when i try to access the variable later in the program it says that it is not declared. is there any way to use a variable in a while statement and then still be able to access it outside of it?

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    declare the variable outside of the while.
    Code:
    int variable = 0;
    bool done = false;
    
    while(!done) {
      variable++;
      if(variable >5)
        done = true;
    }
    ?

  3. #3
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    if i declare a variable in a while statement, can that variable only be used by the whiel statement?
    Yep, the variables scope is restricted to the while loop and any nested blocks inside the loop.
    is there any way to use a variable in a while statement and then still be able to access it outside of it?
    If you need a variable outside of a loop, declare it outside of the loop.
    p.s. What the alphabet would look like without q and r.

  4. #4
    jasondoucette.com JasonD's Avatar
    Join Date
    Mar 2003
    Posts
    278

    Re: local variables

    Originally posted by Gil22
    if i declare a variable in a while statement, can that variable only be used by the whiel statement?
    Yes, ANSI standard states that this is the case, and it logically makes sense. However, certain versions of MSVC++ allow the variable to be accessed after the loop - it is best to avoid this, since it is NOT part of the standard and your code may not compile on other compilers, or even on later versions of MSVC.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    57
    ok heres another question then.......i declared it outside of the variable but if i try to output it after the while statement then just junk comes out. its an array by the way, it outputs fine while i'm still inside of the loop but doesnt outside of it. any thoughts?

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    if it is an array, then you should output it through each individual element anyways. were you trying something like this?
    Code:
    int array[5];
    
    std::cout << array;

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    57
    yea thats exactly what i was doing......cant you do that with an array and it will output what you put in it?

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Originally posted by Gil22
    yea thats exactly what i was doing......cant you do that with an array and it will output what you put in it?
    i don't believe so.

    you'll need to loop through the array and output each individual element.

  9. #9
    Registered User
    Join Date
    Feb 2003
    Posts
    28
    so you want the proggy to output every value in the array or what? for that i suggest doing
    Code:
      int array[5];
    for (int i=0;i<5;i++) 
      cout << w[i];
    hope i helped some
    the early bird gets the worm but the second mouse gets the cheese

  10. #10
    Registered User
    Join Date
    Feb 2003
    Posts
    57
    it doesnt work for some reason.....here a small example of my code so you guys can see what i'm trying to do
    Code:
    while(top2 != NULL)
    	{
    		int i = 0;
    	    y[i] = top2->data;
    		cout << y[i];
    		listptr temp5;
    		temp5 = top2;
    		top2 = top2->link;
    		delete temp5;
    		i++;
    	}
    	char x[250];
    	cout << "enter a pattern";
    	cin >> x;
    	cout << y[1];

  11. #11
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    what part doesn't work? the while?

    how are you inputting x?

  12. #12
    Registered User
    Join Date
    Feb 2003
    Posts
    57
    x works fine......its the cout << y[1] that screws up, it should output a simple character but it outputs a piece of junk instead. when i test the filling of the array inside the loop it works fine but when i try and do the same thing outside it doesnt work.

  13. #13
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    its in your while.

    try declaring int i = 0; outside of the while statement, as well as listptr temp5;

    then just increment i in your while, and assign temp5 like you are.

    the reason is that a new i gets declared with the value of 0 every time you start the while, and just overwrites y[0] everytime. and you are creating a new temp5 everytime you run through the while, whereforeas, you can save some memory and time through delcaring it outside as one temp5 is declared.

  14. #14
    Registered User
    Join Date
    Feb 2003
    Posts
    57
    lol........i've been making some stupid mistakes lately. thats def it, thanks for helping me catch that.

  15. #15
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Originally posted by Gil22
    lol........i've been making some stupid mistakes lately. thats def it, thanks for helping me catch that.
    no problem. i've done that too. kindof a hard catch at first, until ya start thinking about it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Father and Son Variables
    By khdani in forum Linux Programming
    Replies: 3
    Last Post: 11-28-2008, 06:42 PM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. how to access local variables outside loops?
    By cdkiller in forum C++ Programming
    Replies: 3
    Last Post: 09-28-2006, 06:48 PM
  4. regarding variables.
    By kiranck007 in forum C Programming
    Replies: 2
    Last Post: 01-24-2006, 07:30 AM
  5. global and local variables
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2001, 01:17 PM