-3

I'm a complete newb at programming and have had no experience of programming before. I bought a book called C++ Programming for the Absolute Beginner by Mark Lee (not advertising or anything) and at the end of Lesson 2 (which shows of variables and forgets to explain casting) they give you this game:

#include <iostream>
#include <string>

int main() {
    using std::cout;
    using std::cin;
    using std::string;
    string name;
    cout << "Welcome to the weapon store, noble knight."
         << " Come to equip the army again?\n"
         << "What is your name? ";
    cin >> name;
    cout << "Well then, Sir " << name.c_str()
         << ", let's get shopping\n";
    double gold = 50.0;
    int silver = 8;
    const double SILVERPERGOLD = 6.7;
    const double BROADSWORDCOST = 3.6;
    unsigned short broadswords;
    cout << "You have " << gold << " gold pieces and "
         << silver << " silver." << "\nThat is equal to ";
    gold += silver / SILVERPERGOLD;
    cout << gold << " gold.\n";
    cout << "How many broadswords would you like to buy?"
         << " (3.6) gold each ";
    cin >> broadswords;
    gold = gold - broadswords * BROADSWORDCOST;
    cout << "\nThank you. You have " << gold << " left.\n";
    silver = (int)((gold - (int)gold)) * SILVERPERGOLD;
    gold = (double)((int)(gold));
    cout << "That is equal to " << gold << " gold and "
         << silver << " silver. \n"
         << "Thank you for shopping at the Weapon Store. "
          << "Have a nice day, Sir " << name.c_str() << ".\n";
   system("pause");
   return 0;
}

I have some questions with this code:

  1. What does the += operator mean in:

    gold += silver / SILVERPERGOLD;

  2. What does the following mean? I am clueless in what type casting is.

    silver = (int)((gold - (int)gold)) * SILVERPERGOLD; gold = (double)((int)(gold));

Please do not hate me for being a noob and please explain it in a way a newb would understand. Thanks!

nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • Silver is cast to a double so that it equals 8.0 – axelduch Sep 06 '14 at 23:57
  • 11
    "`cout << name.c_str()`" and "`(int)gold`"? Throw this book away and [get a real one](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Baum mit Augen Sep 06 '14 at 23:58
  • @BaummitAugen Sorry mate but no. – Sir William of Orange Sep 07 '14 at 00:14
  • 5
    @SirWilliamofOrange this book is already teaching you bad habits, such as C style casts. You should take the advice. – OMGtechy Sep 07 '14 at 00:37
  • 2
    As [STL says](http://nuwen.net/gcc.html), bad programmers are your main enemy. The write books and blogs and post on the internet, and they outnumber good programmers by a large margin. – Kerrek SB Sep 07 '14 at 00:41
  • @OMGtechy Well I already bought the book. Can't buy another one. So any advice on what is the correct way of doing that program? – Sir William of Orange Sep 07 '14 at 00:46
  • @OMGtechy What I did is instead of using "using std::cout" in main() I just use: using namespace std; after #include. I took out c_str(). I still don't understand C Casting because I don't understand what it means in this program. – Sir William of Orange Sep 07 '14 at 00:58
  • Well I know this sucks, but if you want to learn C++ you should not use this book. – n. m. could be an AI Sep 07 '14 at 03:43
  • @SirWilliamofOrange: I have to second the book issue. Go exchange the book for something on the [beginner's C++ book list](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) curated by members of the C++ community on Stack Overflow. It will be far less frustrating in the long run. – In silico Sep 07 '14 at 04:35

1 Answers1

1
gold += silver / SILVERPERGOLD

+= means "increment the variable on the left side of += by the amount on the right side".

silver = (int)((gold - (int)gold)) * SILVERPERGOLD; 
gold = (double)((int)(gold));

This is a really, really, really wrong way to do a floating point remainder calculation.

Type castiing is another name for explicit type conversion.

(double)x

means "take the value of x and return 'the same' value but of type double". If x is 7, the result will be 7.0. It is a long obsolete way to do a type cast in C++. Google "c style casts" for more info.

Correspondingly,

(int)x

means "take x and return 'the same' value as int". If x is 7.83, the result is 7 (i.e. the fractional part is dropped).

So ((gold - (int)gold)) would mean "subtract the whole part from gold, leaving the fractional part". Then the author multiplies the result by the gold-to-silver conversion rate, and rounds it down to a whole number. This presumably gives us the amount of change in silver pieces. Finally, with gold = (double)((int)(gold)) the author rounds the amount of gold down to a whole number. The fractional part is already converted to silver, so the two numbers, gold and silver, together comprise the sum of money you have.

The entire operation tries to fit a whole number of gold pieces into the price, and make up the rest with silver. Never do it this way.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
  • "take the value of x and treat it as having type double". - no, it means to convert the value of `x` to type `double`. – M.M Sep 07 '14 at 04:28
  • @MattMcNabb this is correct but doesn't explain much. "Cast" and "convert" are synonymous, explaining one with the other doesn't go very far. – n. m. could be an AI Sep 07 '14 at 04:53
  • @nm They aren't synonymous. *Cast* means an explicit conversion; there are also non-cast conversions (aka. implicit conversions). Although I take your point that saying "A cast converts the value" is actually not saying much. I'll rephrase: I think "treats the value as" is not a good description; it's reminiscent of what happens when you cast `int *` to `char *`, that is normally described as "treating the int as a series of characters". When converting `double` to `int` for example, the value may change; you lose information. It produces a new value - not a view of the old value. – M.M Sep 07 '14 at 04:56
  • 1
    @MattMcNabb I have changed the wording somewhat. – n. m. could be an AI Sep 07 '14 at 05:00
  • @n.m. Thanks a lot I've researched a little bit and have erased some bad habits that the author shows me. Thanks! – Sir William of Orange Sep 07 '14 at 11:59