Jump to content
  • Advertisement
Sign in to follow this  
lordoftools

Having trouble debugging this myself

This topic is 4854 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi im writeing a game in which it reads a .dat file for the saved game info called 'profiles.dat' i made three functions to go through the process of saving this data one for setting variables (not needed for this thread), one for checking to see if the profile has been saved before, and one to pick the profiles.dat file apart to overwrite or append the new game to it. I have pasted the two functions in below but the one im having trouble with is void SaveGame(). I am getting an infinate loop when trying to overwritten a saved game rather than appending. The append works fine and if you paste this code directly into MSVC++ (which im using) the loop in question is between 103-116. Beyond that there maybe problems but until i can fix this loop i can't continue. The purpose of the loop is to store all the data up until the profile in question appears then there is another process which will save teh data after the file, and another to write the first chunk of data, the new data, then the second chunk.
[source code=cpp]
//checks to see if the use already has a profile by that name saved
bool CheckSaved()
{
	string Data=strPath + "\\profiles.dat", strTemp;
	ifstream inFile(Data.c_str());

	while (inFile >> Data)
	{
		strTemp="profile.name=" + profile.Name;
		if (Data==strTemp)
		{
			inFile.close();
			return(true);
		}
	};
	
	inFile.close();
	return (false);
}

//rewrites the files needed for saving
void SaveGame()
{
	bool Saved;
	string Data=strPath + "\\profiles.dat", strOldData[100], strOldData2[100], strTemp;
	char chrLine[2000];
	int cnt=0;

	//empty strOldData incase this isn't the first save
	for (i=0; i<=99; i++)
		strOldData=" ";

	Saved=CheckSaved();

	if (Saved==false)
	{
		fstream inFile(Data.c_str(), ios::in);

		//store information in file for reprint
		while (! inFile.eof())
		{
			inFile.getline(chrLine,2000);
			
			//remove extra space
			for (i=0; i<=1999; i++)
				chrLine=chrLine[i+1];

			//convert to string array
			for (i=0; i<=1999; i++)
				strOldData[cnt]=strOldData[cnt]+chrLine;

			cnt++;
		};
		inFile.close();
		
		//rewrite the old info
		cnt=1;
		fstream outFile(Data.c_str(), ios::out);
		outFile<<strOldData[0];
		while (!(strOldData[cnt]==" "))
		{
			outFile<<endl<<strOldData[cnt];
			cnt++;
		};

		//write the new info
		outFile<<endl<<endl;
		outFile<<" profile.name="<<profile.Name<<endl;
		outFile<<" profile.gun="<<profile.Gun<<endl;
		outFile<<" profile.location="<<profile.Location<<endl;
		outFile<<" profile.cash="<<profile.Cash<<endl;
		outFile<<" profile.debt="<<profile.Debt<<endl;
		outFile<<" profile.bank="<<profile.Bank<<endl;
		outFile<<" profile.hentchmen="<<profile.Hentchmen<<endl;
		outFile<<" profile.health="<<profile.Health<<endl;
		outFile<<" profile.space="<<profile.Space<<endl;
		outFile<<" profile.day="<<profile.Day<<endl;
		outFile<<" loc.name="<<loc.Name<<endl;
		outFile<<" loc.police="<<loc.Police<<endl;
		outFile<<" loc.mindrugs="<<loc.minDrugs<<endl;
		outFile<<" loc.maxdrugs="<<loc.maxDrugs<<endl;	
		
		outFile.close();
		
	}
	else
	{
		//open file
		fstream inFile(Data.c_str(), ios::in);

		//store information in file before the saved game for reprint
		cnt=0;
		inFile.getline(chrLine,2000);

		//remove extra space
		for (i=0; i<=1999; i++)
			chrLine=chrLine[i+1];

		//convert to string array
		for (i=0; i<=1999; i++)
			strOldData[cnt]=strOldData[cnt]+chrLine;

		while (!(strOldData[cnt]==("profile.name=" + profile.Name)))
		{
			inFile.getline(chrLine,2000);
			
			//remove extra space
			for (i=0; i<=1999; i++)
				chrLine=chrLine[i+1];

			//convert to string array
			for (i=0; i<=1999; i++)
				strOldData[cnt]=strOldData[cnt]+chrLine;

			cnt++;
		};

		if (strOldData[1]==" ")
			strOldData[0]=" ";

		//store information in file after the saved game for reprint
		cnt=0;
		inFile.getline(chrLine,2000);

		//remove extra space
		for (i=0; i<=1999; i++)
			chrLine=chrLine[i+1];

		//convert to string array
		for (i=0; i<=1999; i++)
			strTemp=strTemp+chrLine;

		//scan until it finds the line with the profile
		while (!(strTemp==("profile.name=" + profile.Name)))
		{
			inFile.getline(chrLine,2000);
			
			//remove extra space
			for (i=0; i<=1999; i++)
				chrLine=chrLine[i+1];

			//convert to string array
			for (i=0; i<=1999; i++)
				strTemp=strTemp+chrLine;
		};

		//scan until it finds the line with the last stat in the saved game [loc.maxdrugs=12]
		while (!(strTemp=="loc.maxdrugs=12"))
		{
			inFile.getline(chrLine,2000);
			
			//remove extra space
			for (i=0; i<=1999; i++)
				chrLine=chrLine[i+1];

			//convert to string array
			for (i=0; i<=1999; i++)
				strTemp=strTemp+chrLine;

		};

		//store the rest of the file
		while (!(inFile.eof()))
		{
			inFile.getline(chrLine,2000);
			
			//remove extra space
			for (i=0; i<=1999; i++)
				chrLine=chrLine[i+1];

			//convert to string array
			for (i=0; i<=1999; i++)
				strOldData2[cnt]=strOldData2[cnt]+chrLine;

			cnt++;
		};

		if (strOldData2[1]==" ")
			strOldData2[0]=" ";

		inFile.close();
		
		//rewrite the begining old info [1]
		cnt=1;
		fstream outFile(Data.c_str(), ios::out);
		outFile<<strOldData[0];
		while (!(strOldData[cnt]==" "))
		{
			outFile<<endl<<strOldData[cnt];
			cnt++;
		};

		//write new saved data
		//outFile<<endl<<endl;
		outFile<<" profile.name="<<profile.Name<<endl;
		outFile<<" profile.gun="<<profile.Gun<<endl;
		outFile<<" profile.location="<<profile.Location<<endl;
		outFile<<" profile.cash="<<profile.Cash<<endl;
		outFile<<" profile.debt="<<profile.Debt<<endl;
		outFile<<" profile.bank="<<profile.Bank<<endl;
		outFile<<" profile.hentchmen="<<profile.Hentchmen<<endl;
		outFile<<" profile.health="<<profile.Health<<endl;
		outFile<<" profile.space="<<profile.Space<<endl;
		outFile<<" profile.day="<<profile.Day<<endl;
		outFile<<" loc.name="<<loc.Name<<endl;
		outFile<<" loc.police="<<loc.Police<<endl;
		outFile<<" loc.mindrugs="<<loc.minDrugs<<endl;
		outFile<<" loc.maxdrugs="<<loc.maxDrugs<<endl;	

		//rewrite the end old info [1]
		cnt=1;
		outFile<<strOldData2[0];
		while (!(strOldData2[cnt]==" "))
		{
			outFile<<endl<<strOldData2[cnt];
			cnt++;
		};

		outFile.close();
	
	}
}

I apologize if this turns out to be some simple error that i should have been able to fix but im still a bit green. -Lordoftools

Share this post


Link to post
Share on other sites
Advertisement
Well, the code that you have posted is not easily debuggable without the code that it references (e.g. profile.Name). I can say, however, the you might want to include a final condition to your while loop, such as while( !( strOldData[cnt] == ( "profile.name=" + profile.Name ) ) && cnt < 100 ) because, as far as I can see, you could go through the array of strings and not find a match.

[edit] You should get familiar with your system's/IDE's debugger and create a simple test of what you think the app should be doing in that loop and see what really happens. You may be surprised.


jfl.

Share this post


Link to post
Share on other sites
profile.name which it's refering is a string and the func i posted 'CheckSaved()' checks to see if that string is in teh file or not. i know that it is in the file or it would proform the first condition to the if statment in SaveGame().

if it helps here's all the code

[source code=cpp]
//for VC++
#include "stdafx.h"

//for basic functions i.e. (cout,cin)
#include <iostream>
#include <afx.h>

//for file manipulation
#include <fstream>
#include <cstdlib>

//For getch()/getche()
#include <conio.h>

//For srand() and time()
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

//for the string convertions
#include <sstream>
#include <cstring>
#include <stdexcept>

using namespace std;

//Global Variable Declaration
struct USERDATA
{

string Name, Gun, Location;
int Cash, Debt, Bank, Hentchmen, Health, Space, Day;

};
struct LOCDATA
{

string Name;
int Police, minDrugs, maxDrugs;

};
int i;
struct LOCDATA loc;
struct USERDATA profile;
enum Drugs {ACID=1, COCAINE, HASHISH, HERION, LUDES, MDA, OPIUM, PCP, PEYOTE, SHROOMS, SPEED, WEED};
enum LOCS {BRONX=1, GHETTO, CENTRALPARK, MANHATTAN, CONEYISLAND, BROOKLYN, QUEENS, STATENISLAND};
WORD wText,
wBkg,
wOldColors;
int intDrugArray[12];
string cstrDrugArray[12];
int intInvArray[12];
string strInvArray[12];
string strPath;

//function delcarations
WORD SetConsoleColors(HANDLE hConsole, WORD wColorText, WORD wColorBkg);
WORD GetConsoleColors(HANDLE hConsole);
wstring get_module_path_wide(void);
string wsts(const wstring &src_string);
int MainMenu();
string ProfileMenu();
inline std::string stringify(int x);
string BufferStr(string strString, int MAX_LEN);
void BuyingDrugs(string Drug, int Price);
void PrintStats();
void CityOperations(bool NewDay);
void BankMenu();
void SellDrugsMenu();
void BuyDrugsMenu();
void ChangeLocMenu();
void LoanSharkMenu();
void SaveGame();
bool CheckSaved();

//Start off function
int main(int argc, char* argv[])
{

//Color change sample: (ranges 0-15) [uncommented part fo global variable declare]
/*wText = 1;
wBkg = 5;
wOldColors;
HANDLE hConsole = CreateFile("CONOUT$", GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
OPEN_EXISTING, 0, 0);
wOldColors = SetConsoleColors(hConsole, wText, wBkg);
cout << "Colors in the console!" << endl << endl;
SetConsoleColors(hConsole, wOldColors, 0);*/


//Initialize random generator
srand ( time(NULL) );

//Initialize global variables
intInvArray[250]=0;
strPath=wsts(get_module_path_wide());

//Function level Delcarations
string cstrProfile;
string strTemp;
int intMnuOpt;

//Displays Main Menu
intMnuOpt=MainMenu();

//ERROR code
if (intMnuOpt==0)
{
system("cls");
cout<<"int MainMenu() returned ERROR code '0'\n";
}

//New Game code
if (intMnuOpt==1)
{
//Variable initalization.
profile.Bank=0;
profile.Health=100;
profile.Day=1;
profile.Cash=rand()%3000 + 2000;
profile.Debt=rand()%9000 + 1000;
profile.Gun="NONE";
profile.Location="BRONX";
profile.Hentchmen=rand()%4 + 7;
profile.Space=profile.Hentchmen * 10;
loc.maxDrugs=12;

//Set user name.
system("cls");
cout<<"Enter your name: ";
cin>>strTemp;
profile.Name=strTemp.c_str();

//Starts off in BRONX
loc.Name="BRONX";
loc.minDrugs=7;
loc.Police=10;
CityOperations(false);

}

//Load Profile code
if (intMnuOpt==2)
{
//displays menu, prompts profile choice, stores the user ID
cstrProfile=ProfileMenu();

//ERROR checking
if (cstrProfile=="NULL")
{
system("cls");
cout<<"string ProfileMenu() returned ERROR code 'NULL'\n";
}

//Profile Contence:
//see USERDATA struct (variable - profile)
//use ascii values of each digit to prevent easy editing

//temp debugger code
system("cls");
cout<<"Profile: "<<cstrProfile<<" was selected.\n";

}

//Quit Code
if (intMnuOpt==3)
{
system("cls");
cout<<"Thank you for playing \"Drug Wars\" -- By: Derik Hammer.";
getch();
return 0;
}

//Application Terminator
return 0;
}

//Operates the features of this location
void CityOperations(bool NewDay)
{

if (NewDay==true)
{
//increase the day
profile.Day++;

//increase debt due to interest
if (profile.Day%7==0)
profile.Debt=profile.Debt+(profile.Debt*.12);

}

system("cls");
PrintStats();

//sets up the drugs which are in this city (price, how many, what kind)
int intTemp, cnt=0, DrugChecker;
bool PickAgain;
int intChoice;
HANDLE hConsole = CreateFile("CONOUT$", GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
OPEN_EXISTING, 0, 0);

intTemp=rand()%(loc.maxDrugs-loc.minDrugs)+loc.minDrugs;

while (intTemp-1>=cnt)
{

do
{
PickAgain=false;
DrugChecker=rand()%12+1;
for (i=0; i<=11; i++)
{
if (intDrugArray==DrugChecker)
PickAgain=true;
}
}
while (PickAgain==true);
intDrugArray[cnt]=DrugChecker;
cnt++;

};

for (i=intTemp; i<=11; i++)
intDrugArray=0;

//Converts the int array to a string with the drug names and then makes the int array
//into the prices array at the same time.
for (i=0; i<=11; i++)
{

switch (intDrugArray)
{

case ACID:
cstrDrugArray="ACID";
intDrugArray=rand()%3400+1000;
break;

case COCAINE:
cstrDrugArray="COCAINE";
intDrugArray=rand()%14000+15000;
break;

case HASHISH:
cstrDrugArray="HASHISH";
intDrugArray=rand()%800+480;
break;

case HERION:
cstrDrugArray="HERION";
intDrugArray=rand()%7500+5500;
break;

case LUDES:
cstrDrugArray="LUDES";
intDrugArray=rand()%49+11;
break;

case MDA:
cstrDrugArray="MDA (Ecstasy)";
intDrugArray=rand()%2900+1500;
break;

case OPIUM:
cstrDrugArray="OPIUM";
intDrugArray=rand()%710+540;
break;

case PCP:
cstrDrugArray="PCP";
intDrugArray=rand()%1500+1000;
break;

case PEYOTE:
cstrDrugArray="PEYOTE";
intDrugArray=rand()%480+220;
break;

case SHROOMS:
cstrDrugArray="SHROOMS";
intDrugArray=rand()%670+630;
break;

case SPEED:
cstrDrugArray="SPEED";
intDrugArray=rand()%160+90;
break;

case WEED:
cstrDrugArray="WEED";
intDrugArray=rand()%575+315;
break;

default:
cstrDrugArray="ERROR: NO_DRUG";
break;
}

}

//Run cops coding here, seperate function for the battle run only option when un-armed
if (NewDay==true)
{



}

//print city menu
//make stats 'GREEN' (10) with 'blk bg' (0) [menu std color]
SetConsoleColors(hConsole, 10, 0);

cout<<"Welcome to "<<loc.Name<<"!\n";

//if its the home city [BRONX] then display spec menu
if (loc.Name=="BRONX")
{

specPickAgain:
cout<<"1. Check out available Drugs.\n";
cout<<"2. Sell Drugs.\n";
cout<<"3. Change location.\n";
cout<<"4. Visit Bank.\n";
cout<<"5. Visit Loan Shark.\n";
cout<<"6. Save Game.\n";
cout<<"7. Quit.\n";
cout<<"Selection: ";
//await choice
cin>>intChoice;

switch (intChoice)
{

case 1:
BuyDrugsMenu();
break;

case 2:
SellDrugsMenu();
break;

case 3:
ChangeLocMenu();
break;

case 4:
BankMenu();
break;

case 5:
LoanSharkMenu();
break;

case 6:
SaveGame();
break;

case 7:
break;

default:
goto specPickAgain;
break;

}

}
else
{

normPickAgain:
cout<<"1. Check out available Drugs.\n";
cout<<"2. Sell Drugs.\n";
cout<<"3. Change location.\n";
cout<<"Selection: ";
//await choice
cin>>intChoice;

switch (intChoice)
{

case 1:
BuyDrugsMenu();
break;

case 2:
SellDrugsMenu();
break;

case 3:
ChangeLocMenu();
break;

default:
system("cls");
PrintStats();
//make stats 'GREEN' (10) with 'blk bg' (0) [menu std color]
SetConsoleColors(hConsole, 10, 0);
cout<<"Invalid choice, please pick again.\n";
goto normPickAgain;
break;

}
}

//revert to old colors
SetConsoleColors(hConsole, 15, 0);

}

