manipulators

custom stream manipulator for class

筅森魡賤 提交于 2019-11-29 12:59:10
I am trying to write a simple audit class that takes input via operator << and writes the audit after receiving a custom manipulator like this: class CAudit { public: //needs to be templated CAudit& operator << ( LPCSTR data ) { audittext << data; return *this; } //attempted manipulator static CAudit& write(CAudit& audit) { //write contents of audittext to audit and clear it return audit; } private: std::stringstream audittext; }; //to be used like CAudit audit; audit << "Data " << data << " received at " << time << CAudit::write; I recognise that the overloaded operator in my code does not

Effect of noskipws on cin>>

。_饼干妹妹 提交于 2019-11-28 11:28:45
As I understand, the extraction operator skips the whitespace in the beginning and stops upon encountering a whitespace or end of stream. noskipws can be used to stop ignoring the leading whitespaces. I have the following program where I have used noskipws. #include <iostream> using namespace std; int main() { char name[128]; cout<<"Enter a name "; cin>>noskipws>>name; cout<<"You entered "<<name<<"\n"; cout<<"Enter another name "; cin>>name; cout<<"You entered "<<(int)name[0]<<"\n"; return 0; } My queries are: If I enter "John" as the first input, then the second cin>> operation does not wait