I am trying to make a program that lets the user create objects of a class UNIT which are then stored in a list. I was able to get that to work, but now I want the user to be able to find a specific UNIT in the list by typing it's name. This is done by calling the accessUNIT() function.
The program compiles fine, but it never gives the user a chance to type what unit he or she wants to find. It just assumes that the user entered nothing, and repeatedly prints out, "Was not found".
My proposed solution was to put "sUnit++; at line 115 within the while loop. However, then the program only allowed the user to enter a unit's name when they were adding units, and skipped the whole length and width stuff.
I am pretty sure that the program just prints, "Was not found" repeatedly because the iterator sUnit is not being incremented.
Am I taking the right approach in searching through the list p_units, or do I need a totally different method?
#include <iostream>
#include <string>
#include <list>
using namespace std;
int MAX;
//~~~~~~~~~~~~~~~~~
// UNIT Class
//~~~~~~~~~~~~~~~~~
class UNIT
{
public:
int UnitID;
int width;
int length;
string Name;
void setID(int id) { UnitID = id; }
void setValues(int w, int l, string n);
void printValues();
};
void UNIT::setValues(int w, int l, string n)
{
width = w;
length = l;
Name = n;
}
void UNIT::printValues()
{
cout << "[" << Name << " Parameters]" << "--------------------" << endl;
cout << "Unit Id: " << UnitID << endl;
cout << "Width: " << width << endl;
cout << "Length: " << length << endl << endl;
}
//~~~~~~~~~~~~~~~~~
// UpdateUnits()
//~~~~~~~~~~~~~~~~~
list<UNIT*> p_units;
void UpdateUnits()
{
list<UNIT*>::iterator iter;
UNIT *unit;
iter = p_units.begin();
while (iter != p_units.end())
{
//point local unit to object in the list
unit = *iter;
//various functions of the UNIT class
unit->printValues();
//tell game that the entity has been updated??? p.161 of A2GD.
system("pause");
++iter;
}
}
//~~~~~~~~~~~~~~~~~
// addUNIT()
//~~~~~~~~~~~~~~~~~
void addUNIT(UNIT *unit)
{
static int id = 0;
unit->setID(id);
p_units.push_back(unit);
id++;
}
//~~~~~~~~~~~~~~~~~
// accessUNIT()
//~~~~~~~~~~~~~~~~~
void accessUNIT()
{
list<UNIT*>::iterator sUnit;
string name; //this it the sting to hold the unit to search for
UNIT* tUnit; //this is the temporary unit
cout << "What unit to you want to look for: ";
getline(cin, name);
sUnit = p_units.begin();
//search through all of the UNITs
while (sUnit != p_units.end())
{
tUnit = (UNIT*) *sUnit;
if (tUnit->Name == name)
{
cout << name << "Was found";
}
else
{
cout << name << "Was not found";
}
}
}
//~~~~~~~~~~~~~~~~~
// Main
//~~~~~~~~~~~~~~~~~
int main()
{
UNIT* unit; //temporary unit pointer
string tName; //temporary name
int tWidth, tLength; //temporary width/length
//find out how many units the player wants to add
cout << "How many units do you want to add? ";
cin >> MAX;
system("cls");
//allow user to input the parameters for as many units as specified (MAX)
for (int n=0; n < MAX; n++)
{
unit = new UNIT;
cout << "New Unit Name: ";
cin >> tName;
cout << "New Unit Width: ";
cin >> tWidth;
cout << "New Unit Length: ";
cin >> tLength;
unit->Name = tName;
unit->width = tWidth;
unit->length = tLength;
//adds the unit to the manager
addUNIT(unit);
system("cls");
}
accessUNIT();
return 0;
}