void BankMenu()
{

//function level delcarations
char chrChoice;
int intAmt;
bool boolDep=false, boolWithd=false;

//reset screen and disp stats again
system("cls");
PrintStats();

//print bank options
//make stats 'GREEN' (10) with 'blk bg' (0) [menu std color]
HANDLE hConsole = CreateFile("CONOUT$", GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
OPEN_EXISTING, 0, 0);
SetConsoleColors(hConsole, 10, 0);

cout<<"Welcome to the Bank!\n\n";

//check to see what he can do in the bank
if (profile.Cash>0)
{
cout<<"D. Deposit (Up to $"<<profile.Cash<<")\n";
boolDep=true;
}
if (profile.Bank>0)
{
cout<<"W. Withdraw (Up to $"<<profile.Bank<<")\n";
boolWithd=true;
}
cout<<"L. Leave Bank.\n";
//await choice
cout<<"Selection: ";
cin>>chrChoice;

if ((chrChoice=='d' || chrChoice=='D') && boolDep==true)
{

//disp deposit menu
system("cls");
PrintStats();
SetConsoleColors(hConsole, 10, 0);
DepAgain:
cout<<"Bank Deposit -- $"<<profile.Cash<<" available.\n\n";
cout<<"Deposit Amount (0 to cancel): ";
//await choice
cin>>intAmt;
if (intAmt>profile.Cash || intAmt<0)
{
system("cls");
PrintStats();
SetConsoleColors(hConsole, 10, 0);
cout<<"Invalid $ amount please pick again.\n\n";
goto DepAgain;
}
//alter $ amounts accordingly
profile.Cash=profile.Cash-intAmt;
profile.Bank=profile.Bank+intAmt;


}
else if ((chrChoice=='w' || chrChoice=='W') && boolWithd==true)
{

//disp withdraw menu
system("cls");
PrintStats();
SetConsoleColors(hConsole, 10, 0);
WDrawAgain:
cout<<"Bank Withdraw -- $"<<profile.Bank<<" available.\n\n";
cout<<"Withdraw Amount (0 to cancel): ";
//await choice
cin>>intAmt;
if (intAmt>profile.Bank || intAmt<0)
{
system("cls");
PrintStats();
SetConsoleColors(hConsole, 10, 0);
cout<<"Invalid $ amount please pick again.\n\n";
goto WDrawAgain;
}
//alter $ amounts accordingly
profile.Bank=profile.Bank-intAmt;
profile.Cash=profile.Cash+intAmt;

}
else if (chrChoice=='l' || chrChoice=='L')
{

//return to your city
CityOperations(false);

}
else
BankMenu();

CityOperations(false);

//revert to old colors
SetConsoleColors(hConsole, 15, 0);

}
void SellDrugsMenu()
{

//debugger code
cout<<"debugger code: in void SellDrugsMenu()\n";

}

void BuyingDrugs(string Drug, int Price)
{
//Function level Declarations
HANDLE hConsole = CreateFile("CONOUT$", GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
OPEN_EXISTING, 0, 0);
int intAmt;

//print menu
system("cls");
PrintStats();
//make stats 'GREEN' (10) with 'blk bg' (0) [menu std color]
SetConsoleColors(hConsole, 10, 0);
cout<<"How much "<<Drug<<" you want? ($"<<Price<<" each)"<<endl;
cout<<"Quantity (0 to cancel): ";
cin>>intAmt;

while ((intAmt*Price>profile.Cash) || (intAmt>profile.Space))
{
//print menu
system("cls");
PrintStats();
//make stats 'GREEN' (10) with 'blk bg' (0) [menu std color]
SetConsoleColors(hConsole, 10, 0);
cout<<"You don't have enough money or enough space for that much "<<Drug<<"!\n\n";
cout<<"How much "<<Drug<<" you want? ($"<<Price<<" each)"<<endl;
cout<<"Quantity (0 to cancel): ";
cin>>intAmt;
}

//updates the space in your inv
profile.Space=profile.Space-intAmt;

//fills the drug into the available array slot
for (i=0; i<=11; i++)
{
if (intInvArray==0)
{
strInvArray=Drug;
intInvArray=Price;
i=12;
}
}

//reverts back to old colors
SetConsoleColors(hConsole, 10, 0);

BuyDrugsMenu();

}

void CheckWhichDrug(char chrDrug, int intTemp[12])
{
//Function level Declarations
HANDLE hConsole = CreateFile("CONOUT$", GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
OPEN_EXISTING, 0, 0);
int intChoice;

RePick:
system("cls");
PrintStats();
//make stats 'GREEN' (10) with 'blk bg' (0) [menu std color]
SetConsoleColors(hConsole, 10, 0);

switch (chrDrug)
{
case 'h':
cout<<"Did you want HASHISH or HERION?\n\n";
cout<<"1. HASHISH.\n";
cout<<"2. HERION.\n";
cout<<"Selection (0 to cancel):";
//await choice
cin>>intChoice;

switch (intChoice)
{
case 0:
BuyDrugsMenu();
case 1:
BuyingDrugs("HASHISH", intTemp[3]);
break;
case 2:
BuyingDrugs("HERION", intTemp[4]);
break;
default:
goto RePick;
break;
}
break;

case 'p':
cout<<"Did you want PCP or PEYOTE?\n\n";
cout<<"1. PCP.\n";
cout<<"2. PEYOTE.\n";
cout<<"Selection (0 to cancel):";
//await choice
cin>>intChoice;

switch (intChoice)
{
case 0:
BuyDrugsMenu();
case 1:
BuyingDrugs("PCP", intTemp[8]);
break;
case 2:
BuyingDrugs("PEYOTE", intTemp[9]);
break;
default:
goto RePick;
break;
}
break;

case 's':
cout<<"Did you want SHROOMS or SPEED?\n\n";
cout<<"1. SHROOMS.\n";
cout<<"2. SPEED.\n";
cout<<"Selection (0 to cancel):";
//await choice
cin>>intChoice;

switch (intChoice)
{
case 0:
BuyDrugsMenu();
case 1:
BuyingDrugs("SHROOMS", intTemp[10]);
break;
case 2:
BuyingDrugs("SPEED", intTemp[11]);
break;
default:
goto RePick;
break;
}
break;
}

}

void BuyDrugsMenu()
{
//Function level Declarations
char chrChoice;
bool oneS=false, twoS=false, oneP=false, twoP=false, oneH=false, twoH=false;
int intTemp[12];
HANDLE hConsole = CreateFile("CONOUT$", GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
OPEN_EXISTING, 0, 0);

system("cls");
PrintStats();
//make stats 'GREEN' (10) with 'blk bg' (0) [menu std color]
SetConsoleColors(hConsole, 10, 0);

for (i=0; i<=11; i++)
{

if (intDrugArray<=profile.Cash)
goto SkipFor;

}
cout<<"Your broke! Go somewhere else or go sell some of your product to make up the $$$.\n";
goto EndOfFunc;
SkipFor:

//print menu options
cout<<"So you wanta buy some dope? Here's your choices...\n\n";
InvalidChoice:
for (i=0; i<=11; i++)
{

if (cstrDrugArray=="ACID")
{
cout<<"A. ACID - $"<<intDrugArray<<endl;
intTemp[1]=intDrugArray;
}
else if (cstrDrugArray=="COCAINE")
{
cout<<"C. COCAINE - $"<<intDrugArray<<endl;
intTemp[2]=intDrugArray;
}
else if (cstrDrugArray=="HASHISH")
{
cout<<"H. HASHISH - $"<<intDrugArray<<endl;
intTemp[3]=intDrugArray;
oneH=true;
}
else if (cstrDrugArray=="HERION")
{
cout<<"H. HERION - $"<<intDrugArray<<endl;
intTemp[4]=intDrugArray;
twoH=true;
}
else if (cstrDrugArray=="LUDES")
{
cout<<"L. LUDES - $"<<intDrugArray<<endl;
intTemp[5]=intDrugArray;
}
else if (cstrDrugArray=="MDA")
{
cout<<"M. MDA - $"<<intDrugArray<<endl;
intTemp[6]=intDrugArray;
}
else if (cstrDrugArray=="OPIUM")
{
cout<<"O. OPIUM - $"<<intDrugArray<<endl;
intTemp[7]=intDrugArray;
}
else if (cstrDrugArray=="PCP")
{
cout<<"P. PCP - $"<<intDrugArray<<endl;
intTemp[8]=intDrugArray;
oneP=true;
}
else if (cstrDrugArray=="PEYOTE")
{
cout<<"P. PEYOTE - $"<<intDrugArray<<endl;
intTemp[9]=intDrugArray;
twoP=true;
}
else if (cstrDrugArray=="SHROOMS")
{
cout<<"S. SHROOMS - $"<<intDrugArray<<endl;
intTemp[10]=intDrugArray;
oneS=true;
}
else if (cstrDrugArray=="SPEED")
{
cout<<"S. SPEED - $"<<intDrugArray<<endl;
intTemp[11]=intDrugArray;
twoS=true;
}
else if (cstrDrugArray=="WEED")
{
cout<<"W. WEED - $"<<intDrugArray<<endl;
intTemp[12]=intDrugArray;
}

}
cout<<"B. Back to "<<profile.Location<<".\n";
cout<<"Selection: ";
cin>>chrChoice;

//check for duplicate letters and proseed with purchase
if (chrChoice=='A' || chrChoice=='a')
{

BuyingDrugs("ACID", intTemp[1]);

}
else if (chrChoice=='B' || chrChoice=='b')
{

CityOperations(false);

}
else if (chrChoice=='C' || chrChoice=='c')
{

BuyingDrugs("COCAINE", intTemp[2]);

}
//for duplicate H's
else if ((chrChoice=='H' || chrChoice=='h') && (oneH==true && twoH==true))
{

CheckWhichDrug('h', intTemp);

}
//for none duplicate
else if ((chrChoice=='H' || chrChoice=='h') && (oneH==false || twoH==false))
{

for (i=0; i<=11; i++)
{
if (cstrDrugArray=="HASHISH")
{
BuyingDrugs("HASHISH", intTemp[3]);
i=12;
}
if (cstrDrugArray=="HERION")
{
BuyingDrugs("HERION", intTemp[4]);
i=12;
}
}

}
else if (chrChoice=='L' || chrChoice=='l')
{

BuyingDrugs("LUDES", intTemp[5]);

}
else if (chrChoice=='M' || chrChoice=='m')
{

BuyingDrugs("MDA", intTemp[6]);

}
else if (chrChoice=='O' || chrChoice=='o')
{

BuyingDrugs("OPIUM", intTemp[7]);

}
//for duplicate P's
else if ((chrChoice=='P' || chrChoice=='p') && (oneP==true && twoP==true))
{

CheckWhichDrug('p', intTemp);

}
//for none duplicate
else if ((chrChoice=='P' || chrChoice=='p') && (oneP==false || twoP==false))
{

for (i=0; i<=11; i++)
{
if (cstrDrugArray=="PCP")
{
BuyingDrugs("PCP", intTemp[8]);
i=12;
}
if (cstrDrugArray=="PEYOTE")
{
BuyingDrugs("PEYOTE", intTemp[9]);
i=12;
}
}

}
//for duplicate S's
else if ((chrChoice=='S' || chrChoice=='s') && (oneS==true && twoS==true))
{

CheckWhichDrug('s', intTemp);

}
//for none duplicate
else if ((chrChoice=='S' || chrChoice=='s') && (oneS==false || twoS==false))
{

for (i=0; i<=11; i++)
{
if (cstrDrugArray=="SHROOMS")
{
BuyingDrugs("SHROOMS", intTemp[10]);
i=12;
}
if (cstrDrugArray=="SPEED")
{
BuyingDrugs("SPEED", intTemp[11]);
i=12;
}
}

}
else if (chrChoice=='W' || chrChoice=='w')
{

BuyingDrugs("WEED", intTemp[12]);

}
else
{
system("cls");
PrintStats();
SetConsoleColors(hConsole, 10, 0);
cout<<"Invalid selection please pick again...\n\n";
goto InvalidChoice;

}


EndOfFunc:
//reverts colors back
SetConsoleColors(hConsole, 15, 0);

}
void ChangeLocMenu()
{
//Function level Declarations
int intChoice;
HANDLE hConsole = CreateFile("CONOUT$", GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
OPEN_EXISTING, 0, 0);

system("cls");
PrintStats();
//make stats 'GREEN' (10) with 'blk bg' (0) [menu std color]
SetConsoleColors(hConsole, 10, 0);
LocPickAgain:
cout<<"Where would you like to go?\n\n";
cout<<"1. BRONX\n";
cout<<"2. GHETTO\n";
cout<<"3. CENTRAL PARK\n";
cout<<"4. MANHATTAN\n";
cout<<"5. CONEY ISLAND\n";
cout<<"6. BROOKLYN\n";
cout<<"7. QUEENS\n";
cout<<"8. STATEN ISLAND\n";
cout<<"9. Back to "<<loc.Name<<"\n";
cout<<"Selection: ";
//await selection
cin>>intChoice;

switch (intChoice)
{

case BRONX:
if (profile.Location=="BRONX")
intChoice=9;
profile.Location="BRONX";
loc.Name="BRONX";
loc.minDrugs=7;
loc.maxDrugs=12;
loc.Police=10;
break;

case GHETTO:
if (profile.Location=="GHETTO")
intChoice=9;
profile.Location="GHETTO";
loc.Name="GHETTO";
loc.minDrugs=8;
loc.maxDrugs=12;
loc.Police=5;
break;

case CENTRALPARK:
if (profile.Location=="CENTRALPARK")
intChoice=9;
profile.Location="CENTRALPARK";
loc.Name="CENTRAL PARK";
loc.minDrugs=6;
loc.maxDrugs=12;
loc.Police=15;
break;

case MANHATTAN:
if (profile.Location=="MANHATTAN")
intChoice=9;
profile.Location="MANHATTAN";
loc.Name="MANHATTAN";
loc.minDrugs=4;
loc.maxDrugs=12;
loc.Police=80;
break;

case CONEYISLAND:
if (profile.Location=="CONEYISLAND")
intChoice=9;
profile.Location="CONEYISLAND";
loc.Name="CONEY ISLAND";
loc.minDrugs=6;
loc.maxDrugs=12;
loc.Police=20;
break;

case BROOKLYN:
if (profile.Location=="BROOKLYN")
intChoice=9;
profile.Location="BROOKLYN";
loc.Name="BROOKLYN";
loc.minDrugs=4;
loc.maxDrugs=12;
loc.Police=70;
break;

case QUEENS:
if (profile.Location=="QUEENS")
intChoice=9;
profile.Location="QUEENS";
loc.Name="QUEENS";
loc.minDrugs=6;
loc.maxDrugs=12;
loc.Police=50;
break;

case STATENISLAND:
if (profile.Location=="STATENISLAND")
intChoice=9;
profile.Location="STATENISLAND";
loc.Name="STATEN ISLAND";
loc.minDrugs=6;
loc.maxDrugs=12;
loc.Police=20;
break;

case 9:
break;

default:
system("cls");
PrintStats();
//make stats 'GREEN' (10) with 'blk bg' (0) [menu std color]
SetConsoleColors(hConsole, 10, 0);
cout<<"Invalid entry please pick again.\n\n";
goto LocPickAgain;
break;

}

//revert to old colors
SetConsoleColors(hConsole, 15, 0);

//if the city never changes then don't penalize with a new day
if (intChoice==9)
{
CityOperations(false);
}
else
{
CityOperations(true);
}

}
void LoanSharkMenu()
{
//Function level Declarations
int intChoice, intAmt;
HANDLE hConsole = CreateFile("CONOUT$", GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
OPEN_EXISTING, 0, 0);

//make stats 'GREEN' (10) with 'blk bg' (0) [menu std color]
SetConsoleColors(hConsole, 10, 0);

LoanChoiceAgain:

//Prints the loan shark welcome msg
system("cls");
PrintStats();
SetConsoleColors(hConsole, 10, 0);
cout<<"Welcome to Shady Shawn's Shack!\n";
cout<<"Rates are currently at 12% factored in each Sunday (7th day).\n\n";

//allows you to pay off debt only when you have some
if(profile.Debt>0)
{
//prints menu
cout<<"1. Pay off debt.\n";
cout<<"2. Leave Shady Shawn's.\n";
cout<<"Selection: ";
//await choice
cin>>intChoice;

switch (intChoice)
{

//run through pay off options/operations
case 1:
system("cls");
PrintStats();
SetConsoleColors(hConsole, 10, 0);
PLoanAgain:
cout<<"Shady Shawn's Shack!\n\n";
cout<<"Enter Amount to Pay off (0 to cancel): ";
cin>>intAmt;
if (intAmt>profile.Cash || intAmt<0)
{
system("cls");
PrintStats();
SetConsoleColors(hConsole, 10, 0);
cout<<"Invalid $ amount please pick again.\n\n";
goto PLoanAgain;
}

//redefines variables appropriatly
profile.Cash=profile.Cash-intAmt;
profile.Debt=profile.Debt-intAmt;

break;

//leaves the loan shark and heads back to the city without a day increase
case 2:
CityOperations(false);
break;

//sends you back to pick again due to invalid entry
default:
goto LoanChoiceAgain;
break;

}
}
else
{
//allows you to take out a loan when you have no debt
cout<<"1. Take out a Loan.\n";
cout<<"2. Leave Shady Shawn's.\n";
cout<<"Selection: ";
//await choice
cin>>intChoice;

switch (intChoice)
{

//prints options and runs the loan request operations
case 1:
system("cls");
PrintStats();
SetConsoleColors(hConsole, 10, 0);
PLoanAgain2:
cout<<"Shady Shawn's Shack!\n\n";
cout<<"Enter Amount of the Loan (0 to cancel): ";
//await selection
cin>>intAmt;

//can't use negatives
if (intAmt<0)
{
system("cls");
PrintStats();
SetConsoleColors(hConsole, 10, 0);
cout<<"Invalid amount entered, try again man.\n\n";
goto PLoanAgain2;
}
//only loans out up to 30000
if (intAmt>=30000)
{
system("cls");
PrintStats();
SetConsoleColors(hConsole, 10, 0);
cout<<"I only give out Loans of $30,000 and below, try again man.\n\n";
goto PLoanAgain2;
}

//redefines variables appropriatly
profile.Cash=profile.Cash+intAmt;
profile.Debt=profile.Debt+intAmt;

break;

//send you back to the city without a day penalty
case 2:
CityOperations(false);
break;

//sends user through menu again due to invalid entry
default:
goto LoanChoiceAgain;
break;

}

}

//revert to old colors
SetConsoleColors(hConsole, 15, 0);

//sends user back to the city without day penalty
CityOperations(false);

}

