Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi there, I am doing a c++ program where I need to print off the last man of eve

ID: 3597459 • Letter: H

Question

Hi there, I am doing a c++ program where I need to print off the last man of every hat number. For example the outline of the code is below:

In the QueueMain.cpp, the countOff() method displays the name of each officer removed from the queue, in the order in which they are removed, and displays the name of the officer that goes for help. For example, the output for the queue of officers above with a count of 4 would appear as:

Count is: 4

Officers leaving the queue are:
Ewell, Richard Stoddert, Lieutenant General
Johnston, Joseph Eggleston, General
Stuart, James Ewell Brown "JEB", General
Early, Jubal Anderson, Lieutenant General
Lee, Robert Edward, General
Beauregard, Pierre Gustave Toutant, General
Jackson, Thomas Jonathan "Stonewall", Major General
Bragg, Braxton, General
Pickett, George Edward, Brigadier General
Longstreet, James, Lieutenant General
Van Dorn, Earl, Major General
Hood, John Bell, General

The officer going for help is: Forrest, Nathan Bedford, Lieutenant General

----------------------------MY OUTPUT

Hays, Harry Thompson, Major General
Ewell, Richard Stoddert, Lieutenant General
Smith, Martin Luther, Major General
Rosser, Thomas Lafayette, Major General
Bowen, John Stevens, Major General
Hardee, William Joseph, Lieutenant General
Longstreet, James, Lieutenant General
Lee, William Henry Fitzhugh, Major General
Forney, John Horace, Major General
Rodes, Robert Emmett, Major General
Jackson, Thomas Jonathan 'Stonewall', Major General <--- Not supose to be there...

The officer going for help is:
Jackson, Thomas Jonathan 'Stonewall', Major General

---------------------------------------------------------------------------

My program is printing the officer that is leaving correctly, BUT his nam still remians at the end of the list.. My program is below along with the other files required. Please help me fix my countOff() method so that the last guys name is removed from the list too.. Thanks !

-----------------------main.cpp---------------------------

/**

* This program creates a queue of Civil War officers who agree to a pact

* to determine which of them is to go for help because they are surrounded

* by Union forces and there is no chance for victory without reinforcements.

* They must choose an officer to go for help. The officers form a queue and

* they pick a number from a hat. Beginning with the first officer in the

* queue, they begin to count. As each officer counts off, he moves to the

* end of the queue. When the count reaches the number that was picked, that

* officer is removed from the queue, and the count begins again with the

* next man. The last officer that remains in the queue is the one that rides

* away on their only horse, Little Sorrel, to summon help.

*

* The program inputs the list of officers from a text file and puts each name

* into a queue. The program is incomplete because the countOff() method has

* not been implemented.

*/  

#include

#include

#include

#include

#include

#include

#include

#include "LinkedQueue.h"

using namespace std;

/**

* Name of input file containing officer names

*/

const char INPUT_FILE[] = "officers.txt";

/**

* Inputs data from a text file and places each officer's name into the

* queue passed as a parameter.

* @param officers - dynamic queue of Confederate officers in the Civil War

*/

void loadQueue(LinkedQueue &officers);

/**

* Displays a list of officers removed from the queue and also displays

* the name of the officer going for help. Retrieves officers from the

* officers queue, one at a time, counting as each is retrieved. If the

* count is not equal to hatNumber, the officer is put back into the

* queue. When the count reaches hatNumber, that officer is removed from

* the queue permanently (i.e., not put back into the queue), and the

* officer's name is output to the screen. The last officer remaining in

* the queue is the one shown as going for help.

* @param officers - dynamic queue of Confederate officers in the Civil War

* @param hatNumber - the number drawn from the hat, used to count off

*/

void countOff (LinkedQueue officers, int hatNumber);

int main (void)

