#include #include using namespace std; struct input { string line; int pos, len; // function inside struct // no return type // same name as struct // is an automatic constructor input() { line = ""; pos = 0; len = 0; } void fill(string s) { line = s; pos = 0; len = s.length(); } void print() { cout << "line is '" << line << "'\n"; cout << "pos is " << pos << "\n"; cout << "len is " << len << "\n"; } char next() { if (pos >= len) return '\n'; char result = line[pos]; pos += 1; return result; } void un_next() { if (pos <= 0) return; pos -= 1; } }; string read_atom(input & x) { string s = ""; char c1 = x.next(); if (c1 < 'A' || c1 > 'Z') { cout << "Capital expected at beginning of atom\n"; return "XXXXXX"; } s += c1; char c2 = x.next(); if (c2 >= 'a' && c2 <= 'z') return s + c2; x.un_next(); return s; } int main() { input I; string s; cout << "Enter a line: "; getline(cin, s); I.fill(s); // internal function of input object: Method for (int x = 0; x < 6; x += 1) { string at = read_atom(I); cout << "Atom = '" << at << "'\n"; I.print(); } } /* while (true) { I.print(); char c = I.next(); cout << "Got the character '" << c << "'\n"; if (c == '\n') break; } } */