//checks to see if the use already has a profile by that name saved
bool CheckSaved()
{
string Data=strPath + "\\profiles.dat", strTemp;
ifstream inFile(Data.c_str());

while (inFile >> Data)
{
strTemp="profile.name=" + profile.Name;
if (Data==strTemp)
{
inFile.close();
return(true);
}
};

inFile.close();
return (false);
}

//rewrites the files needed for saving
void SaveGame()
{
bool Saved;
string Data=strPath + "\\profiles.dat", strOldData[100], strOldData2[100], strTemp;
char chrLine[2000];
int cnt=0;

//empty strOldData incase this isn't the first save
for (i=0; i<=99; i++)
strOldData=" ";

Saved=CheckSaved();

if (Saved==false)
{
fstream inFile(Data.c_str(), ios::in);

//store information in file for reprint
while (! inFile.eof())
{
inFile.getline(chrLine,2000);

//remove extra space
for (i=0; i<=1999; i++)
chrLine=chrLine[i+1];

//convert to string array
for (i=0; i<=1999; i++)
strOldData[cnt]=strOldData[cnt]+chrLine;

cnt++;
};
inFile.close();

//rewrite the old info
cnt=1;
fstream outFile(Data.c_str(), ios::out);
outFile<<strOldData[0];
while (!(strOldData[cnt]==" "))
{
outFile<<endl<<strOldData[cnt];
cnt++;
};

//write the new info
outFile<<endl<<endl;
outFile<<" profile.name="<<profile.Name<<endl;
outFile<<" profile.gun="<<profile.Gun<<endl;
outFile<<" profile.location="<<profile.Location<<endl;
outFile<<" profile.cash="<<profile.Cash<<endl;
outFile<<" profile.debt="<<profile.Debt<<endl;
outFile<<" profile.bank="<<profile.Bank<<endl;
outFile<<" profile.hentchmen="<<profile.Hentchmen<<endl;
outFile<<" profile.health="<<profile.Health<<endl;
outFile<<" profile.space="<<profile.Space<<endl;
outFile<<" profile.day="<<profile.Day<<endl;
outFile<<" loc.name="<<loc.Name<<endl;
outFile<<" loc.police="<<loc.Police<<endl;
outFile<<" loc.mindrugs="<<loc.minDrugs<<endl;
outFile<<" loc.maxdrugs="<<loc.maxDrugs<<endl;

outFile.close();

}
else
{
//open file
fstream inFile(Data.c_str(), ios::in);

//store information in file before the saved game for reprint
cnt=0;
inFile.getline(chrLine,2000);

//remove extra space
for (i=0; i<=1999; i++)
chrLine=chrLine[i+1];

//convert to string array
for (i=0; i<=1999; i++)
strOldData[cnt]=strOldData[cnt]+chrLine;

while (!(strOldData[cnt]==("profile.name=" + profile.Name)))
{
inFile.getline(chrLine,2000);

//remove extra space
for (i=0; i<=1999; i++)
chrLine=chrLine[i+1];

//convert to string array
for (i=0; i<=1999; i++)
strOldData[cnt]=strOldData[cnt]+chrLine;

cnt++;
};

if (strOldData[1]==" ")
strOldData[0]=" ";

//store information in file after the saved game for reprint
cnt=0;
inFile.getline(chrLine,2000);

//remove extra space
for (i=0; i<=1999; i++)
chrLine=chrLine[i+1];

//convert to string array
for (i=0; i<=1999; i++)
strTemp=strTemp+chrLine;

//scan until it finds the line with the profile
while (!(strTemp==("profile.name=" + profile.Name)))
{
inFile.getline(chrLine,2000);

//remove extra space
for (i=0; i<=1999; i++)
chrLine=chrLine[i+1];

//convert to string array
for (i=0; i<=1999; i++)
strTemp=strTemp+chrLine;
};

//scan until it finds the line with the last stat in the saved game [loc.maxdrugs=12]
while (!(strTemp=="loc.maxdrugs=12"))
{
inFile.getline(chrLine,2000);

//remove extra space
for (i=0; i<=1999; i++)
chrLine=chrLine[i+1];

//convert to string array
for (i=0; i<=1999; i++)
strTemp=strTemp+chrLine;

};

//store the rest of the file
while (!(inFile.eof()))
{
inFile.getline(chrLine,2000);

//remove extra space
for (i=0; i<=1999; i++)
chrLine=chrLine[i+1];

//convert to string array
for (i=0; i<=1999; i++)
strOldData2[cnt]=strOldData2[cnt]+chrLine;

cnt++;
};

if (strOldData2[1]==" ")
strOldData2[0]=" ";

inFile.close();

//rewrite the begining old info [1]
cnt=1;
fstream outFile(Data.c_str(), ios::out);
outFile<<strOldData[0];
while (!(strOldData[cnt]==" "))
{
outFile<<endl<<strOldData[cnt];
cnt++;
};

//write new saved data
//outFile<<endl<<endl;
outFile<<" profile.name="<<profile.Name<<endl;
outFile<<" profile.gun="<<profile.Gun<<endl;
outFile<<" profile.location="<<profile.Location<<endl;
outFile<<" profile.cash="<<profile.Cash<<endl;
outFile<<" profile.debt="<<profile.Debt<<endl;
outFile<<" profile.bank="<<profile.Bank<<endl;
outFile<<" profile.hentchmen="<<profile.Hentchmen<<endl;
outFile<<" profile.health="<<profile.Health<<endl;
outFile<<" profile.space="<<profile.Space<<endl;
outFile<<" profile.day="<<profile.Day<<endl;
outFile<<" loc.name="<<loc.Name<<endl;
outFile<<" loc.police="<<loc.Police<<endl;
outFile<<" loc.mindrugs="<<loc.minDrugs<<endl;
outFile<<" loc.maxdrugs="<<loc.maxDrugs<<endl;

//rewrite the end old info [1]
cnt=1;
outFile<<strOldData2[0];
while (!(strOldData2[cnt]==" "))
{
outFile<<endl<<strOldData2[cnt];
cnt++;
};

outFile.close();

}
}

//this class checks for bad convertions with stringify(int x)
class BadConversion : public std::runtime_error
{
public:
BadConversion(const std::string& s)
: std::runtime_error(s)
{ }
};

//converts ints into strings
inline std::string stringify(int x)
{
std::ostringstream o;
if (!(o << x))
throw BadConversion("stringify(int)");
return o.str();
}

//Prints the user data on the screen
void PrintStats()
{
//Function level Declarations
HANDLE hConsole = CreateFile("CONOUT$", GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
OPEN_EXISTING, 0, 0);
string st;

//make stats 'YELLOW' (14) with 'blk bg' (0) [stats std color]
SetConsoleColors(hConsole, 14, 0);

//print stats
system("cls");
cout<<BufferStr("[" + profile.Name + "]",20)<<"Location: "<<profile.Location<<"\n";

st="Cash: " + stringify(profile.Cash);
cout<<BufferStr(st,20)<<"Debt: "<<profile.Debt<<"\n";

st="Bank: " + stringify(profile.Bank);
cout<<BufferStr(st,20)<<"Hentchmen: "<<profile.Hentchmen<<"\n";

st="Health: " + stringify(profile.Health);
cout<<BufferStr(st,20)<<"Free Space: "<<profile.Space<<"\n";

cout<<BufferStr("Gun: " + profile.Gun,20)<<"Day: "<<profile.Day<<"\n\n";

//revert to old colors
SetConsoleColors(hConsole, 15, 0);

}

//Fixes text into columns
string BufferStr(string strString, int MAX_LEN)
{
//Function level delcarations
int intSpaces;
string strTemp;

//add proper spaces after the string
intSpaces=MAX_LEN-strString.length();
strTemp=strString;
for (i=1; i<=intSpaces; i++)
{
strTemp=strTemp + " ";
}

//return new str
return (strTemp.c_str());

}

//Displays the profiles menu along with scanning the app's dir for the
//profile files within it.
string ProfileMenu()
{

//Function level Declarations
string Profile1, Profile2, Profile3, Profile4, Profile5;
int intChoice;
HANDLE hConsole = CreateFile("CONOUT$", GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
OPEN_EXISTING, 0, 0);

//insert directory scanning, list creation, and extention trimming here and delete the following
Profile1="Derik";
Profile2="Dennis";
Profile3="Dad";
Profile4="Jen";
Profile5="Meem";

//make stats 'GREEN' (10) with 'blk bg' (0) [menu std color]
SetConsoleColors(hConsole, 10, 0);

//Print menu
system("cls");
cout<<"Drug Wars --- By: Derik Hammer\n\n";
cout<<"1. "<<Profile1<<"\n";
cout<<"1. "<<Profile2<<"\n";
cout<<"1. "<<Profile3<<"\n";
cout<<"1. "<<Profile4<<"\n";
cout<<"1. "<<Profile5<<"\n\n";
cout<<"Selection: ";
//Await choice
cin>>intChoice;

//loops if invalid numbers are entered.
while (intChoice<1 || intChoice>5)
{

SetConsoleColors(hConsole, 10, 0);
//Print menu
system("cls");
cout<<"Drug Wars --- By: Derik Hammer\n\n";
cout<<"1. "<<Profile1<<"\n";
cout<<"1. "<<Profile2<<"\n";
cout<<"1. "<<Profile3<<"\n";
cout<<"1. "<<Profile4<<"\n";
cout<<"1. "<<Profile5<<"\n\n";
cout<<"INVALID SELECTION PLEASE CHOOSE AGAIN!\n";
cout<<"Selection: ";
//Await choice
cin>>intChoice;
}

//revert to old colors
SetConsoleColors(hConsole, 15, 0);

//Returns the name of the profile needed to be used
switch (intChoice)
{

case 1:
return Profile1;
break;

case 2:
return Profile2;
break;

case 3:
return Profile3;
break;

case 4:
return Profile4;
break;

case 5:
return Profile5;
break;

}

return "NULL";
}

//Displays the options for the Main Menu
int MainMenu()
{
//Function level Declarations
char chrChoice;
HANDLE hConsole = CreateFile("CONOUT$", GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
OPEN_EXISTING, 0, 0);

//make stats 'GREEN' (10) with 'blk bg' (0) [menu std color]
SetConsoleColors(hConsole, 10, 0);

//Print menu
cout<<"Drug Wars --- By: Derik Hammer\n\n";
cout<<"1. New Game.\n";
cout<<"2. Load Profile.\n";
cout<<"3. Quit.\n\n";
cout<<"Selection: ";
//Await choice
cin>>chrChoice;

CheckAgain:
switch (chrChoice)
{

//Proceed with new game
case '1':
//revert to old colors
SetConsoleColors(hConsole, 15, 0);
return (1);
break;

//Proceed with loading profile menu
case '2':
//revert to old colors
SetConsoleColors(hConsole, 15, 0);
return (2);
break;

//Proceed with exiting the application
case '3':
//revert to old colors
SetConsoleColors(hConsole, 15, 0);
return (3);
break;

//Clear screen, reprint menu, notify invalid selection, and allow a second choice
default:
system("cls");
cout<<"Drug Wars --- By: Derik Hammer\n\n";
cout<<"1. New Game.\n";
cout<<"2. Load Profile.\n";
cout<<"3. Quit.\n\n";
//revert to old colors
SetConsoleColors(hConsole, 1, 0);
cout<<"INVALID SELECTION PLEASE CHOOSE AGAIN!\n";
cout<<"Selection: ";
cin>>chrChoice;
//make stats 'GREEN' (10) with 'blk bg' (0) [menu std color]
SetConsoleColors(hConsole, 10, 0);
goto CheckAgain;

}

//revert to old colors
SetConsoleColors(hConsole, 15, 0);

return(0);

}

// use Unicode (wide) version of GetModuleFileName
wstring get_module_path_wide(void)
{
wchar_t wide_buf[65536];
wstring module_name;

if(!GetModuleFileNameW(GetModuleHandle(0), wide_buf, 65535))
return module_name;

wide_buf[65535] = L'\0';

module_name = wide_buf;

size_t pos = module_name.find_last_of(L'\\');

if(pos != string::npos)
module_name = module_name.substr(0, pos);

return module_name;
}

// use this function to convert from wide wstring to ASCII string if need be
string wsts(const wstring &src_string)
{
size_t src_len = src_string.length();

if(src_len == 0)
return "";

char *buf = new(std::nothrow) char[src_len + 1];

if(buf == 0)
return "";

wcstombs(buf, src_string.c_str(), src_len);
buf[src_len] = '\0';

string final_string = buf;

if(buf != 0)
delete [] buf;

return final_string;
}

//Color Change functions
WORD GetConsoleColors(HANDLE hConsole)
{
// get current console text attributes
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hConsole, &csbi);
return csbi.wAttributes;
}

WORD SetConsoleColors(HANDLE hConsole, WORD wColorText, WORD wColorBkg = 0)
{
// get old text attributes
WORD wOldColors = GetConsoleColors(hConsole);

// set new text attributes
SetConsoleTextAttribute(hConsole, wColorText | (wColorBkg << 4));
return wOldColors;
}


Share this post


Link to post
Share on other sites
profile.name which it's refering is a string and the func i posted 'CheckSaved()' checks to see if that string is in teh file or not. i know that it is in the file or it would proform the first condition to the if statment in SaveGame().

if it helps here's all the code

[source code=cpp]
//for VC++
#include "stdafx.h"

//for basic functions i.e. (cout,cin)
#include <iostream>
#include <afx.h>

