Hi,

I am trying to understand why this bit of code is performing the way that it is. I am a novice at C++ and missed this on my test. The teacher will not discuss anything from an exam so that leaves me to try and hash it out myself. So here is the code snippet:

cout<<"Enter in total widgets: ";
cin>>totWidgets;

   if (totWidgets > 250)
     {
        discAmt1 = 25;
        discAmt2 = 5;
      }
    else if (totWidgets > 150)
      {
        discAmt1 = 15;
        discAmt2 = 2.50
       }
      else
        discAmt1 = 5;
        discAmt2 = .5;

cout<<"Discount amount 1 is $ "<<discAmt1<<endl;
cout<<"Discount amount 2 is $ "<<discAmt2<<endl;

We were to assume that the user input 200 and we were to give the output but we couldn't compile and run it. So I answered it- Discount amount 1 is $15 and Discount amount 2 is $2.50. I was wrong and the only explaination was that discAmt2 will always be = to .5. I don't understand why. Could someone clear this up for me? Does it have something to do with the braces being gone off of the else? Any help would be appreciated being that I am sure these types of problems will come up again!

Recommended Answers

All 4 Replies

Does it have something to do with the braces being gone off of the else?

It has everything to do with this - you answered it yourself.
There are no braces associated with the "else" statement so that means that only the line immediately following the "else" statement is included in the statement. Therefore, the line "discAmt2 = .5", which is two lines down, will always be executed. This last line is completely outside of the if, else if, else statements.

>Does it have something to do with the braces being gone off of the else?
You got it in one. If you omit the braces, only the next statement will be a part of the block. If you add braces in the way that your compiler parses the code, it looks like this:

cout<<"Enter in total widgets: ";
cin>>totWidgets;

if (totWidgets > 250)
{
    discAmt1 = 25;
    discAmt2 = 5;
}
else if (totWidgets > 150)
{
    discAmt1 = 15;
    discAmt2 = 2.50
}
else
{
    discAmt1 = 5;
}

discAmt2 = .5;

cout<<"Discount amount 1 is $ "<<discAmt1<<endl;
cout<<"Discount amount 2 is $ "<<discAmt2<<endl;

As you can see, because totWidgets is greater than 150, but not greater than 250, discAmt1 is set to 15 and discAmt2 is set to 2.50. However, after the if..else construct, discAmt2 is always set to .5 because it's not part of the if..else construct.

Line 14 is an else statement. Typically anything you want associated with an if/else if/else statement or a for loop body or a while loop body should be enclosed in {}, like in the snippet for the if and else if. If there isn't a set of {} associated with the if, else if, else, for or while, then the body of the if, else if, else, for, or while will consist of only the first statement after the if, else if, else, for, or while. This information should allow you to look at the snippet and answer the question correctly.

I have to thank everyone for their fast response to this and wish that I had asked sooner. This has stressed me out for over two weeks! I understand know how the computer is viewing the code so that if a question like this comes up again I will know what is happening with the computer! Finally. Thanks again to all of you!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.