int main() { string first; char second[10]; cin >> first >> second; ... }
first == "My" second == "name"
std::string full_name_with_titles; getline(cin, full_name_with_titles);
string file_contents; getline(cin, file_contents, '\0');
vector<int> read_numbers(istream& is) { vector<int> result; int number; while ( is >> number ) result.push_back(number); return result; }
is >> number; while ( is.good() ) { result.push_back(number); is >> number; }
while ( is.good() ) { is >> number; result.push_back(number); }
class Student { public: std::string& name(); std::string& id();... };and you want to support
Student a_student; cin >> a_student; ... update cout << a_student;
istream& operator>>(istream& input, Student& stu) { getline(input, stu.name); input >> stu.id; return input; } ostream& operator<<(ostream& output, const Student& stu) { output << stu.name << endl; output << stu.id; return output; }
long num; ... cout << hex << num << ", decimal " << num;
cout << fixed << 1.5e10; // prints 1500000000
cout.setprecision(4); // all floats printed with up to 4 digits after decimal
cout << 'x' << setw(4) << 5; // prints: x 5 cout << 'y' << setw(4) << '5' << 'z'; // prints: x5 z
ifstream mydata("config.txt"); // opens file if ( !mydata.good() ) cerr << "Could not open config.txt" << endl; int x; string y; mydata >> x >> y; if ( mydata.eof() ) cerr << "Unexpected end of file." << endl;
ofstream image("results.dat"); // opens file if ( !image.good() ) cerr << "Could not write to results.dat" << endl; image << "Image data: "; for(int i = 0; i < 10000; ++i) image << stuff[i];
#include <sstream> using namespace std; ... ostringstream buf; buf << fixed << 6.0221109e23 string converted = buf.str(); istringstream namebuf("Tom Riddle"); string first, last; namebuf >> first >> last;