{

ifstream inFile(INPUT_FILE); // declare and open the input file

int count; // number used for counting off

LinkedQueue officers; // queue containing officer names

string officerName; // name of officer in front of queue

string firstName; // name of first officer in original queue

loadQueue(officers);

// Display list of officers in original queue

cout << "Officers in original queue: " << endl;

firstName = officers.peekFront();

cout << firstName << endl;

officers.dequeue();

officers.enqueue(firstName);

officerName = officers.peekFront();

while (firstName != officerName)

{

cout << officerName << endl;

officers.dequeue();

officers.enqueue(officerName);

officerName = officers.peekFront();

}

// Test countOff() method using various numbers drawn from the hat

count = 5;

cout << endl << endl<< "Count is: " << count << endl;

countOff (officers, count);

count = 8;

cout << endl << endl << "Count is: " << count << endl;

countOff (officers, count);

return EXIT_SUCCESS;

}

void loadQueue(LinkedQueue &officers)

{

ifstream inFile(INPUT_FILE); // declare and open the input file

string officerName; // name of officer input from file

if (!inFile)

cout << "Error opening file for input: " << INPUT_FILE << endl;

else

{

getline (inFile, officerName);

while (!inFile.eof())

{

officers.enqueue(officerName);

getline (inFile, officerName);

}

inFile.close();

}

}

void countOff (LinkedQueue officers, int count) ///// THIS IS THE METHOD GIVING ME PROBLEMS

{

//Write method here

int hatNumber = count;

int num = 1;

string lastMan;

string tempOfficer;

string nameRemove;

while(!officers.isEmpty())

{

if(num != hatNumber)

{

tempOfficer = officers.peekFront();

officers.dequeue();

officers.enqueue(tempOfficer);

num++;

}

  

  

else

{

lastMan = officers.peekFront();

officers.dequeue();

num = 1;

cout << lastMan << endl;

}

  

  

  

  

}//end while

cout << endl;

cout << "The officer going for help is: " << endl ;

cout << lastMan << endl;  

  

}

------------------------QueueInterface.h

// Created by Frank M. Carrano and Tim Henry.
// Copyright (c) 2013 __Pearson Education__. All rights reserved.

/** Listing 13-1.
@file QueueInterface.h */

#ifndef _QUEUE_INTERFACE
#define _QUEUE_INTERFACE

template
class QueueInterface

{
public:
/** Sees whether this queue is empty.
@return True if the queue is empty, or false if not. */
virtual bool isEmpty() const = 0;

/** Adds a new entry to the back of this queue.
@post If the operation was successful, newEntry is at the
back of the queue.
@param newEntry The object to be added as a new entry.
@return True if the addition is successful or false if not. */
virtual bool enqueue(const ItemType& newEntry) = 0;

/** Removes the front of this queue.
@post If the operation was successful, the front of the queue
has been removed.
@return True if the removal is successful or false if not. */
virtual bool dequeue() = 0;

/** Returns the front of this queue.
@pre The queue is not empty.
@post The front of the queue has been returned, and the
queue is unchanged.
@return The front of the queue. */
virtual ItemType peekFront() const = 0;
}; // end QueueInterface
#endif

---------------------------PrecondViolatedExcep.h

// Created by Frank M. Carrano and Tim Henry.
// Copyright (c) 2013 __Pearson Education__. All rights reserved.

/** Listing 7-5.
@file PrecondViolatedExcep.h */

#ifndef _PRECOND_VIOLATED_EXCEP
#define _PRECOND_VIOLATED_EXCEP

#include <stdexcept>
#include <string>

using namespace std;

class PrecondViolatedExcep : public logic_error
{
public:
PrecondViolatedExcep(const string& message = "");
}; // end PrecondViolatedExcep
#endif

---------------------PrecondViolatedExcep.cpp

// Created by Frank M. Carrano and Tim Henry.
// Copyright (c) 2013 __Pearson Education__. All rights reserved.

/** Listing 7-6.
@file PrecondViolatedExcep.cpp */
#include "PrecondViolatedExcep.h"  

PrecondViolatedExcep::PrecondViolatedExcep(const string& message): logic_error("Precondition Violated Exception: " + message)
{
} // end constructor

// End of implementation file.

-------------------Node.cpp

// Created by Frank M. Carrano and Tim Henry.
// Copyright (c) 2013 __Pearson Education__. All rights reserved.

/** @file Node.cpp
Listing 4-2 */
#include "Node.h"
#include <cstddef>

template
Node::Node() : next(nullptr)
{
} // end default constructor

