//
// 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.