hey,
I'm making a crappy little rpg text game. Anyway I set it up so that the player stats are all saved into a class. I have a function which creates the class when a new game is started. The problem is that I can only access the class functions through the function which I used to create it.
I realize this code is probably written very poorly. Anyway, the function that you should worry about is newGame();Code:#include <stdio.h> #include <iostream> #include <fstream> #include <string> #include <stdlib.h> #include <time.h> #include <string> class character{ public: int health; int strength; int armor; string name; character(int, int, int, string); ~character(); void viewStats(); string saveStats(); }; character::character(int h, int s, int a, string n){ health = h; strength = s; armor = a; name = n; } character::~character(){ } void character::viewStats(){ cout << "\n"; cout << "Name: " << name << endl; cout << "Health: " << health << endl; cout << "Strength: " << strength << endl; cout << "Armor: " << armor << endl; } string character::saveStats(){ string send; send += name + "#"; send += health + "#"; send += strength + "#"; send += armor + "#"; return send; } int GetRand(int min, int max) { static int Init = 0; int rc; if (Init == 0) { srand(time(NULL)); Init = 1; } rc = (rand() % (max - min + 1) + min); return (rc); } void newGame(){ string temp; int a, b, c; cout << "\nName:"; cin.get(); getline(cin, temp, '\n'); again: a = GetRand(10, 15); b = GetRand(10, 15); c = GetRand(50, 70); character player(c, a, b, temp); player.viewStats(); cout << "\n\nNew Stats (Y\\N)\n\n"; char choice; cin >> choice; if(choice == 'y' || choice == 'Y'){ goto again; } } void clear(){ for (int i =0; i < 25; i++){ cout << "\n"; } } void sort(string command){ int which; which = command.find("\\help", 0); if(which == 0){ cout << "Help menu!"; return; } which = command.find("\\save", 0); if(which == 0){ cout << player.saveStats(); } } int main(int argc, char *argv[]) { int choose; string command; cout << "Menu:\n\n1.New Game\n2.Load\n3.Quit\n\n"; cin >> choose; switch (choose) { case 1: newGame(); break; } clear(); cout << "Welcome to this crappy RPG game. To see the commands type \\? or \\help\n\n"; cout << "It\'s recommended that you save. You can save at any time by typing \\save\n\n"; cin.get(); getline(cin, command, '\n'); sort(command); cin >> choose; return 0; }
Any help would be apreciated thanks



LinkBack URL
About LinkBacks


