Problem overview
The Top K Brands project builds a small search-results display: for a product, show the top-rated brand that sells it. In this first version there is exactly one product and one brand. The starter gives you product, brand and rating, and the program must print two lines: the product name, then a ranked entry written as 1. Brand (rating).
For the starter data the output is:
Laptop
1. TechPro (4)
The lesson asks you to keep each line in its own variable, productLine and brandLine, join them with a newline and print the result.
Approach: build each line as a string
The first line is just the product name, so productLine is a copy of product. The second line is assembled from pieces: the rank "1. ", the brand, a space and an opening parenthesis, the rating, and a closing parenthesis.
Most of those pieces are text, but rating is an int. C++ does not join a number to a string with +. The standard library function to_string converts a number into a string, and after that all the pieces are strings and + works.
Step by step
- Create
productLine. Declare astringand set it toproduct. Giving every output line its own variable keeps the final print simple. - Create
brandLine. Start from the string literal"1. ", addbrand, add" (", addto_string(rating), and finish with")". The rank is part of the text because this version has only one brand, and it is always first. - Print both lines. Send
productLine, the newline"\n"andbrandLinetocout, and end withendl. The"\n"between them is what puts the two lines on separate rows.
The solution in C++
string product = "Laptop";
string brand = "TechPro";
int rating = 4;
string productLine = product;
string brandLine = "1. " + brand + " (" + to_string(rating) + ")";
cout << productLine << "\n" << brandLine << endl;
Details worth a closer look:
- The order of the
+chain matters. The expression is evaluated from left to right. It starts with"1. " + brand, which is a string literal plus astd::string, and that is allowed because one operand is astring. Every later step adds to astringthat already exists. - Two literals can't be added. An expression like
"1. " + "abc"does not compile, because both sides are plain character arrays and there is no+for them. In the solution,brandsits right after the first literal, so the chain is valid. to_stringonly converts the number. Labels such as" ("are already text and are not wrapped.- Newline.
"\n"is a single newline character.endlat the end ends the second line and flushes the output.
Edge cases and common mistakes
- Adding the number directly. Writing
"1. " + brand + " (" + rating + ")"is rejected by the compiler with an error that startsno match for 'operator+', namingstd::stringandintas the operand types. Wrap the number into_string. - Appending an
intwith+=. This one compiles, which makes it worse:s += ratingappends the character whose code is the number, not its digits. Withrating = 66ands = "R",sbecomesRB, while"x" + to_string(66) + "x"givesx66x. - Missing spaces. The format has a space after the dot and a space before the parenthesis. Leaving either out changes the output, and the lesson's tests compare it exactly.
- Using
coutchaining for the whole line.cout << "1. " << brand << " (" << rating << ")"also prints the right text, withoutto_string. This lesson builds each line as a string first, as its rules ask.
Testing
With other values in the starter variables, the same code prints:
| product, brand, rating | Output |
|---|---|
| Laptop, TechPro, 4 | Laptop, then 1. TechPro (4) |
| Phone, Nova, 5 | Phone, then 1. Nova (5) |
