CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2008
    Posts
    55

    print line in reverse using stack

    im trying to write a program inputs some words and uses a stack object to print the line in reverse. when i run my program it gives me the word the right way not reversed. for ex: hello there it gives me hello there
    what am i doing wrong?
    Code:
    #include <iostream>
    #include <stack>
    #include <string>
    using namespace std;
    
    int main() {
    
        stack<string> allwords; // stack of all words
        string word;            // input for words.
    
    	cout << "enter some words: ";
        getline(cin, word);
        allwords.push(word);
        
        while (!allwords.empty()) {
           cout << allwords.top() << endl;
           allwords.pop();         
        }
        return 0;
    }

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: print line in reverse using stack

    You're getting an entire line, hence your stack will only ever contain one string.

    You need to parse the line. Easiest way is to use a stringstream object with operator>>.

    Incidentally, what was happening would have become immediately apparent if you'd run the code through a debugger and did a few basic checks.
    Last edited by Lindley; May 12th, 2008 at 06:11 PM.

  3. #3
    Join Date
    Mar 2008
    Posts
    55

    Re: print line in reverse using stack

    do you mean for me to use something like cin>>word instead?

  4. #4
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: print line in reverse using stack

    Quote Originally Posted by ptg
    do you mean for me to use something like cin>>word instead?
    Either that, or like Lindley said, use an std::stringstream on an existing string:
    Code:
    std::istringstream converter(userInput);
    
    std::stack<std::string> words;
    std::string currentWord;
    
    while(converter >> currentWord)
    {
        words.push(currentWord);
    }

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: print line in reverse using stack

    The benefit of the stringstream approach is that it would allow the user to input all words on one line. If you just used cin >> word, you'd need to have a special "list terminator" word to mark the end of input.

  6. #6
    Join Date
    Mar 2008
    Posts
    55

    Re: print line in reverse using stack

    well i havent learned about the stringstream yet in class, so i dont think i should use it. but it does seem like an easier way to do this program. it doesnt have to be professional since im a begginer something decent would work.

  7. #7
    Join Date
    Oct 2006
    Location
    Singapore
    Posts
    346

    Re: print line in reverse using stack

    If you are not keen on using istringstream, you can do another thing. Assuming that words in the string are separated by spaces, you could do something like this.
    Code:
    void PushWords(const std::string line, std::stack<std::string>& wordStack)
    {
    	std::string word;
    	for(std::string::size_type i = 0; i < line.length(); ++i)
    	{
    		if(line[i] != ' ')
    		{
    			word += line[i];
    		}
    		else if(word != "")
    		{
    			wordStack.push(word);
    			word = "";
    		}
    	}
    	if(word != "")
    	{
    		wordStack.push(word);
    	}
    }
    Believe in your Dreams, Work for what you Believe in.
    My thoughts? Angelo's Stuff
    Some fun things I've done: RayWatch, QuickFeed, ACSVParser

    @ngelo

  8. #8
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: print line in reverse using stack

    well i havent learned about the stringstream yet in class, so i dont think i should use it.
    I often see this type of comment from students. Assuming you haven't been told not to use certain constructs why should you not use knowlege gained outside of the classroom? I imagine that this could debated this either way, but it seems strange to me that you should have to pretend that you don't know certain things just because you didn't get taught it in the classroom. Should you get marked down for using knowlege gained by your own initiative?

  9. #9
    Join Date
    Mar 2008
    Posts
    55

    Re: print line in reverse using stack

    Quote Originally Posted by JohnW@Wessex
    I often see this type of comment from students. Assuming you haven't been told not to use certain constructs why should you not use knowlege gained outside of the classroom? I imagine that this could debated this either way, but it seems strange to me that you should have to pretend that you don't know certain things just because you didn't get taught it in the classroom. Should you get marked down for using knowlege gained by your own initiative?
    well i think the teachers prefer for us to learn the basis first instead of using things like that. just like in a math class when u learn something the hardway and then they show you a way to make it easier. it makes u appreciate the new way even more. but i have figured it out how to do this without it, it was easier than i thought.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured