Code:
void work()
{
cout << "\n\nHow long would you like to work? (eight-hour maximum) ";
cin >> workHours;
pay = workHours * 100;
if (workHours > 8)
{
workHours = 8;
}
if (workHours >= 1)
{
cout << "\n\nYou worked for " << workHours
<< " hour(s) and earned " << pay << " gold piece(s)!\n\n";
gold += pay;
}
else
{
cout << "You did not work.\n\n";
}
}
A bit of a bug in there: you calculate pay before making sure that workHours is at most 8, so if I typed 100 I'd get paid for 100 hours, while appearing to only work for 8 hours.
Your updated code looks better modularized, but charCreation() could use some work, too. You have a lot of repeated code similar to this.
Code:
hitPoints = rand() % 50 + 50;
attack = rand() % 15 + 5;
defense = rand() % 15 + 5;
magic = rand() % 5 + 5;
accuracy = rand() % 5 + 10;
cout << "Name: " << charName << endl;
cout << "Class: Warrior\n";
cout << "HP: " << hitPoints << endl;
cout << "ATK: " << attack << endl;
cout << "DEF: " << defense << endl;
cout << "MGK: " << magic << endl;
cout << "ACC: " << accuracy << endl;
cout << "\nIs this information correct? (y/n) ";
cin >> recreate;
if (recreate != 'n' && recreate != 'y')
{
cout << "\nYou have entered an invalid character.";
recreate = 'n';
}
You could put that all into a function, passing in the relevant variables such as the 15 and 5 in "rand() % 15 + 5". That would probably result in a lot of arguments, but you could at least put the printing code into a function, as that changes very little.
Similarly for shop(). I'd like to introduce a little bit of what is probably new code as a suggestion to you . . . it might be food for thought, at least.
Code:
#include <iostream>
#include <iomanip>
struct player_t {
int gold;
};
struct weapon_t {
char *name;
int damage;
int speed;
int range;
};
void shop(player_t &player);
int main() {
// create a new instance of the player_t structure
player_t player;
player.gold = 100; // set the player's gold
shop(player);
return 0;
}
struct shop_item_t {
weapon_t weapon;
int cost;
};
void shop(player_t &player) {
// create an array of shop_item_t's, and initialize the elements right here
shop_item_t inventory[] = {
// Name, damage, speed, range, and then cost.
{{"Foil", 5, 12, 1}, 20},
{{"Rapier", 8, 10, 1}, 30},
{{"Short bow", 3, 8, 30}, 25},
{{"Long bow", 5, 7, 40}, 60}
};
for(;;) {
std::cout << "--- Shop contents:\n";
for(size_t x = 0; x < sizeof(inventory) / sizeof(inventory[0]); x ++) {
std::cout << (x + 1) << ") "
<< std::left << std::setw(20) << inventory[x].weapon.name
<< "\tDamage: " << std::setw(3) << inventory[x].weapon.damage
<< "\tSpeed: " << std::setw(3) << inventory[x].weapon.speed
<< "\tRange: " << std::setw(3) << inventory[x].weapon.range
<< "\n";
}
std::cout << "You have " << player.gold << " gold.\n";
std::cout << "Enter your choice (0 to quit): ";
int choice;
std::cin >> choice;
if(choice == 0) return;
choice --; // convert from 1-based counting to 0-based
if(choice >= 0 && choice < sizeof(inventory) / sizeof(inventory[0])) {
if(player.gold >= inventory[choice].cost) {
// not yet implemented: add the new item to the user's inventory
player.gold -= inventory[choice].cost;
std::cout << "You bought a " << inventory[choice].weapon.name
<< " for " << inventory[choice].cost << " gold.\n";
}
else {
std::cout << "You can't afford to buy that.\n";
}
}
else {
std::cout << "Invalid option.\n";
}
}
}
[Code coloured with codeform.]
As you can see, I've made use of structures and of arrays. (And arrays of structures.) You should really learn about those soon (or better yet, about classes); they can make you more productive and your code less repetitive.