template
Node::Node(const ItemType& anItem) : item(anItem), next(nullptr)
{
} // end constructor

template
Node::Node(const ItemType& anItem, Node* nextNodePtr) :
item(anItem), next(nextNodePtr)
{
} // end constructor

template
void Node::setItem(const ItemType& anItem)
{
item = anItem;
} // end setItem

template
void Node::setNext(Node* nextNodePtr)
{
next = nextNodePtr;
} // end setNext

template
ItemType Node::getItem() const
{
return item;
} // end getItem

template
Node* Node::getNext() const
{
return next;
} // end getNext

-------------------Node.h

// Created by Frank M. Carrano and Tim Henry.
// Copyright (c) 2013 __Pearson Education__. All rights reserved.

/** @file Node.h
Listing 4-1 */
#ifndef _NODE
#define _NODE

template
class Node
{
private:
ItemType item; // A data item
Node* next; // Pointer to next node

public:
Node();
Node(const ItemType& anItem);
Node(const ItemType& anItem, Node* nextNodePtr);
void setItem(const ItemType& anItem);
void setNext(Node* nextNodePtr);
ItemType getItem() const ;
Node* getNext() const ;
}; // end Node

#include "Node.cpp"
#endif

----------------------LinkedQueue.cpp

// Created by Frank M. Carrano and Tim Henry.

// Copyright (c) 2013 __Pearson Education__. All rights reserved.

/** @file LinkedQueue.cpp */

#include "LinkedQueue.h"  

#include <cassert>

template

LinkedQueue::LinkedQueue()

{

backPtr = nullptr;

frontPtr = nullptr;

} // end default constructor

template

LinkedQueue::LinkedQueue(const LinkedQueue& aQueue)

{ // Implementation left as an exercise (Exercise 1).

Node* origChainPtr = aQueue.frontPtr; // Points to nodes in original chain

if (origChainPtr == nullptr)

{

frontPtr = nullptr; // Original queue is empty

backPtr = nullptr;

}

else

{

// Copy first node

frontPtr = new Node();

frontPtr->setItem(origChainPtr->getItem());

  

// Advance original-chain pointer

origChainPtr = origChainPtr->getNext();

// Copy remaining nodes

Node* newChainPtr = frontPtr; // Points to last node in new chain

while (origChainPtr != nullptr)

{

// Get next item from original chain

ItemType nextItem = origChainPtr->getItem();

// Create a new node containing the next item

Node* newNodePtr = new Node(nextItem);  

// Link new node to end of new chain

newChainPtr->setNext(newNodePtr);

// Advance pointers

newChainPtr = newChainPtr->getNext();

origChainPtr = origChainPtr->getNext();

} // end while

  

newChainPtr->setNext(nullptr); // Flag end of chain

backPtr = newChainPtr;

} // end if

} // end copy constructor

template

LinkedQueue::~LinkedQueue()

{

while (!isEmpty())

dequeue();

assert ( (backPtr == nullptr) && (frontPtr == nullptr) );

} // end destructor

template

bool LinkedQueue::isEmpty() const

{

return backPtr == nullptr;

} // end isEmpty

template

bool LinkedQueue::enqueue(const ItemType& newEntry)

{

Node* newNodePtr = new Node(newEntry);

// Insert the new node

if (isEmpty())

frontPtr = newNodePtr; // Insertion into empty queue

else

backPtr->setNext(newNodePtr); // Insertion into nonempty queue

backPtr = newNodePtr; // New node is at back

return true;

} // end enqueue

template

bool LinkedQueue::dequeue()

{

bool result = false;

if (!isEmpty())

{

// Queue is not empty; delete front

Node* nodeToDeletePtr = frontPtr;

if (frontPtr == backPtr)

{ // Special case: one node in queue

frontPtr = nullptr;

backPtr = nullptr;

}

else

frontPtr = frontPtr->getNext();

// Return deleted node to system

nodeToDeletePtr->setNext(nullptr);

delete nodeToDeletePtr;

nodeToDeletePtr = nullptr;

  

result = true;

} // end if

return result;

} // end dequeue

template

ItemType LinkedQueue::peekFront() const throw(PrecondViolatedExcep)

