In the book I'm reading it says for extra practice, to use loops and functions to make an advanced player inventory program. That part I can do, but it says to make user to program commands like in the old game "adventure". It was just too much for me to do apparently since it has multiple problems. I don't know what's wrong, so here's the whole program:
Code:
#include <cstdarg>
#include <iostream>
#include <iomanip>
#include <windows.h>
#include <string>
/* define windows colors...*/
#define w_dblue 1
#define w_dgreen 2
#define w_dcyan 3
#define w_dred 4
#define w_dviolet 5
#define w_dyellow 6
#define w_dgray 8
#define w_bule 9
#define w_green 10
#define w_cyan 11
#define w_red 12
#define w_violet 13
#define w_yellow 14
#define w_white 15
//7 = default
using namespace std;
void setColor(int color) //writes the specific string in the selected color
{
// WinApi header
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, color);
}
//returns fail if answer is not one of the options,
//bool exact controls whether answer is an option or answer contains an option
string pickTwo(string answer, string one, string two)
{
if (answer!=one && answer!=two)
return "fail";
else
return answer;
}
void coutWfe(string str)
{
cout << str;
cin.get();
}
/* Item Store */
/*COLOR CODE:
Gray = User input and messages
Cyan = Important messages
Red = Danger and alerts
Violet = Conversation
*/
int main()
{
int itemMax=6;
int item=0;
bool noGo=0;
int error=0;
int subX=0;
int subY=0;
string name="";
string choice=""; //the non-command part of the base
string answer=""; //the command part of the base
string base=""; //the base for a full command
string inventory[itemMax]; // leave inventory[0] empty
//commands
int cmndMax=12;
int cmndNum=0;
string command[cmndMax]; //leave command[0] empty
command[cmndNum++]="drop";
command[cmndNum++]="take";
command[cmndNum++]="check inventory";
command[cmndNum++]="leave";
cout << "Welcome to Inventory Test\n\n";
coutWfe("Choose a name...\n\n");
cin >> name;
coutWfe("\nYou have been named " + name + " \n\n");
coutWfe("Chapter 1: I'm just buying something, doesn't really need a chapter...\n\n");
coutWfe("You are walking through the forest...\n");
setColor(w_cyan);
coutWfe("You stumble upon a convinient chest!\n"); setColor(7);
cout << "What do you do?\n";
cin >> answer;
while (pickTwo(answer, "open", "ignore")=="fail")
{
setColor(w_red);
cout << "You can't do that...\n";
setColor(7);
cin >> answer;
}
coutWfe("\n\nYou choose to "+answer+" the chest...\n\n");
if (answer=="ignore")
{
setColor(w_cyan);
coutWfe("You're an idiot!"); setColor(7);
return 0;
}
setColor(w_cyan);
coutWfe("You found 10 items!\n\n"); setColor(7);
string boxItem[10]; //leave boxItem[0] empty
int boxNum=0;
int boxMax=10;
boxItem[boxNum++]="jewel";
boxItem[boxNum++]="sheild";
boxItem[boxNum++]="dagger";
boxItem[boxNum++]="toy bear";
boxItem[boxNum++]="orange";
boxItem[boxNum++]="ring";
boxItem[boxNum++]="red potion";
boxItem[boxNum++]="black potion";
boxItem[boxNum++]="green potion";
for(int i=0; i < 9; ++i)
{
cout << boxItem[i] << endl;
}
cout << "\nWhat do you do?\n\n";
cmndNum=0;
noGo=1;
while (cmndNum!=cmndMax && noGo==1)
{
cin >> base;
for (cmndNum=0; cmndNum < cmndMax; ++cmndNum)
{
if (base.find(command[cmndNum])==0)
{
choice=base;
answer=base;
choice.erase(0, command[cmndNum].length());
answer.erase(command[cmndNum].length(), answer.length());
break;
}
if (cmndNum==cmndMax && base.find(command[cmndNum])!=0)
{
error+=1;
}
}
if (answer==command[1])
{
for (item=0; item < itemMax; ++item)
{
if (inventory[item]==choice)
{
choice=inventory[item];
subX=item;
inventory[item]="";
break;
}
if (item==itemMax && inventory[item]!=choice)
{
setColor(w_red);
cout << "You don't have that item!"; setColor(7);
error+=1;
}
}
for (int i=subX; i < itemMax; ++i)
{
inventory[i-1]=inventory[i];
inventory[i]="";
}
if (error==0)
{
setColor(w_cyan);
cout << "You dropped your " << choice << "!"; setColor(7);
cout << "What else?...";
subX=0;
}
}
if (answer==command[2])
{
for (boxNum=0; boxNum < boxMax; ++boxNum)
{
if (boxItem[boxNum]==choice)
{
choice=boxItem[boxNum];
boxItem[boxNum]="";
subX=boxNum;
break;
}
if (boxNum==boxMax && boxItem[boxNum]!=choice)
{
error+=1;
}
}
for (int i=subX; i < boxMax; ++i)
{
boxItem[i-1]=boxItem[i];
boxItem[i]="";
}
for (item=0; item < itemMax; ++item)
{
if (inventory[item]=="");
{
inventory[item]=boxItem[subX];
break;
}
if (item==itemMax && inventory[item]!="")
{
setColor(w_red);
cout << "You have no room for that!\n"; setColor(7);
}
}
}
if (error!=0)
{
setColor(w_red);
cout << "You can't do that! There were " << error << " problems.\nTry again...\n\n"; setColor(7);
noGo=1;
error=0;
}
}
}
It doeesn't give any errors. It just has some easily fixable glitches up until I start the loops for the commands. Is it easily fixable, or do I just scrap it and start over? Cuz I don't want to do that.