Thread: determining the end of a piece of text

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    3

    determining the end of a piece of text

    Code:
    int i = 0;
    string word;
    vector <string> sentence;
    
    do{cin >> word; 
       sentence.push_back(word);
       i++;
      }while(i < 10);
    the idea is to get someone to type in a sentence into the console window. This sentence is then broken down into words for further manipulation. However I'd like a more efficient way to determine how many times the loop should execute itself rather than saying 'while(i <10)'.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Code:
    while ((cin >> word).good()) {...}
    //or
    while (!(cin >> word)) {...}
    gg

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    3
    perhaps it's just me, but it doesnt seem to work

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    The second example is wrong. To use the first you will have to use Ctrl-Z (for DOS) or Ctrl-D (for Unix) to signify EOF. Otherwise you have to use another mechanism for exiting the loop - like entering a "~" or something.

    gg

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Here is an example program that shows you how to do it using a string stream (without having to use Ctrl-Z or Ctrl-D). There might be a better way, but this sems to do what you want.
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include <sstream>
    
    int main()
    {
        // Get input from user.
        std::string inputLine;
        std::getline(std::cin, inputLine);
    
        // Create a stream to parse the line.
        std::istringstream lineStream(inputLine);
    
    	// Loop through each word in the stream.
        std::vector<std::string> sentence;
        while (!lineStream.eof())
        {
            std::string word;
            lineStream >> word;
            sentence.push_back(word);
        }
    
    	// Output one word per line.
        std::vector<std::string>::const_iterator iterWord = sentence.begin();
        std::vector<std::string>::const_iterator iterEnd = sentence.end();
        for (; iterWord != iterEnd; ++iterWord)
        {
            std::cout << *iterWord << std::endl;
        }
    }
    Note that if you are running VC++ 6.0 you will have to fix the bug in getline or this won't work well. Just go to http://www.dinkumware.com/vc_fixes.html and scroll down to the <string> fix.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    If this isn't something for school, you might try my generic parser. Aside from ordinary word parsing, you can easily devise all sorts of custom parsing schemes - including the C++ language itself.

    Another good exercise is 'manual' parsing - letter by letter that is.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  2. Text adventure engine idea. thoughts?
    By suzakugaiden in forum Game Programming
    Replies: 16
    Last Post: 01-15-2006, 05:13 AM
  3. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  4. Replies: 3
    Last Post: 05-25-2005, 01:50 PM
  5. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM