Wrong usage of cin.eof()?

This is a discussion on Wrong usage of cin.eof()? within the C++ Programming forums, part of the General Programming Boards category; I'm using cin to read in words that are then sorted into a vector along with a counter for how ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    399

    Wrong usage of cin.eof()?

    I'm using cin to read in words that are then sorted into a vector along with a counter for how many times they have been entered, but the last word always seem to be read in twice.

    For exampe, if I enter "foo bar", then it will say:
    foo 1
    bar 2

    instead of:
    foo 1
    bar 1

    I can't find anything wrong with the insert function, so I'm guessing that something is wrong with how the input is received.

    Code:
    string input;
    while (!cin.eof())
    {
            cin >> input;
            insert(word_list, input);
    }
    Code:
    void insert (vector<word_entry> &word_list, string input)
    {
    	// Convert input to lowercase
    	transform(input.begin(), input.end(), input.begin(), ::tolower);
    			
    	vector<word_entry>::iterator i;
    	for (i = word_list.begin(); i <= word_list.end(); i++)
    	{
    		word_entry elem = {input, 1};
    		
    		if (i == word_list.end())
    		{
    			word_list.push_back(elem);
    			break;
    		}
    			
    		if (i->word.compare(input) > 0)
    		{
    			word_list.insert(i, elem);
    			break;
    		}
    		else if (i->word.compare(input) == 0)
    		{
    			i->count++;
    			break;
    		}
    	}
    }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    eof becomes true only after an attempt to input fails. If the attempt fails, the input string retains its previous value. As a result the last input is stored in the vector one more time.

    Input loops are controlled like this:

    Code:
    while (cin >> input) {
        ...
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    You might want to consider using std::map for this, as it's perfectly suited to what you want to do.

    Code:
    std::map<string, int> wordMap;
    std::vector<string> words;
    
    for(std::vector<string>::iterator i = words.begin(); i != words.end(); i++)
    {
         ++wordMap[*i];
    }
    This is very similar to the wikipedia example. http://en.wikipedia.org/wiki/Map_(C%2B%2B_container)
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Okay, thanks! I found some more information on why the return value of cin can be interpreted as a boolean value if anyone is interested: C++ Notes: What values do the I/O operators (<< and >>) return?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reduce CPU usage
    By patrick22 in forum Windows Programming
    Replies: 9
    Last Post: 07-10-2009, 03:13 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 01:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong with my vector usage?
    By Alphabird32 in forum C++ Programming
    Replies: 4
    Last Post: 09-14-2002, 11:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21