You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"hello" is a C style string since it is just an array of char
Compare with ==, ~=, <, >, <=, >=
Length: str.length();
Characters: str[0]; (Same as C with char[])
cin will stop after reading seeing a space
getline(cin, s) will get the entire line until a \n is encountered
I/O manipulators (iostream, iomanip)
Print x in hexadecimal
Remember the code is evaluated left-to-right
cout << hex << x;
Change how cout works
All future cout will print in hex
cout << dec;
Change cout back to decimal
cout << showpoint << setprecision(3);
Print to three decimal points
Reading/writing files
The stream abstraction can work on other sources of data
Header <fstream>
ifstream - read from files
ofstream - write to files
Anything we can do with cin(type istream) we can do with myfile(type ifstream)
Same for cout & variable of type ofstream
When is this file close?
myfile is stack allocated and thus destroyed when it goes out of scope
When it is destroyed, it closes the file
Reading/writing to strings
Header <sstream>
istringstream -- read from strings
ostringstream -- write to strings
buildStrings.cc
Using ostringstream and writing to a string
#include<iostream>
#include<string>
#include<sstream>usingnamespacestd;intmain () {
ostringstream ss;
int lo {1}, hi {100};
ss << "Enter a # between " << lo << " and " << hi;
string s {ss.str()};
cout << s << endl;
}
getNum.cc
Insist user inputs a number
#include<iostream>
#include<string>
#include<sstream>usingnamespacestd;intmain () {
int n;
while (true) {
cout << "Enter a number:" << endl;
string s;
cin >> s;
// if (cin == EOF) return;
istringstream ss{s};
if (ss >> n) break;
cout << "I said, ";
}
cout << "You entered " << n << endl;
}
readIntsSS.cc
#include<iostream>
#include<sstream>usingnamespacestd;intmain () {
string s;
while (cin >> s) {
istringstream ss{s};
int n;
if (ss >> n) cout << n << endl;
}
}