//for file manipulation
#include <fstream>
#include <cstdlib>

//For getch()/getche()
#include <conio.h>

//For srand() and time()
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

//for the string convertions
#include <sstream>
#include <cstring>
#include <stdexcept>

using namespace std;

//Global Variable Declaration
struct USERDATA
{

string Name, Gun, Location;
int Cash, Debt, Bank, Hentchmen, Health, Space, Day;

};
struct LOCDATA
{

string Name;
int Police, minDrugs, maxDrugs;

};
int i;
struct LOCDATA loc;
struct USERDATA profile;
enum Drugs {ACID=1, COCAINE, HASHISH, HERION, LUDES, MDA, OPIUM, PCP, PEYOTE, SHROOMS, SPEED, WEED};
enum LOCS {BRONX=1, GHETTO, CENTRALPARK, MANHATTAN, CONEYISLAND, BROOKLYN, QUEENS, STATENISLAND};
WORD wText,
wBkg,
wOldColors;
int intDrugArray[12];
string cstrDrugArray[12];
int intInvArray[12];
string strInvArray[12];
string strPath;

//function delcarations
WORD SetConsoleColors(HANDLE hConsole, WORD wColorText, WORD wColorBkg);
WORD GetConsoleColors(HANDLE hConsole);
wstring get_module_path_wide(void);
string wsts(const wstring &src_string);
int MainMenu();
string ProfileMenu();
inline std::string stringify(int x);
string BufferStr(string strString, int MAX_LEN);
void BuyingDrugs(string Drug, int Price);
void PrintStats();
void CityOperations(bool NewDay);
void BankMenu();
void SellDrugsMenu();
void BuyDrugsMenu();
void ChangeLocMenu();
void LoanSharkMenu();
void SaveGame();
bool CheckSaved();

//Start off function
int main(int argc, char* argv[])
{

//Color change sample: (ranges 0-15) [uncommented part fo global variable declare]
/*wText = 1;
wBkg = 5;
wOldColors;
HANDLE hConsole = CreateFile("CONOUT$", GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
OPEN_EXISTING, 0, 0);
wOldColors = SetConsoleColors(hConsole, wText, wBkg);
cout << "Colors in the console!" << endl << endl;
SetConsoleColors(hConsole, wOldColors, 0);*/


//Initialize random generator
srand ( time(NULL) );

//Initialize global variables
intInvArray[250]=0;
strPath=wsts(get_module_path_wide());

//Function level Delcarations
string cstrProfile;
string strTemp;
int intMnuOpt;

//Displays Main Menu
intMnuOpt=MainMenu();

//ERROR code
if (intMnuOpt==0)
{
system("cls");
cout<<"int MainMenu() returned ERROR code '0'\n";
}

//New Game code
if (intMnuOpt==1)
{
//Variable initalization.
profile.Bank=0;
profile.Health=100;
profile.Day=1;
profile.Cash=rand()%3000 + 2000;
profile.Debt=rand()%9000 + 1000;
profile.Gun="NONE";
profile.Location="BRONX";
profile.Hentchmen=rand()%4 + 7;
profile.Space=profile.Hentchmen * 10;
loc.maxDrugs=12;

//Set user name.
system("cls");
cout<<"Enter your name: ";
cin>>strTemp;
profile.Name=strTemp.c_str();

//Starts off in BRONX
loc.Name="BRONX";
loc.minDrugs=7;
loc.Police=10;
CityOperations(false);

}

//Load Profile code
if (intMnuOpt==2)
{
//displays menu, prompts profile choice, stores the user ID
cstrProfile=ProfileMenu();

//ERROR checking
if (cstrProfile=="NULL")
{
system("cls");
cout<<"string ProfileMenu() returned ERROR code 'NULL'\n";
}

//Profile Contence:
//see USERDATA struct (variable - profile)
//use ascii values of each digit to prevent easy editing

//temp debugger code
system("cls");
cout<<"Profile: "<<cstrProfile<<" was selected.\n";

}

//Quit Code
if (intMnuOpt==3)
{
system("cls");
cout<<"Thank you for playing \"Drug Wars\" -- By: Derik Hammer.";
getch();
return 0;
}

//Application Terminator
return 0;
}

//Operates the features of this location
void CityOperations(bool NewDay)
{

if (NewDay==true)
{
//increase the day
profile.Day++;

//increase debt due to interest
if (profile.Day%7==0)
profile.Debt=profile.Debt+(profile.Debt*.12);

}

system("cls");
PrintStats();

//sets up the drugs which are in this city (price, how many, what kind)
int intTemp, cnt=0, DrugChecker;
bool PickAgain;
int intChoice;
HANDLE hConsole = CreateFile("CONOUT$", GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
OPEN_EXISTING, 0, 0);

intTemp=rand()%(loc.maxDrugs-loc.minDrugs)+loc.minDrugs;

while (intTemp-1>=cnt)
{

do
{
PickAgain=false;
DrugChecker=rand()%12+1;
for (i=0; i<=11; i++)
{
if (intDrugArray==DrugChecker)
PickAgain=true;
}
}
while (PickAgain==true);
intDrugArray[cnt]=DrugChecker;
cnt++;

};

for (i=intTemp; i<=11; i++)
intDrugArray=0;

//Converts the int array to a string with the drug names and then makes the int array
//into the prices array at the same time.
for (i=0; i<=11; i++)
{

switch (intDrugArray)
{

case ACID:
cstrDrugArray="ACID";
intDrugArray=rand()%3400+1000;
break;

case COCAINE:
cstrDrugArray="COCAINE";
intDrugArray=rand()%14000+15000;
break;

case HASHISH:
cstrDrugArray="HASHISH";
intDrugArray=rand()%800+480;
break;

case HERION:
cstrDrugArray="HERION";
intDrugArray=rand()%7500+5500;
break;

case LUDES:
cstrDrugArray="LUDES";
intDrugArray=rand()%49+11;
break;

case MDA:
cstrDrugArray="MDA (Ecstasy)";
intDrugArray=rand()%2900+1500;
break;

case OPIUM:
cstrDrugArray="OPIUM";
intDrugArray=rand()%710+540;
break;

case PCP:
cstrDrugArray="PCP";
intDrugArray=rand()%1500+1000;
break;

case PEYOTE:
cstrDrugArray="PEYOTE";
intDrugArray=rand()%480+220;
break;

case SHROOMS:
cstrDrugArray="SHROOMS";
intDrugArray=rand()%670+630;
break;

case SPEED:
cstrDrugArray="SPEED";
intDrugArray=rand()%160+90;
break;

case WEED:
cstrDrugArray="WEED";
intDrugArray=rand()%575+315;
break;

default:
cstrDrugArray="ERROR: NO_DRUG";
break;
}

}

//Run cops coding here, seperate function for the battle run only option when un-armed
if (NewDay==true)
{



}

//print city menu
//make stats 'GREEN' (10) with 'blk bg' (0) [menu std color]
SetConsoleColors(hConsole, 10, 0);

cout<<"Welcome to "<<loc.Name<<"!\n";

//if its the home city [BRONX] then display spec menu
if (loc.Name=="BRONX")
{

specPickAgain:
cout<<"1. Check out available Drugs.\n";
cout<<"2. Sell Drugs.\n";
cout<<"3. Change location.\n";
cout<<"4. Visit Bank.\n";
cout<<"5. Visit Loan Shark.\n";
cout<<"6. Save Game.\n";
cout<<"7. Quit.\n";
cout<<"Selection: ";
//await choice
cin>>intChoice;

switch (intChoice)
{

case 1:
BuyDrugsMenu();
break;

case 2:
SellDrugsMenu();
break;

case 3:
ChangeLocMenu();
break;

case 4:
BankMenu();
break;

case 5:
LoanSharkMenu();
break;

case 6:
SaveGame();
break;

case 7:
break;

default:
goto specPickAgain;
break;

}

}
else
{

normPickAgain:
cout<<"1. Check out available Drugs.\n";
cout<<"2. Sell Drugs.\n";
cout<<"3. Change location.\n";
cout<<"Selection: ";
//await choice
cin>>intChoice;

switch (intChoice)
{

case 1:
BuyDrugsMenu();
break;

case 2:
SellDrugsMenu();
break;

case 3:
ChangeLocMenu();
break;

default:
system("cls");
PrintStats();
//make stats 'GREEN' (10) with 'blk bg' (0) [menu std color]
SetConsoleColors(hConsole, 10, 0);
cout<<"Invalid choice, please pick again.\n";
goto normPickAgain;
break;

}
}

//revert to old colors
SetConsoleColors(hConsole, 15, 0);

}

void BankMenu()
{

//function level delcarations
char chrChoice;
int intAmt;
bool boolDep=false, boolWithd=false;

//reset screen and disp stats again
system("cls");
PrintStats();

//print bank options
//make stats 'GREEN' (10) with 'blk bg' (0) [menu std color]
HANDLE hConsole = CreateFile("CONOUT$", GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
OPEN_EXISTING, 0, 0);
SetConsoleColors(hConsole, 10, 0);

cout<<"Welcome to the Bank!\n\n";

//check to see what he can do in the bank
if (profile.Cash>0)
{
cout<<"D. Deposit (Up to $"<<profile.Cash<<")\n";
boolDep=true;
}
if (profile.Bank>0)
{
cout<<"W. Withdraw (Up to $"<<profile.Bank<<")\n";
boolWithd=true;
}
cout<<"L. Leave Bank.\n";
//await choice
cout<<"Selection: ";
cin>>chrChoice;

if ((chrChoice=='d' || chrChoice=='D') && boolDep==true)
{

//disp deposit menu
system("cls");
PrintStats();
SetConsoleColors(hConsole, 10, 0);
DepAgain:
cout<<"Bank Deposit -- $"<<profile.Cash<<" available.\n\n";
cout<<"Deposit Amount (0 to cancel): ";
//await choice
cin>>intAmt;
if (intAmt>profile.Cash || intAmt<0)
{
system("cls");
PrintStats();
SetConsoleColors(hConsole, 10, 0);
cout<<"Invalid $ amount please pick again.\n\n";
goto DepAgain;
}
//alter $ amounts accordingly
profile.Cash=profile.Cash-intAmt;
profile.Bank=profile.Bank+intAmt;


}
else if ((chrChoice=='w' || chrChoice=='W') && boolWithd==true)
{

//disp withdraw menu
system("cls");
PrintStats();
SetConsoleColors(hConsole, 10, 0);
WDrawAgain:
cout<<"Bank Withdraw -- $"<<profile.Bank<<" available.\n\n";
cout<<"Withdraw Amount (0 to cancel): ";
//await choice
cin>>intAmt;
if (intAmt>profile.Bank || intAmt<0)
{
system("cls");
PrintStats();
SetConsoleColors(hConsole, 10, 0);
cout<<"Invalid $ amount please pick again.\n\n";
goto WDrawAgain;
}
//alter $ amounts accordingly
profile.Bank=profile.Bank-intAmt;
profile.Cash=profile.Cash+intAmt;

}
else if (chrChoice=='l' || chrChoice=='L')
{

//return to your city
CityOperations(false);

}
else
BankMenu();

CityOperations(false);

//revert to old colors
SetConsoleColors(hConsole, 15, 0);

}
void SellDrugsMenu()
{

//debugger code
cout<<"debugger code: in void SellDrugsMenu()\n";

}

void BuyingDrugs(string Drug, int Price)
{
//Function level Declarations
HANDLE hConsole = CreateFile("CONOUT$", GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
OPEN_EXISTING, 0, 0);
int intAmt;

//print menu
system("cls");
PrintStats();
//make stats 'GREEN' (10) with 'blk bg' (0) [menu std color]
SetConsoleColors(hConsole, 10, 0);
cout<<"How much "<<Drug<<" you want? ($"<<Price<<" each)"<<endl;
cout<<"Quantity (0 to cancel): ";
cin>>intAmt;

while ((intAmt*Price>profile.Cash) || (intAmt>profile.Space))
{
//print menu
system("cls");
PrintStats();
//make stats 'GREEN' (10) with 'blk bg' (0) [menu std color]
SetConsoleColors(hConsole, 10, 0);
cout<<"You don't have enough money or enough space for that much "<<Drug<<"!\n\n";
cout<<"How much "<<Drug<<" you want? ($"<<Price<<" each)"<<endl;
cout<<"Quantity (0 to cancel): ";
cin>>intAmt;
}

//updates the space in your inv
profile.Space=profile.Space-intAmt;

//fills the drug into the available array slot
for (i=0; i<=11; i++)
{
if (intInvArray==0)
{
strInvArray=Drug;
intInvArray=Price;
i=12;
}
}

//reverts back to old colors
SetConsoleColors(hConsole, 10, 0);

BuyDrugsMenu();

}

void CheckWhichDrug(char chrDrug, int intTemp[12])
{
//Function level Declarations
HANDLE hConsole = CreateFile("CONOUT$", GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
OPEN_EXISTING, 0, 0);
int intChoice;

RePick:
system("cls");
PrintStats();
//make stats 'GREEN' (10) with 'blk bg' (0) [menu std color]
SetConsoleColors(hConsole, 10, 0);

switch (chrDrug)
{
case 'h':
cout<<"Did you want HASHISH or HERION?\n\n";
cout<<"1. HASHISH.\n";
cout<<"2. HERION.\n";
cout<<"Selection (0 to cancel):";
//await choice
cin>>intChoice;

switch (intChoice)
{
case 0:
BuyDrugsMenu();
case 1:
BuyingDrugs("HASHISH", intTemp[3]);
break;
case 2:
BuyingDrugs("HERION", intTemp[4]);
break;
default:
goto RePick;
break;
}
break;

case 'p':
cout<<"Did you want PCP or PEYOTE?\n\n";
cout<<"1. PCP.\n";
cout<<"2. PEYOTE.\n";
cout<<"Selection (0 to cancel):";
//await choice
cin>>intChoice;

switch (intChoice)
{
case 0:
BuyDrugsMenu();
case 1:
BuyingDrugs("PCP", intTemp[8]);
break;
case 2:
BuyingDrugs("PEYOTE", intTemp[9]);
break;
default:
goto RePick;
break;
}
break;

case 's':
cout<<"Did you want SHROOMS or SPEED?\n\n";
cout<<"1. SHROOMS.\n";
cout<<"2. SPEED.\n";
cout<<"Selection (0 to cancel):";
//await choice
cin>>intChoice;

switch (intChoice)
{
case 0:
BuyDrugsMenu();
case 1:
BuyingDrugs("SHROOMS", intTemp[10]);
break;
case 2:
BuyingDrugs("SPEED", intTemp[11]);
break;
default:
goto RePick;
break;
}
break;
}

}

