Click to See Complete Forum and Search --> : print line in reverse using stack
ptg
May 12th, 2008, 05:58 PM
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?
#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;
}
Lindley
May 12th, 2008, 06:08 PM
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.
ptg
May 12th, 2008, 06:39 PM
do you mean for me to use something like cin>>word instead?
Plasmator
May 12th, 2008, 07:00 PM
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:
std::istringstream converter(userInput);
std::stack<std::string> words;
std::string currentWord;
while(converter >> currentWord)
{
words.push(currentWord);
}
Lindley
May 12th, 2008, 07:23 PM
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.
ptg
May 12th, 2008, 07:30 PM
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.
angelorohit
May 12th, 2008, 07:59 PM
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.
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);
}
}
JohnW@Wessex
May 13th, 2008, 04:14 AM
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?
ptg
May 13th, 2008, 01:03 PM
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.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.