module 3 Flashcards
(17 cards)
Write a main()
that will iterate through each command-line argument, and output them separated by a “ “
#include <iostream> using namespace std; int main(int argc, char* argv[]) { for (int i = 0; i < argc; ++i) { cout << argv[i] << ' '; } }
List the functions defined in <cctype>
-
isalnum(c)
- c is a letter or digit -
isalpha(c)
- c is a letter -
iscntrl(c)
- c is a control character -
isdigit(c)
- c is a digit -
isgraph(c)
- c is not a space but is printable -
islower(c)
- c is a lowercase letter -
isprint(c)
- c is printable -
ispunct(c)
- c is a punctuation character -
isspace(c)
- c is whitespace -
isupper(c)
- c is an uppercase letter -
isxdigit(c)
- c is a hexadecimal digit -
tolower(c)
- makes c a lowercase letter -
toupper(c)
- makes c an uppercase letter
Write a for loop
to iterate through the following string, and make it all uppercase, using string iterators
:
#include <string> #include <cctype> int main() { string s{"Now is the time!"}; // for loop goes here }
for (auto iter = s.begin(); iter != s.end(); ++iter) { *iter = toupper(*iter); }
Write a range-based for loop
to iterate through the following string, and make it all uppercase:
#include <string> #include <cctype> int main() { string s{"Now is the time!"}; // for loop goes here }
for (auto& a : s) { a = toupper(a); }
What allows you to read from a string
as if it were a stream
?
istringstream
Read the following string
into an int
, a string
, then a double
:
string data("1 two 3.2");
istringstream istr(data); int n; string s; double x; istr >> n >> s >> x;
n == 1, s == “two”, x == 3.2
Write the following int
, string
, and double
, into a string
separated by a space, into a string
called data
:
int n = 1; string s = "two"; double x = 3.2; string data;
ostringstream ostr; ostr << n << " " << s << " " << x; data = ostr.str();
List the File Open Modes:
-
in
- open for input -
out
- open for output -
app
- seek to the end before every write -
ate
- seek to the end immediately after the open (must specifyin
,out
, or both) -
trunc
- truncate the file (must specifyin
,out
, or both) -
bin
- do IO operations in binary mode (must specifyin
,out
, or both)
preceded with ios::
What are the default File Open Modes for ofstream
?
ios::out
and ios::trunc
What is output by the following code?
#include <iostream> #include <sstream> using namespace std; int main() { int n; double x; string s; s = "1523.91"; istringstream is1(s); is1 >> x; cout << x << endl; // output 1 is1.clear(); is1.seekg(2,ios::beg); is1 >> n; cout << n << endl; // output 2 is1.seekg(-1,ios::cur); is1 >> s; cout << s << endl; // output 3 }
1523.91
23
3.91
What is output by the following code?
#include <iostream> #include <format> using namespace std; int main() { string s {"Hello"}; cout << format("{0}, John. {0}, Jill.\n{1} characters needed to say {0}\n", s, s.size()); }
Hello, John. Hello, Jill.
5 characters needed to say Hello
What is output by the following code?
#include <iostream> #include <format> using namespace std; int main() { string s {"Hello"}; cout << format("String '{}' has {} characters\n", s, s.size()); }
String ‘Hello’ has 5 characters
What is output by the following code?
#include <iostream> #include <format> using namespace std; int main() { cout << format ("{:.3f}\n", 3.5); cout << format ("{:8.3f}\n", 3.14159265); }
3.500 3.142
Write an Effector class called HideSpace
that takes a string
as a parameter, that removes the whitespace when the string
is output.
class HideSpace { string str; public: HideSpace (string s) :str(s) {} friend ostream& operator<<(ostream& os, const HideSpace& n) { for (auto c : n.str) { if (!isspace(c)) os << c; } return os; } };
What will be output by the following code?
#include <iostream> #include <string> #include <iomanip> using namespace std; int main() { string s {"Happy Birthday"}; cout << quoted(s) << endl; }
"Happy Birthday"
What will be output by the following code?
#include <iostream> #include <string> #include <iomanip> using namespace std; int main() { string s {"She said \"Happy Birthday\""}; cout << s << endl; cout << quoted(s) << endl; }
She said "Happy Birthday" "She said \"Happy Birthday\""
List the Standard Exception Classes defined in <stdexcept>
-
exception
- the most general kind of problem -
runtime_error
- a problem that can only be detected at run time -
range_error
- a runtime error with a result that generated outside the range of values that are meaningful -
overflow_error
- runtime error: computation that overflowed -
underflow_error
- runtime error: computation that underflowed -
logic_error
- error in the logic of the problem -
domain_error
- logic error: argument for which no result exists -
invalid_argument
- logic error: inappropriate argument -
length_error
- logic error: attempt to create an object larger than the maximum size for the type -
out_of_range
- logic error: used a value outside the valid range