void BuyDrugsMenu()
{
//Function level Declarations
char chrChoice;
bool oneS=false, twoS=false, oneP=false, twoP=false, oneH=false, twoH=false;
int intTemp[12];
HANDLE hConsole = CreateFile("CONOUT$", GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
OPEN_EXISTING, 0, 0);

system("cls");
PrintStats();
//make stats 'GREEN' (10) with 'blk bg' (0) [menu std color]
SetConsoleColors(hConsole, 10, 0);

for (i=0; i<=11; i++)
{

if (intDrugArray<=profile.Cash)
goto SkipFor;

}
cout<<"Your broke! Go somewhere else or go sell some of your product to make up the $$$.\n";
goto EndOfFunc;
SkipFor:

//print menu options
cout<<"So you wanta buy some dope? Here's your choices...\n\n";
InvalidChoice:
for (i=0; i<=11; i++)
{

if (cstrDrugArray=="ACID")
{
cout<<"A. ACID - $"<<intDrugArray<<endl;
intTemp[1]=intDrugArray;
}
else if (cstrDrugArray=="COCAINE")
{
cout<<"C. COCAINE - $"<<intDrugArray<<endl;
intTemp[2]=intDrugArray;
}
else if (cstrDrugArray=="HASHISH")
{
cout<<"H. HASHISH - $"<<intDrugArray<<endl;
intTemp[3]=intDrugArray;
oneH=true;
}
else if (cstrDrugArray=="HERION")
{
cout<<"H. HERION - $"<<intDrugArray<<endl;
intTemp[4]=intDrugArray;
twoH=true;
}
else if (cstrDrugArray=="LUDES")
{
cout<<"L. LUDES - $"<<intDrugArray<<endl;
intTemp[5]=intDrugArray;
}
else if (cstrDrugArray=="MDA")
{
cout<<"M. MDA - $"<<intDrugArray<<endl;
intTemp[6]=intDrugArray;
}
else if (cstrDrugArray=="OPIUM")
{
cout<<"O. OPIUM - $"<<intDrugArray<<endl;
intTemp[7]=intDrugArray;
}
else if (cstrDrugArray=="PCP")
{
cout<<"P. PCP - $"<<intDrugArray<<endl;
intTemp[8]=intDrugArray;
oneP=true;
}
else if (cstrDrugArray=="PEYOTE")
{
cout<<"P. PEYOTE - $"<<intDrugArray<<endl;
intTemp[9]=intDrugArray;
twoP=true;
}
else if (cstrDrugArray=="SHROOMS")
{
cout<<"S. SHROOMS - $"<<intDrugArray<<endl;
intTemp[10]=intDrugArray;
oneS=true;
}
else if (cstrDrugArray=="SPEED")
{
cout<<"S. SPEED - $"<<intDrugArray<<endl;
intTemp[11]=intDrugArray;
twoS=true;
}
else if (cstrDrugArray=="WEED")
{
cout<<"W. WEED - $"<<intDrugArray<<endl;
intTemp[12]=intDrugArray;
}

}
cout<<"B. Back to "<<profile.Location<<".\n";
cout<<"Selection: ";
cin>>chrChoice;

//check for duplicate letters and proseed with purchase
if (chrChoice=='A' || chrChoice=='a')
{

BuyingDrugs("ACID", intTemp[1]);

}
else if (chrChoice=='B' || chrChoice=='b')
{

CityOperations(false);

}
else if (chrChoice=='C' || chrChoice=='c')
{

BuyingDrugs("COCAINE", intTemp[2]);

}
//for duplicate H's
else if ((chrChoice=='H' || chrChoice=='h') && (oneH==true && twoH==true))
{

CheckWhichDrug('h', intTemp);

}
//for none duplicate
else if ((chrChoice=='H' || chrChoice=='h') && (oneH==false || twoH==false))
{

for (i=0; i<=11; i++)
{
if (cstrDrugArray=="HASHISH")
{
BuyingDrugs("HASHISH", intTemp[3]);
i=12;
}
if (cstrDrugArray=="HERION")
{
BuyingDrugs("HERION", intTemp[4]);
i=12;
}
}

}
else if (chrChoice=='L' || chrChoice=='l')
{

BuyingDrugs("LUDES", intTemp[5]);

}
else if (chrChoice=='M' || chrChoice=='m')
{

BuyingDrugs("MDA", intTemp[6]);

}
else if (chrChoice=='O' || chrChoice=='o')
{

BuyingDrugs("OPIUM", intTemp[7]);

}
//for duplicate P's
else if ((chrChoice=='P' || chrChoice=='p') && (oneP==true && twoP==true))
{

CheckWhichDrug('p', intTemp);

}
//for none duplicate
else if ((chrChoice=='P' || chrChoice=='p') && (oneP==false || twoP==false))
{

for (i=0; i<=11; i++)
{
if (cstrDrugArray=="PCP")
{
BuyingDrugs("PCP", intTemp[8]);
i=12;
}
if (cstrDrugArray=="PEYOTE")
{
BuyingDrugs("PEYOTE", intTemp[9]);
i=12;
}
}

}
//for duplicate S's
else if ((chrChoice=='S' || chrChoice=='s') && (oneS==true && twoS==true))
{

CheckWhichDrug('s', intTemp);

}
//for none duplicate
else if ((chrChoice=='S' || chrChoice=='s') && (oneS==false || twoS==false))
{

for (i=0; i<=11; i++)
{
if (cstrDrugArray=="SHROOMS")
{
BuyingDrugs("SHROOMS", intTemp[10]);
i=12;
}
if (cstrDrugArray=="SPEED")
{
BuyingDrugs("SPEED", intTemp[11]);
i=12;
}
}

}
else if (chrChoice=='W' || chrChoice=='w')
{

BuyingDrugs("WEED", intTemp[12]);

}
else
{
system("cls");
PrintStats();
SetConsoleColors(hConsole, 10, 0);
cout<<"Invalid selection please pick again...\n\n";
goto InvalidChoice;

}


EndOfFunc:
//reverts colors back
SetConsoleColors(hConsole, 15, 0);

}
void ChangeLocMenu()
{
//Function level Declarations
int intChoice;
HANDLE hConsole = CreateFile("CONOUT$", GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
OPEN_EXISTING, 0, 0);

system("cls");
PrintStats();
//make stats 'GREEN' (10) with 'blk bg' (0) [menu std color]
SetConsoleColors(hConsole, 10, 0);
LocPickAgain:
cout<<"Where would you like to go?\n\n";
cout<<"1. BRONX\n";
cout<<"2. GHETTO\n";
cout<<"3. CENTRAL PARK\n";
cout<<"4. MANHATTAN\n";
cout<<"5. CONEY ISLAND\n";
cout<<"6. BROOKLYN\n";
cout<<"7. QUEENS\n";
cout<<"8. STATEN ISLAND\n";
cout<<"9. Back to "<<loc.Name<<"\n";
cout<<"Selection: ";
//await selection
cin>>intChoice;

switch (intChoice)
{

case BRONX:
if (profile.Location=="BRONX")
intChoice=9;
profile.Location="BRONX";
loc.Name="BRONX";
loc.minDrugs=7;
loc.maxDrugs=12;
loc.Police=10;
break;

case GHETTO:
if (profile.Location=="GHETTO")
intChoice=9;
profile.Location="GHETTO";
loc.Name="GHETTO";
loc.minDrugs=8;
loc.maxDrugs=12;
loc.Police=5;
break;

case CENTRALPARK:
if (profile.Location=="CENTRALPARK")
intChoice=9;
profile.Location="CENTRALPARK";
loc.Name="CENTRAL PARK";
loc.minDrugs=6;
loc.maxDrugs=12;
loc.Police=15;
break;

case MANHATTAN:
if (profile.Location=="MANHATTAN")
intChoice=9;
profile.Location="MANHATTAN";
loc.Name="MANHATTAN";
loc.minDrugs=4;
loc.maxDrugs=12;
loc.Police=80;
break;

case CONEYISLAND:
if (profile.Location=="CONEYISLAND")
intChoice=9;
profile.Location="CONEYISLAND";
loc.Name="CONEY ISLAND";
loc.minDrugs=6;
loc.maxDrugs=12;
loc.Police=20;
break;

case BROOKLYN:
if (profile.Location=="BROOKLYN")
intChoice=9;
profile.Location="BROOKLYN";
loc.Name="BROOKLYN";
loc.minDrugs=4;
loc.maxDrugs=12;
loc.Police=70;
break;

case QUEENS:
if (profile.Location=="QUEENS")
intChoice=9;
profile.Location="QUEENS";
loc.Name="QUEENS";
loc.minDrugs=6;
loc.maxDrugs=12;
loc.Police=50;
break;

case STATENISLAND:
if (profile.Location=="STATENISLAND")
intChoice=9;
profile.Location="STATENISLAND";
loc.Name="STATEN ISLAND";
loc.minDrugs=6;
loc.maxDrugs=12;
loc.Police=20;
break;

case 9:
break;

default:
system("cls");
PrintStats();
//make stats 'GREEN' (10) with 'blk bg' (0) [menu std color]
SetConsoleColors(hConsole, 10, 0);
cout<<"Invalid entry please pick again.\n\n";
goto LocPickAgain;
break;

}

//revert to old colors
SetConsoleColors(hConsole, 15, 0);

//if the city never changes then don't penalize with a new day
if (intChoice==9)
{
CityOperations(false);
}
else
{
CityOperations(true);
}

}
void LoanSharkMenu()
{
//Function level Declarations
int intChoice, intAmt;
HANDLE hConsole = CreateFile("CONOUT$", GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
OPEN_EXISTING, 0, 0);

//make stats 'GREEN' (10) with 'blk bg' (0) [menu std color]
SetConsoleColors(hConsole, 10, 0);

LoanChoiceAgain:

//Prints the loan shark welcome msg
system("cls");
PrintStats();
SetConsoleColors(hConsole, 10, 0);
cout<<"Welcome to Shady Shawn's Shack!\n";
cout<<"Rates are currently at 12% factored in each Sunday (7th day).\n\n";

//allows you to pay off debt only when you have some
if(profile.Debt>0)
{
//prints menu
cout<<"1. Pay off debt.\n";
cout<<"2. Leave Shady Shawn's.\n";
cout<<"Selection: ";
//await choice
cin>>intChoice;

switch (intChoice)
{

//run through pay off options/operations
case 1:
system("cls");
PrintStats();
SetConsoleColors(hConsole, 10, 0);
PLoanAgain:
cout<<"Shady Shawn's Shack!\n\n";
cout<<"Enter Amount to Pay off (0 to cancel): ";
cin>>intAmt;
if (intAmt>profile.Cash || intAmt<0)
{
system("cls");
PrintStats();
SetConsoleColors(hConsole, 10, 0);
cout<<"Invalid $ amount please pick again.\n\n";
goto PLoanAgain;
}

//redefines variables appropriatly
profile.Cash=profile.Cash-intAmt;
profile.Debt=profile.Debt-intAmt;

break;

//leaves the loan shark and heads back to the city without a day increase
case 2:
CityOperations(false);
break;

//sends you back to pick again due to invalid entry
default:
goto LoanChoiceAgain;
break;

}
}
else
{
//allows you to take out a loan when you have no debt
cout<<"1. Take out a Loan.\n";
cout<<"2. Leave Shady Shawn's.\n";
cout<<"Selection: ";
//await choice
cin>>intChoice;

switch (intChoice)
{

//prints options and runs the loan request operations
case 1:
system("cls");
PrintStats();
SetConsoleColors(hConsole, 10, 0);
PLoanAgain2:
cout<<"Shady Shawn's Shack!\n\n";
cout<<"Enter Amount of the Loan (0 to cancel): ";
//await selection
cin>>intAmt;

//can't use negatives
if (intAmt<0)
{
system("cls");
PrintStats();
SetConsoleColors(hConsole, 10, 0);
cout<<"Invalid amount entered, try again man.\n\n";
goto PLoanAgain2;
}
//only loans out up to 30000
if (intAmt>=30000)
{
system("cls");
PrintStats();
SetConsoleColors(hConsole, 10, 0);
cout<<"I only give out Loans of $30,000 and below, try again man.\n\n";
goto PLoanAgain2;
}

//redefines variables appropriatly
profile.Cash=profile.Cash+intAmt;
profile.Debt=profile.Debt+intAmt;

break;

//send you back to the city without a day penalty
case 2:
CityOperations(false);
break;

//sends user through menu again due to invalid entry
default:
goto LoanChoiceAgain;
break;

}

}

//revert to old colors
SetConsoleColors(hConsole, 15, 0);

//sends user back to the city without day penalty
CityOperations(false);

}

//checks to see if the use already has a profile by that name saved
bool CheckSaved()
{
string Data=strPath + "\\profiles.dat", strTemp;
ifstream inFile(Data.c_str());

while (inFile >> Data)
{
strTemp="profile.name=" + profile.Name;
if (Data==strTemp)
{
inFile.close();
return(true);
}
};

inFile.close();
return (false);
}

//rewrites the files needed for saving
void SaveGame()
{
bool Saved;
string Data=strPath + "\\profiles.dat", strOldData[100], strOldData2[100], strTemp;
char chrLine[2000];
int cnt=0;

//empty strOldData incase this isn't the first save
for (i=0; i<=99; i++)
strOldData=" ";

Saved=CheckSaved();

if (Saved==false)
{
fstream inFile(Data.c_str(), ios::in);

//store information in file for reprint
while (! inFile.eof())
{
inFile.getline(chrLine,2000);

//remove extra space
for (i=0; i<=1999; i++)
chrLine=chrLine[i+1];

//convert to string array
for (i=0; i<=1999; i++)
strOldData[cnt]=strOldData[cnt]+chrLine;

cnt++;
};
inFile.close();

//rewrite the old info
cnt=1;
fstream outFile(Data.c_str(), ios::out);
outFile<<strOldData[0];
while (!(strOldData[cnt]==" "))
{
outFile<<endl<<strOldData[cnt];
cnt++;
};

//write the new info
outFile<<endl<<endl;
outFile<<" profile.name="<<profile.Name<<endl;
outFile<<" profile.gun="<<profile.Gun<<endl;
outFile<<" profile.location="<<profile.Location<<endl;
outFile<<" profile.cash="<<profile.Cash<<endl;
outFile<<" profile.debt="<<profile.Debt<<endl;
outFile<<" profile.bank="<<profile.Bank<<endl;
outFile<<" profile.hentchmen="<<profile.Hentchmen<<endl;
outFile<<" profile.health="<<profile.Health<<endl;
outFile<<" profile.space="<<profile.Space<<endl;
outFile<<" profile.day="<<profile.Day<<endl;
outFile<<" loc.name="<<loc.Name<<endl;
outFile<<" loc.police="<<loc.Police<<endl;
outFile<<" loc.mindrugs="<<loc.minDrugs<<endl;
outFile<<" loc.maxdrugs="<<loc.maxDrugs<<endl;

outFile.close();

}
else
{
//open file
fstream inFile(Data.c_str(), ios::in);

//store information in file before the saved game for reprint
cnt=0;
inFile.getline(chrLine,2000);

//remove extra space
for (i=0; i<=1999; i++)
chrLine=chrLine[i+1];

//convert to string array
for (i=0; i<=1999; i++)
strOldData[cnt]=strOldData[cnt]+chrLine;

while (!(strOldData[cnt]==("profile.name=" + profile.Name)))
{
inFile.getline(chrLine,2000);

//remove extra space
for (i=0; i<=1999; i++)
chrLine=chrLine[i+1];

//convert to string array
for (i=0; i<=1999; i++)
strOldData[cnt]=strOldData[cnt]+chrLine;

cnt++;
};

if (strOldData[1]==" ")
strOldData[0]=" ";

//store information in file after the saved game for reprint
cnt=0;
inFile.getline(chrLine,2000);

//remove extra space
for (i=0; i<=1999; i++)
chrLine=chrLine[i+1];

//convert to string array
for (i=0; i<=1999; i++)
strTemp=strTemp+chrLine;

//scan until it finds the line with the profile
while (!(strTemp==("profile.name=" + profile.Name)))
{
inFile.getline(chrLine,2000);

//remove extra space
for (i=0; i<=1999; i++)
chrLine=chrLine[i+1];

//convert to string array
for (i=0; i<=1999; i++)
strTemp=strTemp+chrLine;
};

//scan until it finds the line with the last stat in the saved game [loc.maxdrugs=12]
while (!(strTemp=="loc.maxdrugs=12"))
{
inFile.getline(chrLine,2000);

//remove extra space
for (i=0; i<=1999; i++)
chrLine=chrLine[i+1];

//convert to string array
for (i=0; i<=1999; i++)
strTemp=strTemp+chrLine;

};

//store the rest of the file
while (!(inFile.eof()))
{
inFile.getline(chrLine,2000);

//remove extra space
for (i=0; i<=1999; i++)
chrLine=chrLine[i+1];

//convert to string array
for (i=0; i<=1999; i++)
strOldData2[cnt]=strOldData2[cnt]+chrLine;

cnt++;
};

if (strOldData2[1]==" ")
strOldData2[0]=" ";

inFile.close();

//rewrite the begining old info [1]
cnt=1;
fstream outFile(Data.c_str(), ios::out);
outFile<<strOldData[0];
while (!(strOldData[cnt]==" "))
{
outFile<<endl<<strOldData[cnt];
cnt++;
};

//write new saved data
//outFile<<endl<<endl;
outFile<<" profile.name="<<profile.Name<<endl;
outFile<<" profile.gun="<<profile.Gun<<endl;
outFile<<" profile.location="<<profile.Location<<endl;
outFile<<" profile.cash="<<profile.Cash<<endl;
outFile<<" profile.debt="<<profile.Debt<<endl;
outFile<<" profile.bank="<<profile.Bank<<endl;
outFile<<" profile.hentchmen="<<profile.Hentchmen<<endl;
outFile<<" profile.health="<<profile.Health<<endl;
outFile<<" profile.space="<<profile.Space<<endl;
outFile<<" profile.day="<<profile.Day<<endl;
outFile<<" loc.name="<<loc.Name<<endl;
outFile<<" loc.police="<<loc.Police<<endl;
outFile<<" loc.mindrugs="<<loc.minDrugs<<endl;
outFile<<" loc.maxdrugs="<<loc.maxDrugs<<endl;

//rewrite the end old info [1]
cnt=1;
outFile<<strOldData2[0];
while (!(strOldData2[cnt]==" "))
{
outFile<<endl<<strOldData2[cnt];
cnt++;
};

outFile.close();

}
}

