Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8399703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:22:24+00:00 2026-06-09T21:22:24+00:00

I am working through some C++ exercises in Visual Studio 2010, and I keep

  • 0

I am working through some C++ exercises in Visual Studio 2010, and I keep having problems with an infinite loop which occurs when I try to terminate a standard in stream with “CTRL-Z”, when using the getline() function. Here is the relevant bit of code….

// find all the lines that refer to each word in the input
map<string, vector<int> >
    xref(istream& in,
         vector<string> find_words(const string&) = split)
{
    string line;
    int line_number = 0;
    map<string, vector<int> > ret;

    // read the next line
    while (getline(in, line)) {
        ++line_number;

        // break the input line into words
        vector<string> words = find_words(line);

        // remember that each word occurs on the current line
        for (vector<string>::const_iterator it = words.begin();
             it != words.end(); ++it)
            ret[*it].push_back(line_number);
    }
    return ret;
}

…instead of kicking me out of the while loop, the program goes into an infinite loop printing a random integer. I’m pretty sure this is something specific to the Windows environment that I’m missing. Here’s the entire code…

#include <algorithm>
#include <cctype>
#include <string>
#include <vector>

#include "split.h"

using std::find_if;
using std::string;
using std::vector;

using std::isspace;

// `true' if the argument is whitespace, `false' otherwise
bool space(char c)
{
    return isspace(c);
}

// `false' if the argument is whitespace, `true' otherwise
bool not_space(char c)
{
    return !isspace(c);
}

vector<string> split(const string& str)
{
    typedef string::const_iterator iter;
    vector<string> ret;

    iter i = str.begin();
    while (i != str.end()) {

        // ignore leading blanks
        i = find_if(i, str.end(), not_space);

        // find end of next word
        iter j = find_if(i, str.end(), space);

        // copy the characters in `[i,' `j)'
        if (i != str.end())
            ret.push_back(string(i, j));
        i = j;
    }
    return ret;
}

#include <map>
#include <iostream>
#include <string>
#include <vector>

#include "split.h"

using std::cin;            using std::cout;
using std::endl;           using std::getline;
using std::istream;        using std::string;
using std::vector;         using std::map;

// find all the lines that refer to each word in the input
map<string, vector<int> >
    xref(istream& in,
         vector<string> find_words(const string&) = split)
{
    string line;
    int line_number = 0;
    map<string, vector<int> > ret;

    // read the next line
    while (getline(in, line)) {
        ++line_number;

        // break the input line into words
        vector<string> words = find_words(line);

        // remember that each word occurs on the current line
        for (vector<string>::const_iterator it = words.begin();
             it != words.end(); ++it)
            ret[*it].push_back(line_number);
    }
    return ret;
}

int main()
{
    // call `xref' using `split' by default
    map<string, vector<int> > ret = xref(cin);

    // write the results
    for (map<string, vector<int> >::const_iterator it = ret.begin();
         it != ret.end(); ++it) {
        // write the word
        cout << it->first << " occurs on line(s): ";

        // followed by one or more line numbers
        vector<int>::const_iterator line_it = it->second.begin();
        cout << *line_it;   // write the first line number

        ++line_it;
        // write the rest of the line numbers, if any
        while (line_it != it->second.end()) {
            cout << ", " << *line_it;
            ++line_it;
        }
        // write a new line to separate each word from the next
        cout << endl;
    }

    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:22:26+00:00Added an answer on June 9, 2026 at 9:22 pm

    I think instead of trying to make this work, I’d start by writing code I could understand (and for me to understand it, the code has to be fairly simple):

    #include <map>
    #include <iostream>
    #include <string>
    #include <vector>
    #include <sstream>
    #include <iterator>
    #include "infix_iterator.h"
    
    typedef std::map<std::string, std::vector<unsigned> > index;
    
    namespace std {
    ostream &operator<<(ostream &os, index::value_type const &i) { 
        os << i.first << ":\t";
        std::copy(i.second.begin(), i.second.end(),
            infix_ostream_iterator<unsigned>(os, ", "));
        return os;
    }
    }
    
    void add_words(std::string const &line, size_t num, index &i) { 
        std::istringstream is(line);
        std::string temp;
    
        while (is >> temp)
            i[temp].push_back(num);
    }
    
    int main() { 
        index i;
        std::string line;
        size_t line_number = 0;
    
        while (std::getline(std::cin, line))
            add_words(line, ++line_number, i);
    
        std::copy(i.begin(), i.end(), 
            std::ostream_iterator<index::value_type>(std::cout, "\n"));
        return 0;
    }
    

    As (more or less) usual, this needs the infix_ostream_iterator I’ve posted elsewhere.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently working through some exercises in a c++ book, which uses text based
I'm working through exercises in Building Skills in Python , which to my knowledge
I am working through some indirect addressing problems and I am not sure how
I am working through some of the exercises in The C++ Programming Language by
I'm working through some python problems on pythonchallenge.com to teach myself python and I've
I am working through some Looping exercises, While statements in particular. Here are the
I am working through the 20 Intermediate Haskell Exercises at the moment, which is
I'm working through some android tutorials right now in preparation for a two week
I'm working through some tutorials and examples of java.util.concurrent package. Usually example authors put
I'm working through some MSDN examples, and some books on ADO.Net. What they all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.