Start learning free

LESSON 98 OF 667 · CONDITIONAL STATEMENTS - CHALLENGES

Top K Brands V2 in C++: Picking the Higher Rating with if/else

Practice Read the full explanation

Two brands sell the same product. Show the one with the higher rating.

You start with:

string brand1 = "TechPro";
int rating1 = 4;
string brand2 = "MegaCorp";
int rating2 = 5;

string bestBrand;
int bestRating;

Build:

1. MegaCorp (5)

Rules:

  • Use if/else to pick the winner: brand1 when rating1 > rating2, otherwise brand2.
  • The starter already declares bestBrand and bestRating: assign the winner's name and rating to them inside the branches.
  • Format the result as "1. Brand (rating)" and print it with cout << ... << endl.

Pick the winner into bestBrand and bestRating first, then build the output text from them.

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

string brand1 = "TechPro";
int rating1 = 4;
string brand2 = "MegaCorp";
int rating2 = 5;

string bestBrand;
int bestRating;
if (rating1 > rating2) {
    bestBrand = brand1;
    bestRating = rating1;
} else {
    bestBrand = brand2;
    bestRating = rating2;
}
cout << "1. " << bestBrand << " (" << bestRating << ")" << endl;

STEP 1 OF 5
    Ctrl + Enter to check

    C++ · CONDITIONAL STATEMENTS - CHALLENGES

    Top K Brands V2 in C++, explained

    Problem overview

    Two brands sell the same product, and the program must show the one with the higher rating. The starter code gives brand1 and rating1, brand2 and rating2, and it already declares two empty variables, bestBrand and bestRating. Your code fills those in and prints the winner as 1. Brand (rating).

    With the starter data, TechPro has a rating of 4 and MegaCorp has 5, so the output is:

    1. MegaCorp (5)
    

    Version 1 printed a ranked entry for a single brand. This version has to decide which of two brands deserves the entry.

    Approach: assign the winner's data in each branch

    The comparison is a plain if/else. When rating1 > rating2, the first brand wins. In every other case, the second brand wins, including a tie. The rules choose the strict > and say that a tie goes to brand 2, so the else branch covers both "brand 2 is better" and "they are equal".

    The two variables bestBrand and bestRating are declared before the if. That is deliberate: each branch assigns both, and after the branches the program can print them without knowing which branch ran. The name and the rating always travel together, so each branch sets the pair.

    The printing follows the same format as in version 1, with one difference: here it is a single cout chain, which can print an int directly with <<.

    Following the starter data

    With the starter values, the condition asks whether 4 > 5. It is false, so the program skips the if body and runs the else body instead. There, bestBrand becomes "MegaCorp" and bestRating becomes 5. The if body never ran, so its two assignments had no effect at all. The final cout line reads the two variables and prints 1. MegaCorp (5).

    Swap the ratings, so that TechPro has 5 and MegaCorp has 4, and the same code takes the other path: 5 > 4 is true, the if body runs, and the else body is skipped. The printing code does not change. That is the benefit of collecting the result in bestBrand and bestRating first: the decision happens in one place and the output in another, so neither has to know about the other.

    The lesson uses if/else because it makes the two cases visible. The same decision can be written with the conditional operator: bool firstWins = rating1 > rating2;, then string bestBrand = firstWins ? brand1 : brand2; and int bestRating = firstWins ? rating1 : rating2;. For the starter data this prints 1. MegaCorp (5), like the solution. The branch form is easier to extend when a case needs more than one statement.

    Step by step

    1. Write the if. Its condition is rating1 > rating2. Inside it, assign brand1 to bestBrand.
    2. Assign the rating. Still inside the if, assign rating1 to bestRating. Both values of the winner are set before leaving the branch.
    3. Add the else. Write it as } else { and assign brand2 to bestBrand. A tie lands here, because "not strictly better" includes "equal".
    4. Assign the second rating. Inside the else, assign rating2 to bestRating.
    5. Print the winner. After the else, send "1. ", bestBrand, " (", bestRating and ")" to cout, ending with endl. The variables hold the winner whichever branch ran.

    The solution in C++

    string brand1 = "TechPro";
    int rating1 = 4;
    string brand2 = "MegaCorp";
    int rating2 = 5;
    
    string bestBrand;
    int bestRating;
    if (rating1 > rating2) {
        bestBrand = brand1;
        bestRating = rating1;
    } else {
        bestBrand = brand2;
        bestRating = rating2;
    }
    cout << "1. " << bestBrand << " (" << bestRating << ")" << endl;
    

    Details worth a closer look:

    • Declared, not initialised. string bestBrand; is an empty string, but int bestRating; holds an unspecified value until it is assigned. Reading it before assigning is undefined behaviour. The code is safe because both branches of the if/else assign it.
    • Assignment, not declaration, in the branches. Writing string bestBrand = brand1; inside a branch would create a new variable that disappears at the closing brace, and the outer bestBrand would stay empty.
    • Printing with <<. In this version the line is not built as a string, so bestRating needs no to_string. Each << prints one value, whatever its type.

    Edge cases and common mistakes

    • Ties. With ratings 4 and 4 the condition 4 > 4 is false, so brand 2 is printed. Switching to >= would silently change the answer to brand 1 and the lesson's tests would catch it.
    • Setting only one variable. If a branch sets the brand but forgets the rating, the output pairs the right name with the wrong number, or with an unspecified value.
    • Mixing branches. Assigning brand1 together with rating2 prints a rating that does not belong to the brand.
    • Braces and the else line. The else belongs on the same line as the closing brace of the if body, in the form } else {. Putting a statement between the two, or ending the if with a semicolon, makes the else a syntax error.
    • Using two separate if statements. An if (rating1 > rating2) followed by an if (rating2 > rating1) leaves a tie with no branch at all, so bestBrand would stay empty and the output would be wrong. The else guarantees that one branch runs.
    • Comparing brand names. The condition compares the ratings, not the strings.

    Testing

    Running the same logic with other starter values gives:

    brand1, rating1brand2, rating2Output
    TechPro, 4MegaCorp, 51. MegaCorp (5)
    TechPro, 5MegaCorp, 41. TechPro (5)
    TechPro, 4MegaCorp, 41. MegaCorp (4)

    The last row shows the tie rule: the second brand is printed.

    Keep going with C++650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free