4 Replies - 1800 Views - Last Post: 29 May 2010 - 08:22 AM Rate Topic: -----

#1 xenor  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 148
  • Joined: 23-September 09

How not to program in c++

Posted 29 May 2010 - 04:45 AM

I currently reading How Not to Program in C++: 111 Broken Programs and 3 Working Ones, or Why Does 2+2=5986 by Steve Oualline, it is a great book but i don't understand the problem on chapter 2 - Program 13.

The problem:
/***********************************************
* Billing -- Print out how much we owe         *
*      customers or they owe us.               *
************************************************/
#include <iostream>
// Number of pennies in a dollar
const int DOLLAR = 100;
/***********************************************
* billing -- do the billing.                   *
*      If the customer owes us money           *
*              -- output debt.                 *
*      If we owe more than $100                *
*              -- output credit.               *
*      Between $0 and $100 just ignore the     *
*              account.                        *
************************************************/
  int billing(const int balance)  // Current balance (in cents)
  {
     if (balance < 0)
          if (balance < - (100*DOLLAR))
              std::cout << "Credit " << -balance <<std:: endl;
      else
          std::cout << "Debt " << balance << std::endl;
 
     return (0);
 } 
int main()
{
    /* Test code */
    billing(50);
    billing(-10); 
    return (0);
}



And the solution.
Answer 31: The problem is that the else clause goes with the nearest if. The properly indented code looks like:

        23 if (balance < 0)
        24     if (balance < - (100*DOLLAR))
        25         cout << "Credit " << -balance << endl;
        26     else
        27         cout << "Debt " << balance << endl;

This is not what the programmer intented. What he wanted to do was:

        if (balance < 0) {
            if (balance < - (100*DOLLAR))
                cout << "Credit " << -balance << endl;
        } else
            cout << "Debt " << balance << endl;




I really don't understand what is the difference between "-balance" and "-(balance)".

Is This A Good Question/Topic? 0
  • +

Replies To: How not to program in c++

#2 moopet  Icon User is offline

  • binary decision maker
  • member icon

Reputation: 343
  • View blog
  • Posts: 1,189
  • Joined: 02-April 09

Re: How not to program in c++

Posted 29 May 2010 - 04:55 AM

View Postxenor, on 29 May 2010 - 10:45 AM, said:

I really don't understand what is the difference between "-balance" and "-(balance)".


This is not mentioned in the code you provided, so what do you actually mean?
Was This Post Helpful? 0
  • +
  • -

#3 rs4  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • Posts: 153
  • Joined: 01-February 09

Re: How not to program in c++

Posted 29 May 2010 - 05:15 AM

As above, but in answer to your question -balance and -(balance) are the same, the brackets are ignored by the compiler in this case. Or if you want to know the problem with the code, there were missing brackets {}.

This post has been edited by rs4: 29 May 2010 - 05:15 AM

Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 7056
  • View blog
  • Posts: 14,734
  • Joined: 16-October 07

Re: How not to program in c++

Posted 29 May 2010 - 06:44 AM

The real problem, and perhaps the point of the code ( I don't know), is if-then-else blocks. To this end, I ALWAYS use braces, even when you can get away without them.

So, here's the code offered, with proper curlies.
if (balance < 0) {
	if (balance < - (100*DOLLAR)) {
		std::cout << "Credit " << -balance <<std:: endl;
	} else {
		std::cout << "Debt " << balance << std::endl;
	}
}



The author is saying they meant to do this:
if (balance < 0) {
	if (balance < - (100*DOLLAR)) {
		std::cout << "Credit " << -balance <<std:: endl;
	}
} else {
	std::cout << "Debt " << balance << std::endl;
}



It's kind of an odd code block, since it misses a logical result response.
if (balance < 0) {
	if (balance < - (100*DOLLAR)) {
		std::cout << "Credit " << -balance <<std:: endl;
	} else {
		std::cout << "I'm so confused!" << std:: endl;
	}
} else {
	std::cout << "Debt " << balance << std::endl;
}



The lesson is, ALWAYS use braces, they are never optional.
Was This Post Helpful? 1
  • +
  • -

#5 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 84
  • View blog
  • Posts: 1,385
  • Joined: 28-March 09

Re: How not to program in c++

Posted 29 May 2010 - 08:22 AM

as for the -(balance) or -balance, think of it mathematically.

What does 2 - -2 mean? and what does 2 - (-2) mean? Is it the same result? I think the author is also trying to make you think in terms of properly presenting your code in an easily distinguishable form.

P.S.:and as for the example, I just put values in for balance. (just in case you didn't understand)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1