At Thursday 2004-12-02 15:47, you wrote:

>Here you go:
>
>     #include <string>
>     #include <iostream>
>     #include <stack>
>
>     int main(void)
>     {
>         std::string s;
>         std::cout << "Enter the sentence: " << std::flush;
>         std::stack<std::string> ss;
>         do
>         {
>             std::cin >> s;
>             ss.push(s);
>         } while (std::cin.peek() != '\n');
>
>         while (!ss.empty())
>         {
>             std::cout << ss.top() << ' ';
>             ss.pop();
>         }
>         std::cout << std::endl;
>     }


if you dislike writing lots of loops, here's one that heavily uses the 
library (it also allows you to input more than one sentence)
I've commented it rather heavily since many aren't familiar with the 
standard C++ library

// string and getline(istream&, string)
#include <string>
// cin and cout
#include <iostream>
// istringstream
#include <sstream>
// vector
#include <vector>
// istream_iterator and ostream_iterator
#include <iterator>
// copy
#include <algorithm>

using namespace std;

int main()
{
     string line;   ///<someplace for the input

     /// loop while we're getting good data (the \n at the beginning
     /// means we won't have to output one later)
     /// I've split the conditions of the while into 3 lines to make them 
easier to track
     /// the expression (cout << blah) returns cout (which tests true if 
the stream is still good)
     /// we then get an entire line into the variable  line (getline also 
returns the stream)
     /// finally we test to see if we've input anything or just got an 
empty line
     while ((cout << "\nEnter the sentence(just enter to exit): ")
            && getline(cin, line)
            && line.size()
           )
     {
         /// construct an istringstream from the line we just read
         /// remember that the operator >> when reading a string stops on 
whitespace
         /// that is, it will read one word at a time.
         istringstream ist(line);

         /// what follows is called a range constructor (every STL 
container has one)
         /// we create the vector using beginning and ending iterators.
         /// the extra parens around the 1st argument to the constructor 
are required
         /// (see "Effective STL" Item 6)
         ///
         vector<string> the_words((istream_iterator<string>(ist)), 
istream_iterator<string>());

         /// now just copy them out (backwards) to the output (separate 
them by one space)
         /// use reverse limits rbegin() and rend() to do that
         copy(the_words.rbegin(), the_words.rend(), 
ostream_iterator<string>(cout, " "));
     }
}

[deleted]


Victor A. Wagner Jr.      http://rudbek.com
The five most dangerous words in the English language:
               "There oughta be a law" 



------------------------ Yahoo! Groups Sponsor --------------------~--> 
$9.95 domain names from Yahoo!. Register anything.
http://us.click.yahoo.com/J8kdrA/y20IAA/yQLSAA/EbFolB/TM
--------------------------------------------------------------------~-> 

>-----------------------------------------~-~>
CHECK THE ARCHIVE BEFORE POSTING!!!! Archive is available at 
http://www.eScribe.com/software/C-Paradise/

>------------------------------------------_->


 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/C-Paradise/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



Reply via email to