{

if (isEmpty())

throw PrecondViolatedExcep("getFront() called with empty queue.");

// Queue is not empty; return front

return frontPtr->getItem();

} // end peekFront

// End of implementation file.

------------------------LinkedQueue.h

// Created by Frank M. Carrano and Tim Henry.

// Copyright (c) 2013 __Pearson Education__. All rights reserved.

/** ADT queue: Link-based implementation.

Listing 14-3.

@file LinkedQueue.h */

#ifndef _LINKED_QUEUE

#define _LINKED_QUEUE

#include "QueueInterface.h"

#include "Node.h"

#include "PrecondViolatedExcep.h"

template

class LinkedQueue : public QueueInterface

{

private:

// The queue is implemented as a chain of linked nodes that has

// two external pointers, a head pointer for front of the queue and

// a tail pointer for the back of the queue.

Node* backPtr;

Node* frontPtr;

public:

LinkedQueue();

LinkedQueue (const LinkedQueue& aQueue);

~LinkedQueue();

bool isEmpty() const;

bool enqueue(const ItemType& newEntry);

bool dequeue();

/** @throw PrecondViolatedExcep if the queue is empty */

ItemType peekFront() const throw(PrecondViolatedExcep);

}; // end LinkedQueue

#include "LinkedQueue.cpp"

#endif

Hopefully you can help... Thanks !

Explanation / Answer

#include

#include

#include

#include

#include

#include

#include

#include "LinkedQueue.h"

using namespace std;

/**

* Name of input file containing officer names

*/

const char INPUT_FILE[] = "officers.txt";

/**

* Inputs data from a text file and places each officer's name into the

* queue passed as a parameter.

* @param officers - dynamic queue of Confederate officers in the Civil War

*/

void loadQueue(LinkedQueue &officers);

/**

* Displays a list of officers removed from the queue and also displays

* the name of the officer going for help. Retrieves officers from the

* officers queue, one at a time, counting as each is retrieved. If the

* count is not equal to hatNumber, the officer is put back into the

* queue. When the count reaches hatNumber, that officer is removed from

* the queue permanently (i.e., not put back into the queue), and the

* officer's name is output to the screen. The last officer remaining in

* the queue is the one shown as going for help.

* @param officers - dynamic queue of Confederate officers in the Civil War

* @param hatNumber - the number drawn from the hat, used to count off

*/

void countOff (LinkedQueue officers, int hatNumber);

int main (void)

{

ifstream inFile(INPUT_FILE); // declare and open the input file

int count; // number used for counting off

LinkedQueue officers; // queue containing officer names

string officerName; // name of officer in front of queue

string firstName; // name of first officer in original queue

loadQueue(officers);

// Display list of officers in original queue

cout << "Officers in original queue: " << endl;

firstName = officers.peekFront();

cout << firstName << endl;

officers.dequeue();

officers.enqueue(firstName);

officerName = officers.peekFront();

while (firstName != officerName)

{

cout << officerName << endl;

officers.dequeue();

officers.enqueue(officerName);

officerName = officers.peekFront();

}

// Test countOff() method using various numbers drawn from the hat

count = 5;

cout << endl << endl<< "Count is: " << count << endl;

countOff (officers, count);

count = 8;

cout << endl << endl << "Count is: " << count << endl;

countOff (officers, count);

return EXIT_SUCCESS;

}

void loadQueue(LinkedQueue &officers)

{

ifstream inFile(INPUT_FILE); // declare and open the input file

string officerName; // name of officer input from file

if (!inFile)

cout << "Error opening file for input: " << INPUT_FILE << endl;

else

{

getline (inFile, officerName);

while (!inFile.eof())

{

officers.enqueue(officerName);

getline (inFile, officerName);

}

inFile.close();

}

}

void countOff (LinkedQueue officers, int count) ///// THIS IS THE METHOD GIVING ME PROBLEMS

