0
//
// This is example code from Chapter 20.6.1 "Lines" of
// "Programming -- Principles and Practice Using C++" by Bjarne Stroustrup
//

#include <std_lib_facilities.h>
#include <Windows.h>

const char NEW_LINE = '\n';

//------------------------------------------------------------------------------

typedef vector<char> Line;    // a line is a vector of characters

//------------------------------------------------------------------------------

class Document {
public:
    Document();// { line.push_back(Line()); }
    bool add(char ch);
    void print();
private:
    list<Line> _line;    // a document is a list of lines 
};

Document::Document()
: _line({ Line() })
{}

bool Document::add(char ch)
{
    Beep(0x25, 1);
    if (ch == NEW_LINE) {
        _line.emplace_back(Line());
    }
    _line.back().emplace_back(ch);

    return true;
}

void Document::print()
{
    if (_line.size()) {
        cout << endl;
        for (auto line_it = _line.begin(); line_it != _line.end(); ++line_it) {
            for (auto char_it = line_it->begin(); char_it != line_it->end(); ++char_it) {
                cout << *char_it;
            } // end char_it for loop
            cout << endl;
        } // end _line_it for loop
    } // end if block _line.size()
}
//------------------------------------------------------------------------------

istream& operator>>(istream& is, Document& d)
{
    char ch = 0;
    while (is.get(ch)) {
        if (ch == '[') {
            break;
        }
        d.add(ch);
        cout << "\nHERE";
    }
    return is;
}

//------------------------------------------------------------------------------

int main()
{
    Document d;
    cin >>  d;

    d.print();

    system("PAUSE");
}

//------------------------------------------------------------------------------

I am trying to get it to input one item then move on without having to press enter. IE if I run the program and type S it should call d.add(ch) right away without me having to press enter. I have played around with is >> ch and is.get() in all forms and is.peek() and is.read(). I have also moved where the is.get(ch) is currently to different parts of the while loop. All with no luck. Now its likely I didnt use one of them correctly and as soon as one of the smart persons post on here I am going to be smacking my forehead. Please help me either use what I am using in the proper way or point me in the direction of the right functions to call.

Right now its just having me input a line of text then press enter than it enters everything. Its late I am tired and trying to learn something new, so on top of saying thank you very much for your time and effort. I also want to say sorry =p.

p.s. Please pay no attention to the headers. One is Stroustrup's header that basically includes all the common things for learning purposes.

Jason Ball
  • 163
  • 8
  • 2
    It's all useless, the `std::cin` stream doesn't see anything appear in it's buffer, until the user hits the `ENTER` key. – πάντα ῥεῖ Oct 11 '14 at 08:02
  • [You probably thought this would be easy](http://stackoverflow.com/questions/3962263/checking-if-a-key-was-pressed). – WhozCraig Oct 11 '14 at 08:03
  • Is there another form of input i can use that will do what i need then. Thank you for the looking at my code. – Jason Ball Oct 11 '14 at 08:03
  • @WhozCraig Are you saying keyboard interrupts are the only way to go on this? Again thank you for looking at the code. – Jason Ball Oct 11 '14 at 08:06
  • 3
    There's no way to do this in standard C++. You would need to use platform-specific things. On Windows, lookup `_getch()`. – T.C. Oct 11 '14 at 08:46
  • @T.C. Thank you that was what I was looking for. If you like I can mark yours as the answer. – Jason Ball Oct 11 '14 at 14:06

0 Answers0