//this class checks for bad convertions with stringify(int x)
class BadConversion : public std::runtime_error
{
public:
BadConversion(const std::string& s)
: std::runtime_error(s)
{ }
};

//converts ints into strings
inline std::string stringify(int x)
{
std::ostringstream o;
if (!(o << x))
throw BadConversion("stringify(int)");
return o.str();
}

//Prints the user data on the screen
void PrintStats()
{
//Function level Declarations
HANDLE hConsole = CreateFile("CONOUT$", GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
OPEN_EXISTING, 0, 0);
string st;

//make stats 'YELLOW' (14) with 'blk bg' (0) [stats std color]
SetConsoleColors(hConsole, 14, 0);

//print stats
system("cls");
cout<<BufferStr("[" + profile.Name + "]",20)<<"Location: "<<profile.Location<<"\n";

st="Cash: " + stringify(profile.Cash);
cout<<BufferStr(st,20)<<"Debt: "<<profile.Debt<<"\n";

st="Bank: " + stringify(profile.Bank);
cout<<BufferStr(st,20)<<"Hentchmen: "<<profile.Hentchmen<<"\n";

st="Health: " + stringify(profile.Health);
cout<<BufferStr(st,20)<<"Free Space: "<<profile.Space<<"\n";

cout<<BufferStr("Gun: " + profile.Gun,20)<<"Day: "<<profile.Day<<"\n\n";

//revert to old colors
SetConsoleColors(hConsole, 15, 0);

}

//Fixes text into columns
string BufferStr(string strString, int MAX_LEN)
{
//Function level delcarations
int intSpaces;
string strTemp;

//add proper spaces after the string
intSpaces=MAX_LEN-strString.length();
strTemp=strString;
for (i=1; i<=intSpaces; i++)
{
strTemp=strTemp + " ";
}

//return new str
return (strTemp.c_str());

}

//Displays the profiles menu along with scanning the app's dir for the
//profile files within it.
string ProfileMenu()
{

//Function level Declarations
string Profile1, Profile2, Profile3, Profile4, Profile5;
int intChoice;
HANDLE hConsole = CreateFile("CONOUT$", GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
OPEN_EXISTING, 0, 0);

//insert directory scanning, list creation, and extention trimming here and delete the following
Profile1="Derik";
Profile2="Dennis";
Profile3="Dad";
Profile4="Jen";
Profile5="Meem";

//make stats 'GREEN' (10) with 'blk bg' (0) [menu std color]
SetConsoleColors(hConsole, 10, 0);

//Print menu
system("cls");
cout<<"Drug Wars --- By: Derik Hammer\n\n";
cout<<"1. "<<Profile1<<"\n";
cout<<"1. "<<Profile2<<"\n";
cout<<"1. "<<Profile3<<"\n";
cout<<"1. "<<Profile4<<"\n";
cout<<"1. "<<Profile5<<"\n\n";
cout<<"Selection: ";
//Await choice
cin>>intChoice;

//loops if invalid numbers are entered.
while (intChoice<1 || intChoice>5)
{

SetConsoleColors(hConsole, 10, 0);
//Print menu
system("cls");
cout<<"Drug Wars --- By: Derik Hammer\n\n";
cout<<"1. "<<Profile1<<"\n";
cout<<"1. "<<Profile2<<"\n";
cout<<"1. "<<Profile3<<"\n";
cout<<"1. "<<Profile4<<"\n";
cout<<"1. "<<Profile5<<"\n\n";
cout<<"INVALID SELECTION PLEASE CHOOSE AGAIN!\n";
cout<<"Selection: ";
//Await choice
cin>>intChoice;
}

//revert to old colors
SetConsoleColors(hConsole, 15, 0);

//Returns the name of the profile needed to be used
switch (intChoice)
{

case 1:
return Profile1;
break;

case 2:
return Profile2;
break;

case 3:
return Profile3;
break;

case 4:
return Profile4;
break;

case 5:
return Profile5;
break;

}

return "NULL";
}

//Displays the options for the Main Menu
int MainMenu()
{
//Function level Declarations
char chrChoice;
HANDLE hConsole = CreateFile("CONOUT$", GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
OPEN_EXISTING, 0, 0);

//make stats 'GREEN' (10) with 'blk bg' (0) [menu std color]
SetConsoleColors(hConsole, 10, 0);

//Print menu
cout<<"Drug Wars --- By: Derik Hammer\n\n";
cout<<"1. New Game.\n";
cout<<"2. Load Profile.\n";
cout<<"3. Quit.\n\n";
cout<<"Selection: ";
//Await choice
cin>>chrChoice;

CheckAgain:
switch (chrChoice)
{

//Proceed with new game
case '1':
//revert to old colors
SetConsoleColors(hConsole, 15, 0);
return (1);
break;

//Proceed with loading profile menu
case '2':
//revert to old colors
SetConsoleColors(hConsole, 15, 0);
return (2);
break;

//Proceed with exiting the application
case '3':
//revert to old colors
SetConsoleColors(hConsole, 15, 0);
return (3);
break;

//Clear screen, reprint menu, notify invalid selection, and allow a second choice
default:
system("cls");
cout<<"Drug Wars --- By: Derik Hammer\n\n";
cout<<"1. New Game.\n";
cout<<"2. Load Profile.\n";
cout<<"3. Quit.\n\n";
//revert to old colors
SetConsoleColors(hConsole, 1, 0);
cout<<"INVALID SELECTION PLEASE CHOOSE AGAIN!\n";
cout<<"Selection: ";
cin>>chrChoice;
//make stats 'GREEN' (10) with 'blk bg' (0) [menu std color]
SetConsoleColors(hConsole, 10, 0);
goto CheckAgain;

}

//revert to old colors
SetConsoleColors(hConsole, 15, 0);

return(0);

}

// use Unicode (wide) version of GetModuleFileName
wstring get_module_path_wide(void)
{
wchar_t wide_buf[65536];
wstring module_name;

if(!GetModuleFileNameW(GetModuleHandle(0), wide_buf, 65535))
return module_name;

wide_buf[65535] = L'\0';

module_name = wide_buf;

size_t pos = module_name.find_last_of(L'\\');

if(pos != string::npos)
module_name = module_name.substr(0, pos);

return module_name;
}

// use this function to convert from wide wstring to ASCII string if need be
string wsts(const wstring &src_string)
{
size_t src_len = src_string.length();

if(src_len == 0)
return "";

char *buf = new(std::nothrow) char[src_len + 1];

if(buf == 0)
return "";

wcstombs(buf, src_string.c_str(), src_len);
buf[src_len] = '\0';

string final_string = buf;

if(buf != 0)
delete [] buf;

return final_string;
}

//Color Change functions
WORD GetConsoleColors(HANDLE hConsole)
{
// get current console text attributes
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hConsole, &csbi);
return csbi.wAttributes;
}

WORD SetConsoleColors(HANDLE hConsole, WORD wColorText, WORD wColorBkg = 0)
{
// get old text attributes
WORD wOldColors = GetConsoleColors(hConsole);

// set new text attributes
SetConsoleTextAttribute(hConsole, wColorText | (wColorBkg << 4));
return wOldColors;
}


Share this post


Link to post
Share on other sites
Ok guys i figured out what was going on when i wrote to a file there was an added char at the begining of each line and i thought it was a space but i guess not therefore when i searched the string for the " " + myString it the condition was never true. Now im using .find() and the condition works out fine...however i still am having issues with teh placement of the data. Some strings get cut off and all get put in a wrong order. I want the SaveGame() to write the file again with the new saved game appended if the condition within the if is true and in the else i need it to isolate the saved data in the file, store the rest, rewrite the rest in it's order and place the new saved game in instead of the old data. Im going to disney and have no time to debug this mysel fbut if anyone wants to help me out i'd much appreciate it and in 10 days i can thank you and continue on with my prgm.

MY CODE:
[source code=cpp]
//for VC++
#include "stdafx.h"

//for basic functions i.e. (cout,cin)
#include <iostream>
#include <afx.h>

//for file manipulation
#include <fstream>
#include <cstdlib>

//For getch()/getche()
#include <conio.h>

//For srand() and time()
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

//for the string convertions
#include <sstream>
#include <cstring>
#include <stdexcept>

using namespace std;

//Global Variable Declaration
struct USERDATA
{

string Name, Gun, Location;
int Cash, Debt, Bank, Hentchmen, Health, Space, Day;

};
struct LOCDATA
{

string Name;
int Police, minDrugs, maxDrugs;

};
int i;
struct LOCDATA loc;
struct USERDATA profile;
enum Drugs {ACID=1, COCAINE, HASHISH, HERION, LUDES, MDA, OPIUM, PCP, PEYOTE, SHROOMS, SPEED, WEED};
enum LOCS {BRONX=1, GHETTO, CENTRALPARK, MANHATTAN, CONEYISLAND, BROOKLYN, QUEENS, STATENISLAND};
WORD wText,
wBkg,
wOldColors;
int intDrugArray[12];
string cstrDrugArray[12];
int intInvArray[12];
string strInvArray[12];
string strPath;

//function delcarations
WORD SetConsoleColors(HANDLE hConsole, WORD wColorText, WORD wColorBkg);
WORD GetConsoleColors(HANDLE hConsole);
wstring get_module_path_wide(void);
string wsts(const wstring &src_string);
int MainMenu();
string ProfileMenu();
inline std::string stringify(int x);
string BufferStr(string strString, int MAX_LEN);
void BuyingDrugs(string Drug, int Price);
void PrintStats();
void CityOperations(bool NewDay);
void BankMenu();
void SellDrugsMenu();
void BuyDrugsMenu();
void ChangeLocMenu();
void LoanSharkMenu();
void SaveGame();
bool CheckSaved();

//Start off function
int main(int argc, char* argv[])
{

//Color change sample: (ranges 0-15) [uncommented part fo global variable declare]
/*wText = 1;
wBkg = 5;
wOldColors;
HANDLE hConsole = CreateFile("CONOUT$", GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
OPEN_EXISTING, 0, 0);
wOldColors = SetConsoleColors(hConsole, wText, wBkg);
cout << "Colors in the console!" << endl << endl;
SetConsoleColors(hConsole, wOldColors, 0);*/


//Initialize random generator
srand ( time(NULL) );

//Initialize global variables
intInvArray[250]=0;
strPath=wsts(get_module_path_wide());

//Function level Delcarations
string cstrProfile;
string strTemp;
int intMnuOpt;

//Displays Main Menu
intMnuOpt=MainMenu();

//ERROR code
if (intMnuOpt==0)
{
system("cls");
cout<<"int MainMenu() returned ERROR code '0'\n";
}

//New Game code
if (intMnuOpt==1)
{
//Variable initalization.
profile.Bank=0;
profile.Health=100;
profile.Day=1;
profile.Cash=rand()%3000 + 2000;
profile.Debt=rand()%9000 + 1000;
profile.Gun="NONE";
profile.Location="BRONX";
profile.Hentchmen=rand()%4 + 7;
profile.Space=profile.Hentchmen * 10;
loc.maxDrugs=12;

//Set user name.
system("cls");
cout<<"Enter your name: ";
cin>>strTemp;
profile.Name=strTemp.c_str();

//Starts off in BRONX
loc.Name="BRONX";
loc.minDrugs=7;
loc.Police=10;
CityOperations(false);

}

//Load Profile code
if (intMnuOpt==2)
{
//displays menu, prompts profile choice, stores the user ID
cstrProfile=ProfileMenu();

//ERROR checking
if (cstrProfile=="NULL")
{
system("cls");
cout<<"string ProfileMenu() returned ERROR code 'NULL'\n";
}

//Profile Contence:
//see USERDATA struct (variable - profile)
//use ascii values of each digit to prevent easy editing

//temp debugger code
system("cls");
cout<<"Profile: "<<cstrProfile<<" was selected.\n";

}

//Quit Code
if (intMnuOpt==3)
{
system("cls");
cout<<"Thank you for playing \"Drug Wars\" -- By: Derik Hammer.";
getch();
return 0;
}

//Application Terminator
return 0;
}

//Operates the features of this location
void CityOperations(bool NewDay)
{

if (NewDay==true)
{
//increase the day
profile.Day++;

//increase debt due to interest
if (profile.Day%7==0)
profile.Debt=profile.Debt+(profile.Debt*.12);

}

system("cls");
PrintStats();

//sets up the drugs which are in this city (price, how many, what kind)
int intTemp, cnt=0, DrugChecker;
bool PickAgain;
int intChoice;
HANDLE hConsole = CreateFile("CONOUT$", GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
OPEN_EXISTING, 0, 0);

intTemp=rand()%(loc.maxDrugs-loc.minDrugs)+loc.minDrugs;

while (intTemp-1>=cnt)
{

do
{
PickAgain=false;
DrugChecker=rand()%12+1;
for (i=0; i<=11; i++)
{
if (intDrugArray==DrugChecker)
PickAgain=true;
}
}
while (PickAgain==true);
intDrugArray[cnt]=DrugChecker;
cnt++;

};

for (i=intTemp; i<=11; i++)
intDrugArray=0;

//Converts the int array to a string with the drug names and then makes the int array
//into the prices array at the same time.
for (i=0; i<=11; i++)
{

switch (intDrugArray)
{

case ACID:
cstrDrugArray="ACID";
intDrugArray=rand()%3400+1000;
break;

case COCAINE:
cstrDrugArray="COCAINE";
intDrugArray=rand()%14000+15000;
break;

case HASHISH:
cstrDrugArray="HASHISH";
intDrugArray=rand()%800+480;
break;

case HERION:
cstrDrugArray="HERION";
intDrugArray=rand()%7500+5500;
break;

case LUDES:
cstrDrugArray="LUDES";
intDrugArray=rand()%49+11;
break;

case MDA:
cstrDrugArray="MDA (Ecstasy)";
intDrugArray=rand()%2900+1500;
break;

case OPIUM:
cstrDrugArray="OPIUM";
intDrugArray=rand()%710+540;
break;

case PCP:
cstrDrugArray="PCP";
intDrugArray=rand()%1500+1000;
break;

case PEYOTE:
cstrDrugArray="PEYOTE";
intDrugArray=rand()%480+220;
break;

case SHROOMS:
cstrDrugArray="SHROOMS";
intDrugArray=rand()%670+630;
break;

case SPEED:
cstrDrugArray="SPEED";
intDrugArray=rand()%160+90;
break;

case WEED:
cstrDrugArray="WEED";
intDrugArray=rand()%575+315;
break;

default:
cstrDrugArray="ERROR: NO_DRUG";
break;
}

}

//Run cops coding here, seperate function for the battle run only option when un-armed
if (NewDay==true)
{



}

//print city menu
//make stats 'GREEN' (10) with 'blk bg' (0) [menu std color]
SetConsoleColors(hConsole, 10, 0);

cout<<"Welcome to "<<loc.Name<<"!\n";

//if its the home city [BRONX] then display spec menu
if (loc.Name=="BRONX")
{

specPickAgain:
cout<<"1. Check out available Drugs.\n";
cout<<"2. Sell Drugs.\n";
cout<<"3. Change location.\n";
cout<<"4. Visit Bank.\n";
cout<<"5. Visit Loan Shark.\n";
cout<<"6. Save Game.\n";
cout<<"7. Quit.\n";
cout<<"Selection: ";
//await choice
cin>>intChoice;

switch (intChoice)
{

case 1:
BuyDrugsMenu();
break;

case 2:
SellDrugsMenu();
break;

case 3:
ChangeLocMenu();
break;

case 4:
BankMenu();
break;

case 5:
LoanSharkMenu();
break;

case 6:
SaveGame();
break;

case 7:
break;

default:
goto specPickAgain;
break;

}

}
else
{

normPickAgain:
cout<<"1. Check out available Drugs.\n";
cout<<"2. Sell Drugs.\n";
cout<<"3. Change location.\n";
cout<<"Selection: ";
//await choice
cin>>intChoice;

switch (intChoice)
{

case 1:
BuyDrugsMenu();
break;

case 2:
SellDrugsMenu();
break;

case 3:
ChangeLocMenu();
break;

default:
system("cls");
PrintStats();
//make stats 'GREEN' (10) with 'blk bg' (0) [menu std color]
SetConsoleColors(hConsole, 10, 0);
cout<<"Invalid choice, please pick again.\n";
goto normPickAgain;
break;

}
}

//revert to old colors
SetConsoleColors(hConsole, 15, 0);

}

