# Std::cin inside work()

**URL:** <https://www.ruby-forum.com/t/std-cin-inside-work/240069>\
**Category:** GNU Radio\
**Created:** [March 26, 2014, 9:19am UTC](https://www.ruby-forum.com/t/std-cin-inside-work/240069 "2014-03-26T09:19:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [March 26, 2014, 9:19am UTC](https://www.ruby-forum.com/t/std-cin-inside-work/240069/1 "2014-03-26T09:19:24Z")

</div>

Dear sir,  
Why below custom block doesn’t work…?

```
int
console1_impl::work(
          int noutput_items,
          gr_vector_const_void_star &input_items,
          gr_vector_void_star &output_items)
{
    int *out = (int *) output_items[0];

    int x;
    std::cout << "Please key in an integer: ";
    std::cin >> x;
    std::cout << "You have given me " << x << std::endl;

    out[0] = x;
    return 1;
}
```

---

<div class="post-metadata">

**Author:** ![activecat\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c57346/32.png) [@activecat\_k](https://www.ruby-forum.com/u/activecat_k)\
**Post date:** [March 26, 2014, 10:46am UTC](https://www.ruby-forum.com/t/std-cin-inside-work/240069/2 "2014-03-26T10:46:33Z")

</div>

On 03/26/2014 09:17 AM, Activecat wrote:\> Dear sir,

> Why below custom block doesn’t work..?

Activecat,

first, don’t ask open questions like this. How do you know it’s not  
working? What did you try? Is there an error message? Always provide all  
of these infos when asking questions. You will get much better answers.  
Make sure you’ve read  
[http://gnuradio.org/redmine/projects/gnuradio/wiki/ReportingErrors](http://gnuradio.org/redmine/projects/gnuradio/wiki/ReportingErrors).

> ```
> std::cin >> x;
> std::cout << "You have given me " << x << std::endl;
> 
> out[0] = x;
> return 1;
> }
> 
> ```

A couple of guidelines:

- Don’t block in work()! (i.e., don’t do this)
- This would probably be better solved by passing info into the stream  
with other means, e.g. messages (PDUs), or by writing to a pipe and  
reading from there.
- Try adding a std::flush after the first cout.

M

---

<div class="post-metadata">

**Author:** ![activecat\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c57346/32.png) [@activecat\_k](https://www.ruby-forum.com/u/activecat_k)\
**Post date:** [March 26, 2014, 11:58am UTC](https://www.ruby-forum.com/t/std-cin-inside-work/240069/3 "2014-03-26T11:58:31Z")

</div>

Thank you very much.
