Start learning free

LESSON 90 OF 409 · CONDITIONAL STATEMENTS - CHALLENGES

Top K Brands V2 - Better of Two in C++

Practice

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

    Top K Brands V2 in C++

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