void BankMenu()
{

//function level delcarations
char chrChoice;
int intAmt;
bool boolDep=false, boolWithd=false;

//reset screen and disp stats again
system("cls");
PrintStats();

//print bank options
//make stats 'GREEN' (10) with 'blk bg' (0) [menu std color]
HANDLE hConsole = CreateFile("CONOUT$", GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
OPEN_EXISTING, 0, 0);
SetConsoleColors(hConsole, 10, 0);

cout<<"Welcome to the Bank!\n\n";

//check to see what he can do in the bank
if (profile.Cash>0)
{
cout<<"D. Deposit (Up to $"<<profile.Cash<<")\n";
boolDep=true;
}
if (profile.Bank>0)
{
cout<<"W. Withdraw (Up to $"<<profile.Bank<<")\n";
boolWithd=true;
}
cout<<"L. Leave Bank.\n";
//await choice
cout<<"Selection: ";
cin>>chrChoice;

if ((chrChoice=='d' || chrChoice=='D') && boolDep==true)
{

//disp deposit menu
system("cls");
PrintStats();
SetConsoleColors(hConsole, 10, 0);
DepAgain:
cout<<"Bank Deposit -- $"<<profile.Cash<<" available.\n\n";
cout<<"Deposit Amount (0 to cancel): ";
//await choice
cin>>intAmt;
if (intAmt>profile.Cash || intAmt<0)
{
system("cls");
PrintStats();
SetConsoleColors(hConsole, 10, 0);
cout<<"Invalid $ amount please pick again.\n\n";
goto DepAgain;
}
//alter $ amounts accordingly
profile.Cash=profile.Cash-intAmt;
profile.Bank=profile.Bank+intAmt;


}
else if ((chrChoice=='w' || chrChoice=='W') && boolWithd==true)
{

//disp withdraw menu
system("cls");
PrintStats();
SetConsoleColors(hConsole, 10, 0);
WDrawAgain:
cout<<"Bank Withdraw -- $"<<profile.Bank<<" available.\n\n";
cout<<"Withdraw Amount (0 to cancel): ";
//await choice
cin>>intAmt;
if (intAmt>profile.Bank || intAmt<0)
{
system("cls");
PrintStats();
SetConsoleColors(hConsole, 10, 0);
cout<<"Invalid $ amount please pick again.\n\n";
goto WDrawAgain;
}
//alter $ amounts accordingly
profile.Bank=profile.Bank-intAmt;
profile.Cash=profile.Cash+intAmt;

}
else if (chrChoice=='l' || chrChoice=='L')
{

//return to your city
CityOperations(false);

}
else
BankMenu();

CityOperations(false);

//revert to old colors
SetConsoleColors(hConsole, 15, 0);

}
void SellDrugsMenu()
{

//debugger code
cout<<"debugger code: in void SellDrugsMenu()\n";

}

void BuyingDrugs(string Drug, int Price)
{
//Function level Declarations
HANDLE hConsole = CreateFile("CONOUT$", GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
OPEN_EXISTING, 0, 0);
int intAmt;

//print menu
system("cls");
PrintStats();
//make stats 'GREEN' (10) with 'blk bg' (0) [menu std color]
SetConsoleColors(hConsole, 10, 0);
cout<<"How much "<<Drug<<" you want? ($"<<Price<<" each)"<<endl;
cout<<"Quantity (0 to cancel): ";
cin>>intAmt;

while ((intAmt*Price>profile.Cash) || (intAmt>profile.Space))
{
//print menu
system("cls");
PrintStats();
//make stats 'GREEN' (10) with 'blk bg' (0) [menu std color]
SetConsoleColors(hConsole, 10, 0);
cout<<"You don't have enough money or enough space for that much "<<Drug<<"!\n\n";
cout<<"How much "<<Drug<<" you want? ($"<<Price<<" each)"<<endl;
cout<<"Quantity (0 to cancel): ";
cin>>intAmt;
}

//updates the space in your inv
profile.Space=profile.Space-intAmt;

//fills the drug into the available array slot
for (i=0; i<=11; i++)
{
if (intInvArray==0)
{
strInvArray=Drug;
intInvArray=Price;
i=12;
}
}

//reverts back to old colors
SetConsoleColors(hConsole, 10, 0);

BuyDrugsMenu();

}

void CheckWhichDrug(char chrDrug, int intTemp[12])
{
//Function level Declarations
HANDLE hConsole = CreateFile("CONOUT$", GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
OPEN_EXISTING, 0, 0);
int intChoice;

RePick:
system("cls");
PrintStats();
//make stats 'GREEN' (10) with 'blk bg' (0) [menu std color]
SetConsoleColors(hConsole, 10, 0);

switch (chrDrug)
{
case 'h':
cout<<"Did you want HASHISH or HERION?\n\n";
cout<<"1. HASHISH.\n";
cout<<"2. HERION.\n";
cout<<"Selection (0 to cancel):";
//await choice
cin>>intChoice;

switch (intChoice)
{
case 0:
BuyDrugsMenu();
case 1:
BuyingDrugs("HASHISH", intTemp[3]);
break;
case 2:
BuyingDrugs("HERION", intTemp[4]);
break;
default:
goto RePick;
break;
}
break;

case 'p':
cout<<"Did you want PCP or PEYOTE?\n\n";
cout<<"1. PCP.\n";
cout<<"2. PEYOTE.\n";
cout<<"Selection (0 to cancel):";
//await choice
cin>>intChoice;

switch (intChoice)
{
case 0:
BuyDrugsMenu();
case 1:
BuyingDrugs("PCP", intTemp[8]);
break;
case 2:
BuyingDrugs("PEYOTE", intTemp[9]);
break;
default:
goto RePick;
break;
}
break;

case 's':
cout<<"Did you want SHROOMS or SPEED?\n\n";
cout<<"1. SHROOMS.\n";
cout<<"2. SPEED.\n";
cout<<"Selection (0 to cancel):";
//await choice
cin>>intChoice;

switch (intChoice)
{
case 0:
BuyDrugsMenu();
case 1:
BuyingDrugs("SHROOMS", intTemp[10]);
break;
case 2:
BuyingDrugs("SPEED", intTemp[11]);
break;
default:
goto RePick;
break;
}
break;
}

}

void BuyDrugsMenu()
{
//Function level Declarations
char chrChoice;
bool oneS=false, twoS=false, oneP=false, twoP=false, oneH=false, twoH=false;
int intTemp[12];
HANDLE hConsole = CreateFile("CONOUT$", GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
OPEN_EXISTING, 0, 0);

system("cls");
PrintStats();
//make stats 'GREEN' (10) with 'blk bg' (0) [menu std color]
SetConsoleColors(hConsole, 10, 0);

for (i=0; i<=11; i++)
{

if (intDrugArray<=profile.Cash)
goto SkipFor;

}
cout<<"Your broke! Go somewhere else or go sell some of your product to make up the $$$.\n";
goto EndOfFunc;
SkipFor:

//print menu options
cout<<"So you wanta buy some dope? Here's your choices...\n\n";
InvalidChoice:
for (i=0; i<=11; i++)
{

if (cstrDrugArray=="ACID")
{
cout<<"A. ACID - $"<<intDrugArray<<endl;
intTemp[1]=intDrugArray;
}
else if (cstrDrugArray=="COCAINE")
{
cout<<"C. COCAINE - $"<<intDrugArray<<endl;
intTemp[2]=intDrugArray;
}
else if (cstrDrugArray=="HASHISH")
{
cout<<"H. HASHISH - $"<<intDrugArray<<endl;
intTemp[3]=intDrugArray;
oneH=true;
}
else if (cstrDrugArray=="HERION")
{
cout<<"H. HERION - $"<<intDrugArray<<endl;
intTemp[4]=intDrugArray;
twoH=true;
}
else if (cstrDrugArray=="LUDES")
{
cout<<"L. LUDES - $"<<intDrugArray<<endl;
intTemp[5]=intDrugArray;
}
else if (cstrDrugArray=="MDA")
{
cout<<"M. MDA - $"<<intDrugArray<<endl;
intTemp[6]=intDrugArray;
}
else if (cstrDrugArray=="OPIUM")
{
cout<<"O. OPIUM - $"<<intDrugArray<<endl;
intTemp[7]=intDrugArray;
}
else if (cstrDrugArray=="PCP")
{
cout<<"P. PCP - $"<<intDrugArray<<endl;
intTemp[8]=intDrugArray;
oneP=true;
}
else if (cstrDrugArray=="PEYOTE")
{
cout<<"P. PEYOTE - $"<<intDrugArray<<endl;
intTemp[9]=intDrugArray;
twoP=true;
}
else if (cstrDrugArray=="SHROOMS")
{
cout<<"S. SHROOMS - $"<<intDrugArray<<endl;
intTemp[10]=intDrugArray;
oneS=true;
}
else if (cstrDrugArray=="SPEED")
{
cout<<"S. SPEED - $"<<intDrugArray<<endl;
intTemp[11]=intDrugArray;
twoS=true;
}
else if (cstrDrugArray=="WEED")
{
cout<<"W. WEED - $"<<intDrugArray<<endl;
intTemp[12]=intDrugArray;
}

}
cout<<"B. Back to "<<profile.Location<<".\n";
cout<<"Selection: ";
cin>>chrChoice;

//check for duplicate letters and proseed with purchase
if (chrChoice=='A' || chrChoice=='a')
{

BuyingDrugs("ACID", intTemp[1]);

}
else if (chrChoice=='B' || chrChoice=='b')
{

CityOperations(false);

}
else if (chrChoice=='C' || chrChoice=='c')
{

BuyingDrugs("COCAINE", intTemp[2]);

}
//for duplicate H's
else if ((chrChoice=='H' || chrChoice=='h') && (oneH==true && twoH==true))
{

CheckWhichDrug('h', intTemp);

}
//for none duplicate
else if ((chrChoice=='H' || chrChoice=='h') && (oneH==false || twoH==false))
{

for (i=0; i<=11; i++)
{
if (cstrDrugArray=="HASHISH")
{
BuyingDrugs("HASHISH", intTemp[3]);
i=12;
}
if (cstrDrugArray=="HERION")
{
BuyingDrugs("HERION", intTemp[4]);
i=12;
}
}

}
else if (chrChoice=='L' || chrChoice=='l')
{

BuyingDrugs("LUDES", intTemp[5]);

}
else if (chrChoice=='M' || chrChoice=='m')
{

BuyingDrugs("MDA", intTemp[6]);

}
else if (chrChoice=='O' || chrChoice=='o')
{

BuyingDrugs("OPIUM", intTemp[7]);

}
//for duplicate P's
else if ((chrChoice=='P' || chrChoice=='p') && (oneP==true && twoP==true))
{

CheckWhichDrug('p', intTemp);

}
//for none duplicate
else if ((chrChoice=='P' || chrChoice=='p') && (oneP==false || twoP==false))
{

for (i=0; i<=11; i++)
{
if (cstrDrugArray=="PCP")
{
BuyingDrugs("PCP", intTemp[8]);
i=12;
}
if (cstrDrugArray=="PEYOTE")
{
BuyingDrugs("PEYOTE", intTemp[9]);
i=12;
}
}

}
//for duplicate S's
else if ((chrChoice=='S' || chrChoice=='s') && (oneS==true && twoS==true))
{

CheckWhichDrug('s', intTemp);

}
//for none duplicate
else if ((chrChoice=='S' || chrChoice=='s') && (oneS==false || twoS==false))
{

for (i=0; i<=11; i++)
{
if (cstrDrugArray=="SHROOMS")
{
BuyingDrugs("SHROOMS", intTemp[10]);
i=12;
}
if (cstrDrugArray=="SPEED")
{
BuyingDrugs("SPEED", intTemp[11]);
i=12;
}
}

}
else if (chrChoice=='W' || chrChoice=='w')
{

BuyingDrugs("WEED", intTemp[12]);

}
else
{
system("cls");
PrintStats();
SetConsoleColors(hConsole, 10, 0);
cout<<"Invalid selection please pick again...\n\n";
goto InvalidChoice;

}


EndOfFunc:
//reverts colors back
SetConsoleColors(hConsole, 15, 0);

}
void ChangeLocMenu()
{
//Function level Declarations
int intChoice;
HANDLE hConsole = CreateFile("CONOUT$", GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
OPEN_EXISTING, 0, 0);

system("cls");
PrintStats();
//make stats 'GREEN' (10) with 'blk bg' (0) [menu std color]
SetConsoleColors(hConsole, 10, 0);
LocPickAgain:
cout<<"Where would you like to go?\n\n";
cout<<"1. BRONX\n";
cout<<"2. GHETTO\n";
cout<<"3. CENTRAL PARK\n";
cout<<"4. MANHATTAN\n";
cout<<"5. CONEY ISLAND\n";
cout<<"6. BROOKLYN\n";
cout<<"7. QUEENS\n";
cout<<"8. STATEN ISLAND\n";
cout<<"9. Back to "<<loc.Name<<"\n";
cout<<"Selection: ";
//await selection
cin>>intChoice;

switch (intChoice)
{

case BRONX:
if (profile.Location=="BRONX")
intChoice=9;
profile.Location="BRONX";
loc.Name="BRONX";
loc.minDrugs=7;
loc.maxDrugs=12;
loc.Police=10;
break;

case GHETTO:
if (profile.Location=="GHETTO")
intChoice=9;
profile.Location="GHETTO";
loc.Name="GHETTO";
loc.minDrugs=8;
loc.maxDrugs=12;
loc.Police=5;
break;

case CENTRALPARK:
if (profile.Location=="CENTRALPARK")
intChoice=9;
profile.Location="CENTRALPARK";
loc.Name="CENTRAL PARK";
loc.minDrugs=6;
loc.maxDrugs=12;
loc.Police=15;
break;

case MANHATTAN:
if (profile.Location=="MANHATTAN")
intChoice=9;
profile.Location="MANHATTAN";
loc.Name="MANHATTAN";
loc.minDrugs=4;
loc.maxDrugs=12;
loc.Police=80;
break;

case CONEYISLAND:
if (profile.Location=="CONEYISLAND")
intChoice=9;
profile.Location="CONEYISLAND";
loc.Name="CONEY ISLAND";
loc.minDrugs=6;
loc.maxDrugs=12;
loc.Police=20;
break;

case BROOKLYN:
if (profile.Location=="BROOKLYN")
intChoice=9;
profile.Location="BROOKLYN";
loc.Name="BROOKLYN";
loc.minDrugs=4;
loc.maxDrugs=12;
loc.Police=70;
break;

case QUEENS:
if (profile.Location=="QUEENS")
intChoice=9;
profile.Location="QUEENS";
loc.Name="QUEENS";
loc.minDrugs=6;
loc.maxDrugs=12;
loc.Police=50;
break;

case STATENISLAND:
if (profile.Location=="STATENISLAND")
intChoice=9;
profile.Location="STATENISLAND";
loc.Name="STATEN ISLAND";
loc.minDrugs=6;
loc.maxDrugs=12;
loc.Police=20;
break;

case 9:
break;

default:
system("cls");
PrintStats();
//make stats 'GREEN' (10) with 'blk bg' (0) [menu std color]
SetConsoleColors(hConsole, 10, 0);
cout<<"Invalid entry please pick again.\n\n";
goto LocPickAgain;
break;

}

//revert to old colors
SetConsoleColors(hConsole, 15, 0);

//if the city never changes then don't penalize with a new day
if (intChoice==9)
{
CityOperations(false);
}
else
{
CityOperations(true);
}

}
void LoanSharkMenu()
{
//Function level Declarations
int intChoice, intAmt;
HANDLE hConsole = CreateFile("CONOUT$", GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
OPEN_EXISTING, 0, 0);

//make stats 'GREEN' (10) with 'blk bg' (0) [menu std color]
SetConsoleColors(hConsole, 10, 0);

LoanChoiceAgain:

//Prints the loan shark welcome msg
system("cls");
PrintStats();
SetConsoleColors(hConsole, 10, 0);
cout<<"Welcome to Shady Shawn's Shack!\n";
cout<<"Rates are currently at 12% factored in each Sunday (7th day).\n\n";

//allows you to pay off debt only when you have some
if(profile.Debt>0)
{
//prints menu
cout<<"1. Pay off debt.\n";
cout<<"2. Leave Shady Shawn's.\n";
cout<<"Selection: ";
//await choice
cin>>intChoice;

switch (intChoice)
{

//run through pay off options/operations
case 1:
system("cls");
PrintStats();
SetConsoleColors(hConsole, 10, 0);
PLoanAgain:
cout<<"Shady Shawn's Shack!\n\n";
cout<<"Enter Amount to Pay off (0 to cancel): ";
cin>>intAmt;
if (intAmt>profile.Cash || intAmt<0)
{
system("cls");
PrintStats();
SetConsoleColors(hConsole, 10, 0);
cout<<"Invalid $ amount please pick again.\n\n";
goto PLoanAgain;
}

//redefines variables appropriatly
profile.Cash=profile.Cash-intAmt;
profile.Debt=profile.Debt-intAmt;

break;

//leaves the loan shark and heads back to the city without a day increase
case 2:
CityOperations(false);
break;

//sends you back to pick again due to invalid entry
default:
goto LoanChoiceAgain;
break;

}
}
else
{
//allows you to take out a loan when you have no debt
cout<<"1. Take out a Loan.\n";
cout<<"2. Leave Shady Shawn's.\n";
cout<<"Selection: ";
//await choice
cin>>intChoice;

switch (intChoice)
{

//prints options and runs the loan request operations
case 1:
system("cls");
PrintStats();
SetConsoleColors(hConsole, 10, 0);
PLoanAgain2:
cout<<"Shady Shawn's Shack!\n\n";
cout<<"Enter Amount of the Loan (0 to cancel): ";
//await selection
cin>>intAmt;

//can't use negatives
if (intAmt<0)
{
system("cls");
PrintStats();
SetConsoleColors(hConsole, 10, 0);
cout<<"Invalid amount entered, try again man.\n\n";
goto PLoanAgain2;
}
//only loans out up to 30000
if (intAmt>=30000)
{
system("cls");
PrintStats();
SetConsoleColors(hConsole, 10, 0);
cout<<"I only give out Loans of $30,000 and below, try again man.\n\n";
goto PLoanAgain2;
}

//redefines variables appropriatly
profile.Cash=profile.Cash+intAmt;
profile.Debt=profile.Debt+intAmt;

break;

//send you back to the city without a day penalty
case 2:
CityOperations(false);
break;

//sends user through menu again due to invalid entry
default:
goto LoanChoiceAgain;
break;

}

}

//revert to old colors
SetConsoleColors(hConsole, 15, 0);

//sends user back to the city without day penalty
CityOperations(false);

}

