Newbie stack question

This is a discussion on Newbie stack question within the C++ Programming forums, part of the General Programming Boards category; Hi, can u help me with this code below Code: #pragma warning(disable:4786) #include<iostream> #include<string> #include<stack> using namespace std; void displayStack( ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    18

    Newbie stack question

    Hi, can u help me with this code below
    Code:
    #pragma warning(disable:4786)
    #include<iostream>
    #include<string>
    #include<stack>
    using namespace std;
    
    void displayStack( std::stack<string> aStack ) //by value
    	{
    		for( ; !aStack.empty(); aStack.pop() )
    		{
    			std::cout<<"Expression entered:"<<aStack.top()<<"\n";
    		}	
    	}
    
    typedef stack<string> stackMain;
    
    int main()
    {
    	stackMain stack1;
    	std::string str;
    
    	//add item into stack 1
    	cout<<"Insert any an expression: ";
    	cin>>str;
    	stack1.push(str);
    	displayStack(stack1);
    	return 0;
    
    }

    when i entered an expression such as 1 + 2, the output will be 1 + 2 but the stack size is 1. I expected the stack size to be 3. are there ways to make the stack size into 3 ??

  2. #2
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    1+2 is a single string?
    Have tried to split it into 3 and push seperatly?

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    when i entered an expression such as 1 + 2, the output will be 1 + 2
    Not possible. If you entered 1 + 2, the output would be 1. 'cin>>' stops reading when it encounters whitespace.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Quote Originally Posted by blackmisery
    are there ways to make the stack size into 3 ??
    Probably one of the better ways:

    1. Use getline to read in the entire line of input from the user.
    2. Initialize a stringstream using the input from above.
    3. Use a loop to extract individual tokens from the above stringstream and push them onto the stack.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Or just put the cin >> str and push(str) into a loop and separate each token with a space.

  6. #6
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    Sorry I did'nt see the space b/w the 1 the + and the 2.
    ^^!

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    18
    Code:
    #pragma warning(disable:4786)
    #include<iostream>
    #include<string>
    #include<stack>
    using namespace std;
    
    void displayStack( std::stack<char> aStack ) //by value
    	{
    		for( ; !aStack.empty(); aStack.pop() )
    		{
    			std::cout<<"Expression entered: "<<aStack.top()<<"\n";	
    		}	
    	}
    
    typedef stack<char> stackMain;
    
    
    int main()
    {
    
    	stackMain stack1;
    	string str;
    	int ctr=-1;
    
    
    	//add item into stack 1
    	cout<<"Insert any an expression: ";
    	cin>>str;
    	
    	while((++ctr)<str.length())
    	{
    		stack1.push(str[ctr]);
    	}
    	
    	displayStack(stack1);
    	cout<<"Stack size: "<<stack1.size()<<"\n";
    	return 0;
    
    }
    I changed my code it i works fine.but i have a doubt in this part

    Code:
    while((++ctr)<str.length())
    	{
    		stack1.push(str[ctr]);
    	}
    what does this part do ?? i found it on an article

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by blackmisery
    I changed my code it i works fine.but i have a doubt in this part

    Code:
    while((++ctr)<str.length())
    	{
    		stack1.push(str[ctr]);
    	}
    what does this part do ?? i found it on an article
    you could rewrite the while loop this way (Easier to understand IMHO)
    Code:
    int ctr=0;
    while( ctr < str.length() )
    {
       stack1.push(str[ctr] );
       ++ctr;
    }
    Your version works because 'ctr' is initialised to the value of -1 - then when the while loop is evaluated on the first run, ctr is incremented to 0. Personally, I wouldn't do that, it's very un-idiomatic to say the least.

    Also, you may prefer str.at(ctr) to str[ctr]. The square brackets are not range checked, wheras if you attempt to access an invalid part of the string with at(), you will get an out_of_range error (This is preferable for debugging since it identifies the problem immediately).

  9. #9
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203

    Exclamation

    Quote Originally Posted by blackmisery
    Code:
    #pragma warning(disable:4786)
    #include<iostream>
    #include<string>
    #include<stack>
    using namespace std;
    
    void displayStack( std::stack<char> aStack ) //by value
    	{
    		for( ; !aStack.empty(); aStack.pop() )
    		{
    			std::cout<<"Expression entered: "<<aStack.top()<<"\n";	
    		}	
    	}
    
    typedef stack<char> stackMain;
    
    
    int main()
    {
    
    	stackMain stack1;
    	string str;
    	int ctr=-1;
    
    
    	//add item into stack 1
    	cout<<"Insert any an expression: ";
    	cin>>str;
    	
    	while((++ctr)<str.length())
    	{
    		stack1.push(str[ctr]);
    	}
    	
    	displayStack(stack1);
    	cout<<"Stack size: "<<stack1.size()<<"\n";
    	return 0;
    
    }
    I changed my code it i works fine.but i have a doubt in this part

    Code:
    while((++ctr)<str.length())
    	{
    		stack1.push(str[ctr]);
    	}
    what does this part do ?? i found it on an article
    that puses in only a single character at a time? that might not be what you want. try typing in 12 + 25 and see.

  10. #10
    Registered User
    Join Date
    Apr 2005
    Posts
    18
    I was about to ask about the character.can u give me any clue

    I tried to check the parentheses here, but it gave me a debug window when i ran it. Did i maka any mistake or the whole code is wrong

    Code:
    string valid="true";
    	int ctr=0;
    	while( ctr < str.length())
    	{
    		stack1.push(str.at(ctr));
    		++ctr;
    	
    		if(str.at(ctr) == '(' || '[' || '{')
    		{
    			cout<<"Opening Encountered"<<"\n";
    				if(str.at(ctr) == ')' || ']' || '}' )
    				{
    					if(!stack1.empty())
    					valid="false";
    				}
    		}
    			
    	}

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Think about how the loop runs. Your code says: If, in one cycle through the loop, you find an open parenthesis, then check to see if that character is a closed parenthesis. In reality, you should remember that an open was encountered and continue looping until a close is encountered.

    Also, you used the || incorrectly. Look up the correct way to use the ||.

  12. #12
    Registered User
    Join Date
    Apr 2005
    Posts
    18
    I modified my code. Here it is

    Code:
    	int ctr = 0;
    	while (ctr<expression.length())
    	{
    		if (expression.at(ctr)== '(' || expression.at(ctr)== '[' || expression.at(ctr)== '{')
    			numStack.push(expression.at(ctr));
    					
    		else
    			numStack.push(expression.at(ctr));
    		++ctr;
    	}
    It works fine. And i made another code which works similar as the code above

    Code:
    string::size_type i;
    for(i=0; i<expression.length();++i)
    	{
    		next = expression[i];
    		if(next=='('||next=='['||next=='{')
    			numStack.push(next);
    		else
    			numStack.push(next);
    	}
    both code works fine for me. but i'm not very sure which one is better to use. and also what this line means
    Code:
    string::size_type i;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie with Very Newbie Question
    By Jedi_Mediator in forum C++ Programming
    Replies: 18
    Last Post: 07-01-2008, 08:00 AM
  2. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  3. Total Newbie Question
    By Kid A in forum C++ Programming
    Replies: 4
    Last Post: 06-22-2006, 05:36 AM
  4. Question about stack - push - pop
    By The Wiep in forum C++ Programming
    Replies: 6
    Last Post: 12-29-2002, 11:08 AM
  5. Newbie question about registries/files
    By SirDavidGuy in forum C++ Programming
    Replies: 1
    Last Post: 02-20-2002, 08:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21