Thread: intializing variables

  1. #1
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99

    intializing variables

    Here's par of my code:
    Code:
    bool Opener(char ch){
    	 if((ch == '(') || (ch =='[') || (ch =='{'))
    		return true;
    	else 
    		return false;
    	}/*end of opener*/
    
    bool Closer(char ch){
    		if((ch == ')') || (ch == ']') || (ch == '}'))
    			return true;
    		else
    			return false;
    	}/*end of closer*/
    
    int main(){
    	
    	Astack S;
    	string s;
    	unsigned int i;
    
    	cout <<"Enter data:\n";
    	getline(cin, s);
    
    	for(i = 0; i < s.length(); i++){
    		if(Opener())
    			S.push();
    	}
    		for(i = 0; i < s.length(); i++){
    			if(Closer())
    				S.pop();
    			if(S.IsEmpty())
    				cout << "No Openers found";
    		}
    		return 0;
    }
    Right now 4 functions dont take any arguments. If I put in variables then build the solution it gives me a warning stating that my variables are unintialized local variables. What does that mean?

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The compiler actually should be giving you an error about too few arguments to Opener and Closer, which are the two functions I see.

    If you are getting a warning about uninitialized local variables in addition to that, it means that one of the local variables you are using for its value was never assigned a value in the first place. The particular variable should be indicated by the line number of the warning. C++ does not initialize plain old data types (it will initialize user defined types if a default constructor is available) and their value in that case is undefined.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Notice that you shouldn't be putting in brand new variables in your function calls.

    Code:
    Opener(char ch) //NO
    Opener(s[0]) //YES, as s already exists and is a string and therefore s[0] is a char
    (EDIT: I mean when you call them, of course.)

  4. #4
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    The 4 functions are the Opener, Closer, push, and pop. Thats bc I havent put a variable in yet. But when I do it says that they are unintialized local variables. Which Im confused about.
    Here is my whole code so far and Im a add variables so everyone can see :
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    const int DefaultListSize = 50;
    typedef char Elem;
    
    class Astack 
    {
    private:
    	int top;	/*Index for top element*/
    	int size;	/*Maximum size of stack*/
    	Elem*listArray;	/*Array holding stack elements*/
    public:	
    		Astack(int sz =DefaultListSize)	/*Constructor*/
    		{size = sz; top = 0; listArray = new Elem[sz];}
    		~Astack() { delete [] listArray;}	/*Destructor*/
    		void clear() {top = 0;}
    		bool push(const Elem& item){
    			if(top == size) return false;	/* Stack is full*/
    			else {listArray [top++] = item;
    				return true;
    			}
    		}
    		bool pop(Elem& item){	/*Pop top element*/
    			if(top == 0) return false;
    			else {item = listArray[--top]; 
    				return true;
    			}
    		}
    		bool topValue(Elem& item) const {	/*Return top element*/
    			if (top == 0) return false;
    			else {item = listArray[top - 1];
    			return true;
    			}
    		}
    		int length() const {return top;}
    		bool IsEmpty() const {if(top == 0) return true;
    		else return false;
    		}
    	};
    
    	bool Opener(char ch){
    	 if((ch == '(') || (ch =='[') || (ch =='{'))
    		return true;
    	else 
    		return false;
    	}/*end of opener*/
    
    	bool Match(char Lc, char Rc){
    		if(( Lc == '(') && (Rc == ')') || ((Lc == '[') && (Rc == ']')) ||((Lc == '{') && (Rc == ']')))
    			return true;
    		else
    			return false;
    	}/*end of Match*/
    
    	bool Closer(char ch){
    		if((ch == ')') || (ch == ']') || (ch == '}'))
    			return true;
    		else
    			return false;
    	}/*end of closer*/
    
    
    int main(){
    	
    	Astack S;
    	string s;
    	unsigned int i;
                    char a;
                    char b;
    
    	cout <<"Enter data:\n";
    	getline(cin, s);
    
    	for(i = 0; i < s.length(); i++){
    		if(Opener(a))
    			S.push(a);
    	}
    		for(i = 0; i < s.length(); i++){
    			if(Closer(b))
    				S.pop(a);
    			if(S.IsEmpty())
    				cout << "No Openers found";
    		}
    		return 0;
    }

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And why do you think a has a value when you call Opener(a)? HINT: It doesn't. (Well, it has some value, but it's garbage since you didn't put anything in there.)

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    90
    In main, you don't initialize "a" and "b". Then you call "Opener(a)" but "a" is never set. Similarly you call "Closer(b)" but "b" is never set. Hence the warnings.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Accessing Variables
    By vileoxidation in forum C++ Programming
    Replies: 10
    Last Post: 10-05-2009, 07:58 AM
  2. Protected / Private Variables accessable.
    By +Azazel+ in forum C++ Programming
    Replies: 19
    Last Post: 09-08-2009, 07:39 PM
  3. Question about Static variables and functions.
    By RealityFusion in forum C++ Programming
    Replies: 2
    Last Post: 10-14-2005, 02:31 PM
  4. Remotely Creating Variables
    By Rajin in forum C++ Programming
    Replies: 1
    Last Post: 04-26-2005, 11:20 PM
  5. Declaring an variable number of variables
    By Decrypt in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2005, 04:46 PM