1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
double getNum();
int getInt();
int getIntInRange(int low, int high);
int levelUp(string abilities[], const int SIZE);
void printOptions(string options[], int SIZE);
void printStats(string abilities[], int playerStats[], const int SIZE);
int getLongestSrting(string str[],const int SIZE);
void printWelcome();
int main(){
const int SIZE_ABILITIES = 4, SIZE_OPTIONS = 3;
string options[SIZE_OPTIONS] = {"Print Stats", "Level Up", "Quit"};
string abilities[SIZE_ABILITIES] = {"Strength", "Intelligence", "Dexterity", "Charisma"};
int playerStats[SIZE_ABILITIES] = {15, 14, 17, 12};
int choice;
printWelcome();
do {
printOptions(options, SIZE_OPTIONS);
cout << "Please make a choice: ";
choice = getIntInRange(1, SIZE_OPTIONS);
cout << endl;
switch (choice){
case 1: printStats(abilities, playerStats, SIZE_ABILITIES); break;
case 2: playerStats[levelUp(abilities, SIZE_ABILITIES) - 1]++; break;
case 3: break;
default: cout << "Invalid Choice"; // Though you never get here
}
cout << endl;
} while (choice != SIZE_OPTIONS);
cout << "Thanks for playing. Hit enter to quit."; cin.get();
return 0;
}
void printWelcome(){
cout << "\t\tWelcome to my game :)\n\n";
}
double getNum(){
double num;
while (!(cin >> num)){
cin.clear(); cin.ignore(80, '\n');
cout << "Numbers Only. Try Again: ";
}
cin.ignore(80,'\n');
return num;
}
int getInt(){
double num;
do {
num = getNum();
if (num != static_cast<int>(num)) cout << "Integers Only. Try again: ";
} while (num != static_cast<int>(num));
return static_cast<int>(num);
}
int getIntInRange(int low, int high){
int num;
do {
num = getInt();
if (num < low || num > high) cout << "Not a Valid Choice. Try Again: ";
}
while (num < low || num > high);
return num;
}
int levelUp(string abilities[], const int SIZE){
cout << "You can chose 1 out of these traits:" << endl;
for (int i = 0; i < SIZE; i++) cout << i + 1 << ": " << abilities[i] << endl;
cout << "Now type the number of your choice:";
return getIntInRange(1, SIZE);
}
void printOptions(string options[], int SIZE){
cout << "Your Options:" << endl;
for (int i =0;i <SIZE; i++) cout << i + 1 << ": " << options[i] << endl;
}
void printStats(string abilities[], int playerStats[], const int SIZE){
int longest = getLongestSrting(abilities, SIZE);
cout << "Player Stats:" << endl;
for (int i = 0; i < SIZE; i++) cout << left << setw(abilities[longest].length() + 1) << abilities[i] << ": " << right << playerStats[i] << endl;
}
int getLongestSrting(string str[],const int SIZE){
int longest = 0;
for (int i = 0; i < SIZE; i++)
if (str[i].length() >= str[longest].length()) longest = i;
return longest;
}
|