Start learning free

LESSON 66 OF 667 · STRINGS I

Top K Brands V1 in C++: Joining Strings and Numbers

Practice Read the full explanation

E-commerce search page: when a shopper searches a product, show the top-rated brand selling it. In v1 there's just one product and one brand.

You start with:

string product = "Laptop";
string brand = "TechPro";
int rating = 4;

Build:

Laptop
1. TechPro (4)

Rules:

  • The first line is the product name.
  • The second line is a ranked entry written as "1. Brand (rating)".
  • Keep each line in its own variable (productLine and brandLine), join them with a newline "\n" and print the result with cout << ... << endl.

C++ needs to_string(rating) to join a number to a string with +.

productLine + "\n" + brandLine joins both lines into one string.

string product = "Laptop";
string brand = "TechPro";
int rating = 4;

string productLine = product;
string brandLine = "1. " + brand + " (" + to_string(rating) + ")";
cout << productLine << "\n" << brandLine << endl;

STEP 1 OF 3
    Ctrl + Enter to check

    C++ · STRINGS I

    Top K Brands V1 in C++, explained

    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

    1. Create productLine. Declare a string and set it to product. Giving every output line its own variable keeps the final print simple.
    2. Create brandLine. Start from the string literal "1. ", add brand, add " (", add to_string(rating), and finish with ")". The rank is part of the text because this version has only one brand, and it is always first.
    3. Print both lines. Send productLine, the newline "\n" and brandLine to cout, and end with endl. 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 a std::string, and that is allowed because one operand is a string. Every later step adds to a string that 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, brand sits right after the first literal, so the chain is valid.
    • to_string only converts the number. Labels such as " (" are already text and are not wrapped.
    • Newline. "\n" is a single newline character. endl at 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 starts no match for 'operator+', naming std::string and int as the operand types. Wrap the number in to_string.
    • Appending an int with +=. This one compiles, which makes it worse: s += rating appends the character whose code is the number, not its digits. With rating = 66 and s = "R", s becomes RB, while "x" + to_string(66) + "x" gives x66x.
    • 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 cout chaining for the whole line. cout << "1. " << brand << " (" << rating << ")" also prints the right text, without to_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, ratingOutput
    Laptop, TechPro, 4Laptop, then 1. TechPro (4)
    Phone, Nova, 5Phone, then 1. Nova (5)
    Keep going with C++650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free