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;
}
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.
Re: print line in reverse using stack
do you mean for me to use something like cin>>word instead?
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);
}
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.
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.
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);
}
}
Re: print line in reverse using stack
Quote:
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?
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.