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:
What does the += operator mean in:
gold += silver / SILVERPERGOLD;
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!