So, Ive created a UI in console. No questions as to why. But anyways, It used to be just a simple array, but I decided to make it with vectors both to improve my skill with them, and to make its size dynamic.

Dont mind the mess of code here.

//RPG Game
#include <iostream>
#include <windows.h>
#include <string>
#include <conio.h>
#include <vector>
#include <dos.h>
#include <ctime>
#include <iomanip>
#include <fstream>
using namespace std;
HANDLE  hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
static string selection;

void hL(string s){
	int highLight = 240;
	HANDLE  hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(hConsole, highLight);
	cout << s << setw(10);
	int nonHighLight = 15;
	SetConsoleTextAttribute(hConsole, nonHighLight);
}
void nHL(string s){
	int nonHighLight = 15;
	SetConsoleTextAttribute(hConsole, nonHighLight);
	cout << s << setw(10);
}
void bS(){
	int blankSpace = 0;
	SetConsoleTextAttribute(hConsole, blankSpace);
	cout <<	"		" << setw(5);
	SetConsoleTextAttribute(hConsole, 15);
}
void borderDown(){ //Stupid formatting due to angry compiler gods
	const char o = 209;
	cout << o << o << o << o << o << o << o << o << o << o << o << o << o << o << o << o << o << o
		<< o << o << o << o << o << o << o << o << o << o << o << o << o << o << o << o << o << o << o
		<< o << o << o << o << o << o << o << o << o << o << o << o << o << o << o << o << o << o << o
		<< o << o << o << o << o << o << o << o << o << o << o << o << o << o << o << o << o << o << endl;
		cout << "		";
}
void borderUp(){
	const char o = 207;
	cout << o << o << o << o << o << o << o << o << o << o << o << o << o << o << o << o << o << o
		<< o << o << o << o << o << o << o << o << o << o << o << o << o << o << o << o << o << o << o
		<< o << o << o << o << o << o << o << o << o << o << o << o << o << o << o << o << o << o << o
		<< o << o << o << o << o << o << o << o << o << o << o << o << o << o << o << o << o << o << endl;
}
void select(string s){
	hL(s);
	selection = s;
}

class Creature{
private:
	int statArr[6];
	int str;	int dex;	int con;
	int inte;	int wis; 	int cha;
	int ac;		int hp;		int xp;
	int fort;	int ref;	int wil;
	int ini;	int mel;	int ran;
	string name;
public:
	Creature(){
		rollStats();
		name = "Ciziten";
	}
	void rollStats()
	{
		int iniStat[4];
		int temp = 6;
		for(int i = 0; i < 6; i++){
		for(int q = 0; q < 4; q++)
		{
			unsigned int srand = time(0);
			iniStat[q] = rand() % 6;
			if(temp > iniStat[q]){temp = iniStat[q];}
		}
		statArr[i] = iniStat[0] + iniStat[1] + iniStat[2] + iniStat[3] - temp;
		}
	}

	void selectStat()
	{
		char a = 'a';
		for(int i = 0; a != ' ';)
		{
		if(a == 'a' || a == 'A')
		{
			statArr[i - 1];
		
			if(i - 1 < 0){i = 6;}
		}
		if(a == 'd' || a == 'D')
		{
			statArr[i + 1];
			if(i + 1 > 6){i = 0;}
		}
		cout << statArr[i];
		}
	}
		void setStats()
		{
		cout <<"Strength: <";
		selectStat();
	}

	void setName()
	{
		cin >> name;
		cin.clear();
	}
	void viewStats()
	{
		for(int i = 0; i < 6; i++){
			 cout << statArr[i]	<< endl;
		}
	}
};