//checks to see if the use already has a profile by that name saved
bool CheckSaved()
{
string Data=strPath + "\\profiles.dat", strTemp;
ifstream inFile(Data.c_str());

while (inFile >> Data)
{
strTemp="profile.name=" + profile.Name;
if (Data==strTemp)
{
inFile.close();
return(true);
}
};

inFile.close();
return (false);
}

//rewrites the files needed for saving
void SaveGame()
{
bool Saved;
string Data=strPath + "\\profiles.dat", strOldData[100], strOldData2[100], strTemp;
char chrLine[2000];
int cnt;

//empty strOldData incase this isn't the first save
for (i=0; i<=99; i++)
strOldData=" ";

Saved=CheckSaved();

if (Saved==false)
{
fstream inFile(Data.c_str(), ios::in);

//store information in file for reprint
while (! inFile.eof())
{
inFile.getline(chrLine,2000);

//remove extra space
for (i=0; i<=1999; i++)
chrLine=chrLine[i+1];

//convert to string array
for (i=0; i<=1999; i++)
strOldData[cnt]=strOldData[cnt]+chrLine;

cnt++;
};
inFile.close();

//rewrite the old info
cnt=1;
fstream outFile(Data.c_str(), ios::out);
outFile<<strOldData[0];
while (!(strOldData[cnt]==" "))
{
outFile<<endl<<strOldData[cnt];
cout<<strOldData[cnt]<<endl;
cnt++;
if (cnt>=100)
getch();
};

//write the new info
outFile<<endl<<endl;
outFile<<" profile.name="<<profile.Name<<endl;
outFile<<" profile.gun="<<profile.Gun<<endl;
outFile<<" profile.location="<<profile.Location<<endl;
outFile<<" profile.cash="<<profile.Cash<<endl;
outFile<<" profile.debt="<<profile.Debt<<endl;
outFile<<" profile.bank="<<profile.Bank<<endl;
outFile<<" profile.hentchmen="<<profile.Hentchmen<<endl;
outFile<<" profile.health="<<profile.Health<<endl;
outFile<<" profile.space="<<profile.Space<<endl;
outFile<<" profile.day="<<profile.Day<<endl;
outFile<<" loc.name="<<loc.Name<<endl;
outFile<<" loc.police="<<loc.Police<<endl;
outFile<<" loc.mindrugs="<<loc.minDrugs<<endl;
outFile<<" loc.maxdrugs="<<loc.maxDrugs<<endl;

outFile.close();

}
else
{
//open file
fstream inFile(Data.c_str(), ios::in);

//store information in file before the saved game for reprint
cnt=0;
inFile.getline(chrLine,2000);

//remove extra space
for (i=0; i<=1999; i++)
chrLine=chrLine[i+1];

//convert to string array
for (i=0; i<=1999; i++)
strOldData[cnt]=strOldData[cnt]+chrLine;

while (! ( strOldData[cnt].find("profile.name=" + profile.Name) ) )
{
cnt++;
inFile.getline(chrLine,2000);

//remove extra space
for (i=0; i<=1999; i++)
chrLine=chrLine[i+1];

cout<<chrLine<<endl;

//convert to string array
for (i=0; i<=1999; i++)
strOldData[cnt]=strOldData[cnt]+chrLine;

cout<<strOldData[cnt]<<endl;

};

if (strOldData[1]==" ")
strOldData[0]=" ";

//store information in file after the saved game for reprint
cnt=0;
inFile.getline(chrLine,2000);

//remove extra space
for (i=0; i<=1999; i++)
chrLine=chrLine[i+1];

//convert to string array
for (i=0; i<=1999; i++)
strTemp=strTemp+chrLine;

//scan until it finds the line with the profile
while (!(strTemp.find("profile.name=" + profile.Name)))
{
inFile.getline(chrLine,2000);

//remove extra space
for (i=0; i<=1999; i++)
chrLine=chrLine[i+1];

//convert to string array
for (i=0; i<=1999; i++)
strTemp=strTemp+chrLine;
};

//scan until it finds the line with the last stat in the saved game [loc.maxdrugs=12]
while (!(strTemp.find("loc.maxdrugs=12")))
{
inFile.getline(chrLine,2000);

//remove extra space
for (i=0; i<=1999; i++)
chrLine=chrLine[i+1];

//convert to string array
for (i=0; i<=1999; i++)
strTemp=strTemp+chrLine;

};

//store the rest of the file
while (!(inFile.eof()))
{
inFile.getline(chrLine,2000);

//remove extra space
for (i=0; i<=1999; i++)
chrLine=chrLine[i+1];

//convert to string array
for (i=0; i<=1999; i++)
strOldData2[cnt]=strOldData2[cnt]+chrLine;

cnt++;
};

if (strOldData2[1]==" ")
strOldData2[0]=" ";

inFile.close();

//rewrite the begining old info [1]
cnt=1;
fstream outFile(Data.c_str(), ios::out);
outFile<<strOldData[0];
while (!(strOldData[cnt]==" "))
{
outFile<<endl<<strOldData[cnt];
cnt++;
};

//write new saved data
//outFile<<endl<<endl;
outFile<<" profile.name="<<profile.Name<<endl;
outFile<<" profile.gun="<<profile.Gun<<endl;
outFile<<" profile.location="<<profile.Location<<endl;
outFile<<" profile.cash="<<profile.Cash<<endl;
outFile<<" profile.debt="<<profile.Debt<<endl;
outFile<<" profile.bank="<<profile.Bank<<endl;
outFile<<" profile.hentchmen="<<profile.Hentchmen<<endl;
outFile<<" profile.health="<<profile.Health<<endl;
outFile<<" profile.space="<<profile.Space<<endl;
outFile<<" profile.day="<<profile.Day<<endl;
outFile<<" loc.name="<<loc.Name<<endl;
outFile<<" loc.police="<<loc.Police<<endl;
outFile<<" loc.mindrugs="<<loc.minDrugs<<endl;
outFile<<" loc.maxdrugs="<<loc.maxDrugs<<endl;

//rewrite the end old info [1]
cnt=1;
outFile<<strOldData2[0];
while (!(strOldData2[cnt]==" "))
{
outFile<<endl<<strOldData2[cnt];
cnt++;
};

outFile.close();

}
}

//this class checks for bad convertions with stringify(int x)
class BadConversion : public std::runtime_error
{
public:
BadConversion(const std::string& s)
: std::runtime_error(s)
{ }
};

//converts ints into strings
inline std::string stringify(int x)
{
std::ostringstream o;
if (!(o << x))
throw BadConversion("stringify(int)");
return o.str();
}

//Prints the user data on the screen
void PrintStats()
{
//Function level Declarations
HANDLE hConsole = CreateFile("CONOUT$", GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
OPEN_EXISTING, 0, 0);
string st;

//make stats 'YELLOW' (14) with 'blk bg' (0) [stats std color]
SetConsoleColors(hConsole, 14, 0);

//print stats
system("cls");
cout<<BufferStr("[" + profile.Name + "]",20)<<"Location: "<<profile.Location<<"\n";

st="Cash: " + stringify(profile.Cash);
cout<<BufferStr(st,20)<<"Debt: "<<profile.Debt<<"\n";

st="Bank: " + stringify(profile.Bank);
cout<<BufferStr(st,20)<<"Hentchmen: "<<profile.Hentchmen<<"\n";

st="Health: " + stringify(profile.Health);
cout<<BufferStr(st,20)<<"Free Space: "<<profile.Space<<"\n";

cout<<BufferStr("Gun: " + profile.Gun,20)<<"Day: "<<profile.Day<<"\n\n";

//revert to old colors
SetConsoleColors(hConsole, 15, 0);

}

//Fixes text into columns
string BufferStr(string strString, int MAX_LEN)
{
//Function level delcarations
int intSpaces;
string strTemp;

//add proper spaces after the string
intSpaces=MAX_LEN-strString.length();
strTemp=strString;
for (i=1; i<=intSpaces; i++)
{
strTemp=strTemp + " ";
}

//return new str
return (strTemp.c_str());

}

//Displays the profiles menu along with scanning the app's dir for the
//profile files within it.
string ProfileMenu()
{

//Function level Declarations
string Profile1, Profile2, Profile3, Profile4, Profile5;
int intChoice;
HANDLE hConsole = CreateFile("CONOUT$", GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
OPEN_EXISTING, 0, 0);

//insert directory scanning, list creation, and extention trimming here and delete the following
Profile1="Derik";
Profile2="Dennis";
Profile3="Dad";
Profile4="Jen";
Profile5="Meem";

//make stats 'GREEN' (10) with 'blk bg' (0) [menu std color]
SetConsoleColors(hConsole, 10, 0);

//Print menu
system("cls");
cout<<"Drug Wars --- By: Derik Hammer\n\n";
cout<<"1. "<<Profile1<<"\n";
cout<<"1. "<<Profile2<<"\n";
cout<<"1. "<<Profile3<<"\n";
cout<<"1. "<<Profile4<<"\n";
cout<<"1. "<<Profile5<<"\n\n";
cout<<"Selection: ";
//Await choice
cin>>intChoice;

//loops if invalid numbers are entered.
while (intChoice<1 || intChoice>5)
{

SetConsoleColors(hConsole, 10, 0);
//Print menu
system("cls");
cout<<"Drug Wars --- By: Derik Hammer\n\n";
cout<<"1. "<<Profile1<<"\n";
cout<<"1. "<<Profile2<<"\n";
cout<<"1. "<<Profile3<<"\n";
cout<<"1. "<<Profile4<<"\n";
cout<<"1. "<<Profile5<<"\n\n";
cout<<"INVALID SELECTION PLEASE CHOOSE AGAIN!\n";
cout<<"Selection: ";
//Await choice
cin>>intChoice;
}

//revert to old colors
SetConsoleColors(hConsole, 15, 0);

//Returns the name of the profile needed to be used
switch (intChoice)
{

case 1:
return Profile1;
break;

case 2:
return Profile2;
break;

case 3:
return Profile3;
break;

case 4:
return Profile4;
break;

case 5:
return Profile5;
break;

}

return "NULL";
}

//Displays the options for the Main Menu
int MainMenu()
{
//Function level Declarations
char chrChoice;
HANDLE hConsole = CreateFile("CONOUT$", GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
OPEN_EXISTING, 0, 0);

//make stats 'GREEN' (10) with 'blk bg' (0) [menu std color]
SetConsoleColors(hConsole, 10, 0);

//Print menu
cout<<"Drug Wars --- By: Derik Hammer\n\n";
cout<<"1. New Game.\n";
cout<<"2. Load Profile.\n";
cout<<"3. Quit.\n\n";
cout<<"Selection: ";
//Await choice
cin>>chrChoice;

CheckAgain:
switch (chrChoice)
{

//Proceed with new game
case '1':
//revert to old colors
SetConsoleColors(hConsole, 15, 0);
return (1);
break;

//Proceed with loading profile menu
case '2':
//revert to old colors
SetConsoleColors(hConsole, 15, 0);
return (2);
break;

//Proceed with exiting the application
case '3':
//revert to old colors
SetConsoleColors(hConsole, 15, 0);
return (3);
break;

//Clear screen, reprint menu, notify invalid selection, and allow a second choice
default:
system("cls");
cout<<"Drug Wars --- By: Derik Hammer\n\n";
cout<<"1. New Game.\n";
cout<<"2. Load Profile.\n";
cout<<"3. Quit.\n\n";
//revert to old colors
SetConsoleColors(hConsole, 1, 0);
cout<<"INVALID SELECTION PLEASE CHOOSE AGAIN!\n";
cout<<"Selection: ";
cin>>chrChoice;
//make stats 'GREEN' (10) with 'blk bg' (0) [menu std color]
SetConsoleColors(hConsole, 10, 0);
goto CheckAgain;

}

//revert to old colors
SetConsoleColors(hConsole, 15, 0);

return(0);

}

// use Unicode (wide) version of GetModuleFileName
wstring get_module_path_wide(void)
{
wchar_t wide_buf[65536];
wstring module_name;

if(!GetModuleFileNameW(GetModuleHandle(0), wide_buf, 65535))
return module_name;

wide_buf[65535] = L'\0';

module_name = wide_buf;

size_t pos = module_name.find_last_of(L'\\');

if(pos != string::npos)
module_name = module_name.substr(0, pos);

return module_name;
}

// use this function to convert from wide wstring to ASCII string if need be
string wsts(const wstring &src_string)
{
size_t src_len = src_string.length();

if(src_len == 0)
return "";

char *buf = new(std::nothrow) char[src_len + 1];

if(buf == 0)
return "";

wcstombs(buf, src_string.c_str(), src_len);
buf[src_len] = '\0';

string final_string = buf;

if(buf != 0)
delete [] buf;

return final_string;
}

//Color Change functions
WORD GetConsoleColors(HANDLE hConsole)
{
// get current console text attributes
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hConsole, &csbi);
return csbi.wAttributes;
}

WORD SetConsoleColors(HANDLE hConsole, WORD wColorText, WORD wColorBkg = 0)
{
// get old text attributes
WORD wOldColors = GetConsoleColors(hConsole);

// set new text attributes
SetConsoleTextAttribute(hConsole, wColorText | (wColorBkg << 4));
return wOldColors;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!