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
- Write the
if. Its condition israting1 > rating2. Inside it, assignbrand1tobestBrand. - Assign the rating. Still inside the
if, assignrating1tobestRating. Both values of the winner are set before leaving the branch. - Add the
else. Write it as} else {and assignbrand2tobestBrand. A tie lands here, because "not strictly better" includes "equal". - Assign the second rating. Inside the
else, assignrating2tobestRating. - Print the winner. After the
else, send"1. ",bestBrand," (",bestRatingand")"tocout, ending withendl. 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, butint bestRating;holds an unspecified value until it is assigned. Reading it before assigning is undefined behaviour. The code is safe because both branches of theif/elseassign 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 outerbestBrandwould stay empty. - Printing with
<<. In this version the line is not built as a string, sobestRatingneeds noto_string. Each<<prints one value, whatever its type.
Edge cases and common mistakes
- Ties. With ratings 4 and 4 the condition
4 > 4is 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
brand1together withrating2prints a rating that does not belong to the brand. - Braces and the
elseline. Theelsebelongs on the same line as the closing brace of theifbody, in the form} else {. Putting a statement between the two, or ending theifwith a semicolon, makes theelsea syntax error. - Using two separate
ifstatements. Anif (rating1 > rating2)followed by anif (rating2 > rating1)leaves a tie with no branch at all, sobestBrandwould stay empty and the output would be wrong. Theelseguarantees 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, rating1 | brand2, rating2 | Output |
|---|---|---|
| TechPro, 4 | MegaCorp, 5 | 1. MegaCorp (5) |
| TechPro, 5 | MegaCorp, 4 | 1. TechPro (5) |
| TechPro, 4 | MegaCorp, 4 | 1. MegaCorp (4) |
The last row shows the tie rule: the second brand is printed.
