1.6 Bookstore Program
This section is the “comprehensive case” of Chapter 1—combining the previously learned input and output,<span><span>if</span></span>、<span><span>for</span></span>、<span><span>Sales_item</span></span> class to write a program that “processes sales records of multiple books”. The function is to read the sales records of multiple books, accumulate the sales data of the same ISBN, and finally output the total sales record for each book.
Complete Code
#include <iostream>
#include "Sales_item.h"
using namespace std;
int main() {
Sales_item total; // Store the sales record of the first book (as the initial total record)
// First, read the record of the first book
if (cin >> total) {
Sales_item trans; // Store the records of subsequent books
// Loop to read subsequent records until input ends (Ctrl+Z on Windows, Ctrl+D on Linux)
while (cin >> trans) {
// If the current record's ISBN is the same as the total record's ISBN, accumulate
if (total.isbn() == trans.isbn()) {
total += trans; // Equivalent to total = total + trans
} else {
// If the ISBN is different, first output the previous total record, then update total to the current record
cout << total << endl;
total = trans;
}
}
// Output the total record of the last book (the last total has not been output after the loop ends)
cout << total << endl;
} else {
// If no records were read, output an error message
cerr << "No sales records were input!" << endl;
return -1;
}
return 0;
}
Program Logic Analysis
- Initial Read: First use
<span><span>if (cin >> total)</span></span>to read the record of the first book—if reading fails (e.g., the user directly inputs the end symbol), output an error message and exit. - Loop to Read Subsequent Records: Use
<span><span>while (cin >> trans)</span></span>to loop to read subsequent records—<span><span>cin >> trans</span></span>‘s return value is<span><span>cin</span></span>itself, when<span><span>cin</span></span>encounters the “input end symbol” (press<span><span>Ctrl+Z</span></span>on Windows, press<span><span>Ctrl+D</span></span>on Linux) or “input error”, it will return<span><span>false</span></span>, and the loop ends. - Accumulate or Output
- If
<span><span>trans</span></span>has the same ISBN as<span><span>total</span></span>: Use<span><span>total += trans</span></span>to accumulate sales data. - If the ISBN is different: first output
<span><span>total</span></span>(the total record of a previously accumulated book), then update<span><span>total</span></span>to<span><span>trans</span></span>(start processing the next book).
<span><span>cout << total</span></span>.Core Summary of Chapter 1
- Program StructureA C++ program must have exactly one
<span><span>main()</span></span>function, and the program starts executing from<span><span>main()</span></span><code><span><span>. The return value of</span></span><code><span><span>main()</span></span><span><span> is used to inform the operating system of the program's execution status (0 for success, non-0 for failure).</span></span> - Input and Output
<span><span>std::cout</span></span>(paired with<span><span><<</span></span>) is used for output,<span><span>std::cin</span></span>(paired with<span><span>>></span></span>) is used for input.<span><span>using namespace std;</span></span>can simplify the use of standard library components, but caution is needed in large projects.
- Statements must end with
<span><span>;</span></span>. - Comments use
<span><span>//</span></span>(single line) or<span><span>/* ... */</span></span>(multi-line). - Control flow:
<span><span>if</span></span>(branching),<span><span>for</span></span>(looping) are the most basic control statements.