Questions: The following code snippet contains an error. What is the error? ``` int cost = 0; cin >> cost; if ( cost > 100); cost = cost - 10; cout << "Discount cost: " << cost << endl; ```

The following code snippet contains an error. What is the error?

```
int cost = 0;
cin >> cost;
if ( cost > 100);

    cost = cost - 10;

cout << "Discount cost: " << cost << endl;
```
Transcript text: The following code snippet contains an error. What is the error? ``` int cost = 0; cin $3 cost; if ( cost > 100); r cost = cost - 10; } cout &< "Discount cost: " < cost << endl; ```
failed

Solution

failed
failed

Solution Steps

Step 1: Identify the code snippet

The given code snippet is:

int cost = 0;
cin >> cost;
if (cost > 100);
{
    cost = cost - 10;
}
cout << "Discount cost: " << cost << endl;
Step 2: Locate the error

The error is in the if statement. The semicolon (;) after the if condition terminates the if statement prematurely.

Step 3: Correct the error

Remove the semicolon after the if condition to ensure the block of code within the braces is executed only if the condition is true.

Final Answer

The corrected code snippet should be:

int cost = 0;
cin >> cost;
if (cost > 100)
{
    cost = cost - 10;
}
cout << "Discount cost: " << cost << endl;
Was this solution helpful?
failed
Unhelpful
failed
Helpful