|
-
May 12th, 2008, 05:58 PM
#1
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;
}
-
May 12th, 2008, 06:08 PM
#2
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.
-
May 12th, 2008, 06:39 PM
#3
Re: print line in reverse using stack
do you mean for me to use something like cin>>word instead?
-
May 12th, 2008, 07:00 PM
#4
Re: print line in reverse using stack
 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);
}
-
May 12th, 2008, 07:23 PM
#5
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.
-
May 12th, 2008, 07:30 PM
#6
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.
-
May 12th, 2008, 07:59 PM
#7
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);
}
}
-
May 13th, 2008, 04:14 AM
#8
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?
-
May 13th, 2008, 01:03 PM
#9
Re: print line in reverse using stack
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|