int main()
{
	Creature player;
	string menuType;	
	int menu = 0;
	char a ='q';
	string hint;
	vector<vector<string>> menOp(2, vector<string> (2));
	int x = 0;
	int y = 0;
	int num = 0;
	string displayTextBefore = "";
	string displayTextAfter = "";
	for(bool endgame = 1; endgame != 0;)
	{
		switch(menu){
		case 0:
			menuType = "	|Main Menu|";
			menOp[0][0] = "Start Game";
			menOp[0][1] = "Game Options";
			menOp[1][0] = "Select Character";
			menOp[1][1] = "Exit";
			hint = "W, A, S, D are to move and space to select";
			break;
		case 1:
			menuType = "	|Options Menu|";
			menOp[0][0] = "Controls";
			menOp[0][1] = "Color Scheme";
			menOp[1][0] = "About";
			menOp[1][1] = "Back";
			hint = "Options are cool.";
			break;
		case 2:
			menuType = "	|Character Menu|";
			menOp[0][0] = "Select Character";
			menOp[0][1] = "Create Character";
			menOp[1][0] = "Character Guide";
			menOp[1][1] = "Back";
			hint = "Dont have time to make a character? Use a prebuilt.";
			break;
		case 3:
			menuType = "	|!!!|";
			menOp[0][0] = "Yes";
			menOp[0][1] = "No";
			 // FIND A WAY TO POPBACK THESE! (ASK QUESTIONS, GETTING ERROR!)
			hint = "Exit, if you must.";
			break;
		case 4:
			menuType = "	|Select a Character|";
		}
	for(char a = 'q'; a != ' ';)
	{
		system("cls");
		cout << x << "," << y;
		if(a == 'w' || a == 'W')
		{
			y += 1;
			if(y > (menOp.at(0).size() - 1)){y = 0;}
		} 
		if(a == 'a' || a == 'A')
		{
			x -= 1;
			if(x < 0){x = (menOp.size() - 1);}
		}
		if(a == 's' || a == 'S')
		{
			y -= 1;
			if(y < 0){y = (menOp.at(0).size() - 1);}
		}
		if(a == 'd' || a == 'D')
		{
			x += 1;
			if(x > (menOp.size() - 1)){x = 0;}
		}
		SetConsoleTextAttribute(hConsole, 15);
		cout << endl << endl;
		cout << displayTextBefore << endl << endl;
		cout << "			" << menuType << endl;
		borderDown();	
			for (int i = 0; i < menOp.size(); i++)
			{
				for (int j = 0; j < menOp.at(0).size(); j++)
				{
					if(y == i && x == j){
						select(menOp[i][j]);
					}
					else
						nHL(menOp[i][j]);
						bS();
				}
				cout << endl;
				bS();
			}
			cout << endl;

			borderUp();
			cout << "Hint: " << hint;
			cout << endl;
			cout << displayTextAfter << endl;
			
			a = getch();	
	}
		if(selection == "Start Game"){cout << "Game Started";
			system("pause");
		}
		if(selection == "Game Options"){menu = 1;}
			if(selection == "Back" && menu == 1){menu = 0;}
		if(selection == "Select Character"){menu = 2;}
			if(selection == "Create Character")
			{
				player.rollStats();
				displayTextAfter = "Here are the stats you rolled for your character: ";
				player.viewStats();
			}
			if(selection == "Back" && menu == 2){menu = 0;}
		if(selection == "Exit")
		{
			menu = 3;
			x = 0;
			y = 0;
				menOp.at(1).pop_back();
				menOp.at(1).pop_back();
		}
			if(selection == "Yes" && menu == 3){
				endgame = 0;
			}
			if(selection == "No" && menu == 3){
				menu = 0;
				menOp.at(1).push_back("DEFAULT");
				menOp.at(1).push_back("DEFAULT");
			}
	
	}	
	system("pause");
	return 0;
}

The error happens to be an out of bounds one. The vector shrinks to one dimension, yet the program tries to access the second dimension. My problem is I cant see where or why it tries to access the second dimension. This notably(and only at this point) shows up in the exit menu. The million dollar question would be, does anyone know why/where it's accessing this nonexistent dimension, even though I told it not to?

Also, for bonus points,

void viewStats()
	{
		for(int i = 0; i < 6; i++){
			 cout << statArr[i]	<< endl;
		}
	}

is my way of outputting the initial stat rolls for a character, but the only display method I really have is to display a string after each menu redraw. I tried adding each integer to the end of the string, but it didn't work. Any Ideas on how to display it?

Sorry for my shoddy wording of things, it doesn't seem to be my forte at the moment, but thanks for any input!

Recommended Answers

All 5 Replies

As a side issue, why didn't you define the border functions using a for() loop? It would be a lot easier to read and change, if you needed to:

const int LINE_LENGTH = 75;

// ...

void borderDown() 
{
    const char o = 209;

    for (int i = 0; i < LINE_LENGTH; i++)
        cout << o;
    cout << endl << "		";
}

void borderUp()
{
    const char o = 207;

    for (int i = 0; i < LINE_LENGTH; i++)
        cout << o;
    cout << endl;
}

I tested this and it appeared to work the same as the original, so why not do it this way to begin with?

Another side issue I notice while going through the code: you have the following line of code

unsigned int srand = time(0);

However, srand() is a function, which this variable is masking; the result is that the randomizer never actually gets initialized. Also, you only need call srand() once, at the beginning of the program.

With the border issue, when I tried it, it kept throwing errors. I don't remember what they were, but now it seems to work. I also fixed the seeding. Thank you very much. Is there any way to modify the original post so it doesnt draw further attention?

Alright, so i fixed my main problem.
The issue was that I didnt completely destroy the second row, which i fixed with a simple resize function. So then I had the issue of my controls inverting, so i inverted them(again). but I'm still stuck on the problem of converting a integer array into strings, then adding it to the end of a string.

Probably the easiest way to convert an integer to a string is by using a stringstream:

int five = 5;

string mystring;
stringstream ss;

ss << five;

ss >> mystring;

A slightly different take on this can be found here; which is better for you depends on what you find convenient.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.