/* This just shows the parts of the progrm that relate to the nicestream, today's real topic */ struct nicestream // intended to be nicer and more convenient for us than "cin >>" { string wholeline; int numtaken; int length; bool ended; }; char readchar(nicestream & ns) { char c; if (ns.numtaken >= ns.length) c = '\n'; else c = ns.wholeline[ns.numtaken]; ns.numtaken += 1; return c; } char sneakchar(const nicestream & ns) { return ns.wholeline[ns.numtaken]; } void unreadchar(nicestream & ns) { if (ns.numtaken > 0) ns.numtaken -= 1; } void refill(nicestream & ns) { getline(cin, ns.wholeline); if (cin.fail()) { ns.wholeline = "\n"; ns.ended = true; } else { ns.wholeline += '\n'; ns.ended = false; } ns.length = ns.wholeline.length(); ns.numtaken = 0; } bool ok(const nicestream & ns) { return ! ns.ended; } /* This is the part of main that processes the molecular formula */ nicestream nin; refill(nin); while (true) { char c = readchar(nin); if (c >= 'A' && c <= 'Z') { string symbol = ""; symbol += c; c = sneakchar(nin); if (c >= 'a' && c <= 'z') { c = readchar(nin); symbol += c; } cout << "The symbol is '" << symbol << "'\n"; } else if (c == '\n') break; else if (c >= '0' && c <= '9') { int num = 0; while (c >= '0' && c <= '9') { num = num * 10 + c - '0'; c = readchar(nin); } unreadchar(nin); cout << "number " << num << "\n"; } else cout << "Only dealing with symbols yet\n"; } cout << "I would say the molecular weight here\n";