{

//Write method here

int hatNumber = count;

int num = 1;

string lastMan;

string tempOfficer;

string nameRemove;

while(!officers.isEmpty())

{

if(num != hatNumber)

{

tempOfficer = officers.peekFront();

officers.dequeue();

officers.enqueue(tempOfficer);

num++;

}

  

  

else

{

lastMan = officers.peekFront();

officers.dequeue();

num = 1;

cout << lastMan << endl;

}

  

  

  

  

}//end while

cout << endl;

cout << "The officer going for help is: " << endl ;

cout << lastMan << endl;  

  

}

------------------------QueueInterface.h

// Created by Frank M. Carrano and Tim Henry.
// Copyright (c) 2013 __Pearson Education__. All rights reserved.

/** Listing 13-1.
@file QueueInterface.h */

#ifndef _QUEUE_INTERFACE
#define _QUEUE_INTERFACE

template
class QueueInterface

{
public:
/** Sees whether this queue is empty.
@return True if the queue is empty, or false if not. */
virtual bool isEmpty() const = 0;

/** Adds a new entry to the back of this queue.
@post If the operation was successful, newEntry is at the
back of the queue.
@param newEntry The object to be added as a new entry.
@return True if the addition is successful or false if not. */
virtual bool enqueue(const ItemType& newEntry) = 0;

/** Removes the front of this queue.
@post If the operation was successful, the front of the queue
has been removed.
@return True if the removal is successful or false if not. */
virtual bool dequeue() = 0;

/** Returns the front of this queue.
@pre The queue is not empty.
@post The front of the queue has been returned, and the
queue is unchanged.
@return The front of the queue. */
virtual ItemType peekFront() const = 0;
}; // end QueueInterface
#endif

---------------------------PrecondViolatedExcep.h

// Created by Frank M. Carrano and Tim Henry.
// Copyright (c) 2013 __Pearson Education__. All rights reserved.

/** Listing 7-5.
@file PrecondViolatedExcep.h */

#ifndef _PRECOND_VIOLATED_EXCEP
#define _PRECOND_VIOLATED_EXCEP

#include <stdexcept>
#include <string>

using namespace std;

class PrecondViolatedExcep : public logic_error
{
public:
PrecondViolatedExcep(const string& message = "");
}; // end PrecondViolatedExcep
#endif

---------------------PrecondViolatedExcep.cpp

// Created by Frank M. Carrano and Tim Henry.
// Copyright (c) 2013 __Pearson Education__. All rights reserved.

/** Listing 7-6.
@file PrecondViolatedExcep.cpp */
#include "PrecondViolatedExcep.h"  

PrecondViolatedExcep::PrecondViolatedExcep(const string& message): logic_error("Precondition Violated Exception: " + message)
{
} // end constructor

// End of implementation file.

-------------------Node.cpp

// Created by Frank M. Carrano and Tim Henry.
// Copyright (c) 2013 __Pearson Education__. All rights reserved.

/** @file Node.cpp
Listing 4-2 */
#include "Node.h"
#include <cstddef>

template
Node::Node() : next(nullptr)
{
} // end default constructor

template
Node::Node(const ItemType& anItem) : item(anItem), next(nullptr)
{
} // end constructor

template
Node::Node(const ItemType& anItem, Node* nextNodePtr) :
item(anItem), next(nextNodePtr)
{
} // end constructor

template
void Node::setItem(const ItemType& anItem)
{
item = anItem;
} // end setItem

template
void Node::setNext(Node* nextNodePtr)
{
next = nextNodePtr;
} // end setNext

template
ItemType Node::getItem() const
{
return item;
} // end getItem

template
Node* Node::getNext() const
{
return next;
} // end getNext

-------------------Node.h

// Created by Frank M. Carrano and Tim Henry.
// Copyright (c) 2013 __Pearson Education__. All rights reserved.

/** @file Node.h
Listing 4-1 */
#ifndef _NODE
#define _NODE

template
class Node
{
private:
ItemType item; // A data item
Node* next; // Pointer to next node

public:
Node();
Node(const ItemType& anItem);
Node(const ItemType& anItem, Node* nextNodePtr);
void setItem(const ItemType& anItem);
void setNext(Node* nextNodePtr);
ItemType getItem() const ;
Node* getNext() const ;
}; // end Node

#include "Node.cpp"
#endif

----------------------LinkedQueue.cpp

// Created by Frank M. Carrano and Tim Henry.

