C ++ program does not move past cin step for string input

I obviously don't quite understand the concept of "end of file" with C ++ as the program below just doesn't go through the "while (cin -> x)" step. Whenever I run it from the command line, it just sits there taunting it.

Searching through SO and other places gives a lot of mention of hitting ctrl-z and then hitting enter to skip the end-of-file character in windows, but that doesn't seem to work for me. This leads me to assume my problem is elsewhere. Maybe defining x as strings is my fault? Any suggestions on where I go wrong would be great.

Note: sorry for missing comments in the code - the program itself should accept a series of words and then return a count for each word.

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iomanip>

using std::cin;
using std::cout;            using std::endl;
using std::sort;
using std::string;          using std::vector;

int main()
{
    cout << "Enter a series of words separated by spaces, "
            "followed by end-of-file: ";

    vector<string> wordList;
    string x;
    while (cin >> x)
          wordList.push_back(x);

    typedef vector<string>::size_type vec_sz;
    vec_sz size = wordList.size();
    if (size == 0) {
       cout << endl << "This list appears empty.  "
                       "Please try again."  << endl;
       return 1;
    }

    sort(wordList.begin(), wordList.end());

    cout << "Your word count is as follows:" << endl;
    int wordCount = 1;
    for (int i = 0; i != size; i++) {
        if (wordList[i] == wordList[i+1]) {
           wordCount++;
           }
        else {
             cout << wordList[i] << "    " << wordCount << endl;
             wordCount = 1;
             }
         }
    return 0;
}

      

+2


source to share


4 answers


If you're on windows ^ Z should appear as the first character after a newline, if you're using a unixy shell, then you want to type ^ D.



+3


source


The code entry part works. The only real problem I see is a loop that tries to count words:

for (int i = 0; i != size; i++) {
    if (wordList[i] == wordList[i+1]) {

      



Valid indices for wordList are from 0 to size-1. In the last iteration of your loop, i = size-1, but then you try to use wordList[i+1]

by indexing past the end of the vector and getting undefined results. If you used wordList.at(i+1)

this instead, it would throw an exception, quickly letting you know more about the problem.

My guess is that what happens is that you press Control-Z and it exits the input loop, but it crashes when it tries to count the words, so when you fix it will work better at all. If you really can't get through the input loop after fixing another problem (s?) And you're running Windows, you can try using F6 instead of entering control-Z - it seems a little more reliable.

+1


source


I almost always use getline when using cin (especially when I want it to be a line):

istream& std::getline( istream& is, string& s );

      

So, you would call getline(cin, x)

and it will grab everything to a new line. You must wait for the new cin line to give you something anyway. So, in this case, your loop will become:

while(getline(cin, x))
   wordList.push_back(x);

      

0


source


cin

does not accept whitespace or line breaks, so execution cin

does not exit unless you type anything, here is a test program that gives you what you want

#include "stdafx.h"
#include<iostream>
#include <string>
#include <sstream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    string str = "";
    while(std::getline(cin, str) && str!="")
    {
        cout<<"got "<<str<<endl;
    }
    cout<<"out"<<endl;
    cin>>str;
    return 0;
}

      

0


source







All Articles