module 3 Flashcards

(17 cards)

1
Q

Write a main() that will iterate through each command-line argument, and output them separated by a “ “

A
#include <iostream>
using namespace std;
int main(int argc, char* argv[]) {
     for (int i = 0; i < argc; ++i) {
          cout << argv[i] << ' ';
     }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

List the functions defined in <cctype>

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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
}
A
for (auto iter = s.begin(); iter != s.end(); ++iter) {
     *iter = toupper(*iter);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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
}
A
for (auto& a : s) {
     a = toupper(a);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What allows you to read from a string as if it were a stream?

A

istringstream

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Read the following string into an int, a string, then a double:

string data("1 two 3.2");
A
istringstream istr(data);
int n;
string s;
double x;
istr >> n >> s >> x;

n == 1, s == “two”, x == 3.2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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;
A
ostringstream ostr;
ostr << n << " " << s << " " << x;
data = ostr.str();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

List the File Open Modes:

A
  • 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 specify in, out, or both)
  • trunc - truncate the file (must specify in, out, or both)
  • bin - do IO operations in binary mode (must specify in, out, or both)

preceded with ios::

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the default File Open Modes for ofstream?

A

ios::out and ios::trunc

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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
}
A

1523.91
23
3.91

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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());
}
A

Hello, John. Hello, Jill.
5 characters needed to say Hello

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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());
}
A

String ‘Hello’ has 5 characters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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);
}
A
3.500
      3.142
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Write an Effector class called HideSpace that takes a string as a parameter, that removes the whitespace when the string is output.

A
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;
    }
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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;
}
A
"Happy Birthday"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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;
}
A
She said "Happy Birthday"
"She said \"Happy Birthday\""
17
Q

List the Standard Exception Classes defined in <stdexcept>

A
  • 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