// Copyright (c) 2013 __Pearson Education__. All rights reserved.

/** @file LinkedQueue.cpp */

#include "LinkedQueue.h"  

#include <cassert>

template

LinkedQueue::LinkedQueue()

{

backPtr = nullptr;

frontPtr = nullptr;

} // end default constructor

template

LinkedQueue::LinkedQueue(const LinkedQueue& aQueue)

{ // Implementation left as an exercise (Exercise 1).

Node* origChainPtr = aQueue.frontPtr; // Points to nodes in original chain

if (origChainPtr == nullptr)

{

frontPtr = nullptr; // Original queue is empty

backPtr = nullptr;

}

else

{

// Copy first node

frontPtr = new Node();

frontPtr->setItem(origChainPtr->getItem());

  

// Advance original-chain pointer

origChainPtr = origChainPtr->getNext();

// Copy remaining nodes

Node* newChainPtr = frontPtr; // Points to last node in new chain

while (origChainPtr != nullptr)

{

// Get next item from original chain

ItemType nextItem = origChainPtr->getItem();

// Create a new node containing the next item

Node* newNodePtr = new Node(nextItem);  

// Link new node to end of new chain

newChainPtr->setNext(newNodePtr);

// Advance pointers

newChainPtr = newChainPtr->getNext();

origChainPtr = origChainPtr->getNext();

} // end while

  

newChainPtr->setNext(nullptr); // Flag end of chain

backPtr = newChainPtr;

} // end if

} // end copy constructor

template

LinkedQueue::~LinkedQueue()

{

while (!isEmpty())

dequeue();

assert ( (backPtr == nullptr) && (frontPtr == nullptr) );

} // end destructor

template

bool LinkedQueue::isEmpty() const

{

return backPtr == nullptr;

} // end isEmpty

template

bool LinkedQueue::enqueue(const ItemType& newEntry)

{

Node* newNodePtr = new Node(newEntry);

// Insert the new node

if (isEmpty())

frontPtr = newNodePtr; // Insertion into empty queue

else

backPtr->setNext(newNodePtr); // Insertion into nonempty queue

backPtr = newNodePtr; // New node is at back

return true;

} // end enqueue

template

bool LinkedQueue::dequeue()

{

bool result = false;

if (!isEmpty())

{

// Queue is not empty; delete front

Node* nodeToDeletePtr = frontPtr;

if (frontPtr == backPtr)

{ // Special case: one node in queue

frontPtr = nullptr;

backPtr = nullptr;

}

else

frontPtr = frontPtr->getNext();

// Return deleted node to system

nodeToDeletePtr->setNext(nullptr);

delete nodeToDeletePtr;

nodeToDeletePtr = nullptr;

  

result = true;

} // end if

return result;

} // end dequeue

template

ItemType LinkedQueue::peekFront() const throw(PrecondViolatedExcep)

{

if (isEmpty())

throw PrecondViolatedExcep("getFront() called with empty queue.");

// Queue is not empty; return front

return frontPtr->getItem();

} // end peekFront

// End of implementation file.

------------------------LinkedQueue.h

// Created by Frank M. Carrano and Tim Henry.

// Copyright (c) 2013 __Pearson Education__. All rights reserved.

/** ADT queue: Link-based implementation.

Listing 14-3.

@file LinkedQueue.h */

#ifndef _LINKED_QUEUE

#define _LINKED_QUEUE

#include "QueueInterface.h"

#include "Node.h"

#include "PrecondViolatedExcep.h"

template

class LinkedQueue : public QueueInterface

{

private:

// The queue is implemented as a chain of linked nodes that has

// two external pointers, a head pointer for front of the queue and

// a tail pointer for the back of the queue.

Node* backPtr;

Node* frontPtr;

public:

LinkedQueue();

LinkedQueue (const LinkedQueue& aQueue);

~LinkedQueue();

bool isEmpty() const;

bool enqueue(const ItemType& newEntry);

bool dequeue();

/** @throw PrecondViolatedExcep if the queue is empty */

ItemType peekFront() const throw(PrecondViolatedExcep);

}; // end LinkedQueue

#include "LinkedQueue.cpp"

#endif

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote