Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cin stream infinite loop, why?

Tags:

c++

I copied this simple program from c++ programming language, but I cannot get it work as desired. Am I missing something? Basically, the program will output "input end" after I hit return, and then repeat input from cin. It is never able to go to the next statement. I tried to use the vector (commented two statements below), the same. Tried on Vc6 and vs2008.

    #include <iostream>
#include <map>
#include <algorithm>
#include <string>
#include <iterator>
#include <vector>
using namespace std;

map<string, int> histogram;
void record(const string &s)
{
    histogram[s]++; //this is pretty strange, however it does work!
    cout<<"recorded:"<<s<<" occurance="<<histogram[s]<<"\n";
}

void print(const pair<const string,int> &r)
{
    cout<<r.first<<' '<<r.second<<'\n';
}

int main()
{
    istream_iterator<string> ii(cin);
    istream_iterator<string> eos;
    cout<<"input end\n";

    for_each(ii,eos,record); //this statement cannot get out why? It repeats the keyboard input
    //vector<string> b(ii,eos);
    //for_each(b.begin(),b.end(),record);
    for_each(histogram.begin(),histogram.end(),print); //program never comes here why?
}

Running results:

a b c

input end

recorded:a occurance=1

recorded:b occurance=1

recorded:c occurance=1

1 2 3

recorded:1 occurance=1

recorded:2 occurance=1

recorded:3 occurance=1

like image 560
shangping Avatar asked Jul 19 '11 04:07

shangping


People also ask

Why is infinite loop necessary?

Usually, an infinite loop results from a programming error - for example, where the conditions for exit are incorrectly written. Intentional uses for infinite loops include programs that are supposed to run continuously, such as product demo s or in programming for embedded system s.

What causes an infinite loop in C++?

A loop becomes infinite loop if a condition never becomes false. The for loop is traditionally used for this purpose. Since none of the three expressions that form the 'for' loop are required, you can make an endless loop by leaving the conditional expression empty.

How do you avoid infinite loop C++?

To stop your code going into infinite loop, you have to use either break statement or you can use the concept of exception handling using try,catch, throw etc. If suddenly you program runs in infinite loop, then use ctrl+pause/break.


1 Answers

istream_iterator will continue until it hits the end of the stream, which on cin doesn't normally happen.

cin will encounter an end-of-stream, causing your statement to terminate, when it was redirected to a file (when it reaches the end of the file), or if it's receiving console input you can send an end-of-stream by pressing CTRL-Z (CTRL-D on Linux, iirc). You may need to press enter afterwards.

Note that cin will be unusable after you do this, so you cannot read more input after that point.

Until the end-of-stream is encountered, ii will remain valid and continue to request more data from the stream (in thie case the console).

The solution would be to not use for_each but a manual while loop which you can break out of when whatever condition you want is satisfied.

like image 177
Sven Avatar answered Sep 29 '22 07:09

Sven