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
	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

		//tell game that the entity has been updated??? p.161 of A2GD.


// addUNIT()
void addUNIT(UNIT *unit)
	static int id = 0;

// 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";
			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;

	//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


	return 0;

The reason this is happening is there is a newline left in the buffer from line 145. when you call getline() in you accessUNIT() function it reads in the newline and that's all. To fix it put cin.get(); on line 146. Try reading this for more information.

Be a part of the DaniWeb community

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