Skip to main content
Contents
Search Book
Search Results:
No results.
Readability settings Prev Up Next Scratch ActiveCode Profile
title here
\(
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Section 9.2 printTime
When we define a new type it is a good idea to write function that displays the instance variables in a human-readable form, which you attempted on the page before. Your solution should have looked something like this.
void printTime(Time& t) {
cout << t.hour << ":" << t.minute << ":" << t.second << endl;
}
Listing 9.2.1. In this active code, the output of this function, if we pass time an argument, is 11:59:3.14159.
Checkpoint 9.2.1 .
Which of the following would be a correct way to display the price of an object and finish the
printPrice, which we saw on the previous page?
struct Price {
int dollar, cents;
};
void printPrice(Price& p) {
// Implementation here
}
int main() {
Price sandwich = { 3, 45 };
Price coffee = { 2, 50 };
Price pastry = { 2, 0 };
}
cout << "Price is " << "p.dollar" << " dollars and" << "p.cents" << "cents." << endl;
Try again. We want to print the values rather than statements.
cout << "Price is " << p.dollar << " dollars and " << p.cents << " cents." << endl;
Correct!
cout << "Price is " << p.dollar << " dollars and " << p.cents << " cents." << endl
This would not compile. There is an important character that ends nearly all statements in C++.
You have attempted
of
activities on this page.