title: Off By One Error within a loop?
body: <p>So I have an off-by-one error in my while loop within my client code - for some reason the code executes 11 times when it should execute 10 (even though it doesn't actually print out the 11th item).  I'm not sure where the error lies (I've even printed out the condition test and it increments properly, it just asks for another player when it shouldn't)</p>
  
  <p>client.cpp</p>
  
  <pre><code>//Client File
  #include "sortedNFL.h"
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  
  using namespace std;
  int main() {
  
    //Declare necessary variables
    NFL teamPlayer; //holds created player
    SortedNFL NFLroster; //create roster list
    bool inputCheck; //flag to check if user wants to add more players
    char inputFlag; //input to check if user wants to add more players
    char printFlag; //character input for user to print
    bool printCheck; //flag to check if user wants to print
  
    //set necessary variables
    inputCheck = true; //set input check to true
  
  
    //begin user input
    while(inputCheck == true) {
      cout &lt;&lt; "Enter your player's first name: ";
      cin &gt;&gt; teamPlayer.firstName;
      cout &lt;&lt; "Enter your player's last name: ";
      cin &gt;&gt; teamPlayer.lastName;
      cout &lt;&lt; "Enter your player's position: ";
      cin.ignore();
      getline(cin,teamPlayer.position);
      cout &lt;&lt; "Enter your player's school: ";
      getline(cin,teamPlayer.school);
  
      if(!NFLroster.IsFull()) { //check if total size is less than 10 (maximum amount of players)
        NFLroster.PutItem(teamPlayer); //add player to list
        cout &lt;&lt; "Would you like to enter another player? (Y/N): "; //check if user wants to add another player
        cin &gt;&gt; inputFlag;
        if(inputFlag == 'N' || inputFlag == 'n') { //if user is done adding players
          inputCheck = false; //set check to false (break while loop)
          cout &lt;&lt; "Would you like to print out the current roster? (Y/N): "; //check if user wants to print roster
          cin &gt;&gt; printFlag;
          if(printFlag == 'Y' || printFlag == 'y') //if user wants to print roster, set check to true
            printCheck = true;
          else if (printFlag == 'N' || printFlag == 'n') { //if user doesn't want to print roster, set check to false
            printCheck = false;
          }
        }
      }
      else if(NFLroster.IsFull()) { //when total players equals 10, print out alert
        inputCheck = false;
        cout &lt;&lt; "The roster is full! We will print it out below."
             &lt;&lt; endl
             &lt;&lt; endl;
      }
    }
    if(NFLroster.IsFull()) //when total size equals ten, print out roster
      NFLroster.PrintList(); //call print function
    else if(printCheck) { //if print check is true, then print list early
      NFLroster.PrintList(); //print out list
    }
  }
  </code></pre>
  
  <p>sortedNFL.cpp</p>
  
  <pre><code>#include "sortedNFL.h"
  #include &lt;iostream&gt;
  
  struct NodeType
  {
    NFL player;
    NodeType* next;
  };
  
  SortedNFL::SortedNFL()  // Class constructor
  {
    length = 0;
    nflList = NULL;
  }
  
  bool SortedNFL::IsFull() const
  {
    if (length == 10)
      return true;
    else
      return false;
  }
  
  int SortedNFL::GetLength() const
  {
    return length;
  }
  
  void SortedNFL::MakeEmpty()
  {
    NodeType* tempPtr;
  
    while (nflList != NULL)
    {
      tempPtr = nflList;
      nflList = nflList-&gt;next;
      delete tempPtr;
    }
    length = 0;
  }
  
  NFL SortedNFL::GetItem(NFL&amp; playerRequested, bool&amp; found)
  {
    bool moreToSearch; //flag for more items to search
    NodeType* location; 
  
    location = nflList; //initial location is first item in NFL list
    found = false; //flag for if item is found
    moreToSearch = (location != NULL);
  
    int compare_result = playerRequested.lastName.compare(location-&gt;player.lastName);
    while (moreToSearch &amp;&amp; !found) //while there is more to search and item is not found
    {
      if (compare_result &gt; 0)
      {
        location = location-&gt;next;
        moreToSearch = (location != NULL);
      }
      else if (compare_result == 0 &amp;&amp; playerRequested.firstName.compare(location-&gt;player.firstName) == 0)
      {
        found = true; 
        playerRequested = location-&gt;player;
      }
      else if (compare_result &lt; 0)
      {
        moreToSearch = false;
      }
    }
    return playerRequested;
  }
  
  void SortedNFL::PutItem(NFL inputPlayer)
  {
    NodeType* newNode;    // pointer to node being inserted
    NodeType* predLoc;    // trailing pointer
    NodeType* location;   // traveling pointer
    bool moreToSearch;
  
    location = nflList;
    predLoc = NULL;
    moreToSearch = (location != NULL);
  
    // Find insertion point.
    while (moreToSearch) //while moreToSearch is true
    {
      int compare_result = inputPlayer.lastName.compare(location-&gt;player.lastName);
      if (compare_result &gt; 0)
      {
        predLoc = location;
        location = location-&gt;next;
        moreToSearch = (location != NULL);
      }
      else if (compare_result &lt; 0)
      {
        moreToSearch = false;
      }
      else if (compare_result == 0) {
        int compare_result_firstname = inputPlayer.firstName.compare(location-&gt;player.firstName);
        if (compare_result_firstname &gt; 0) 
        {
          predLoc = location;
          location = location-&gt;next;
          moreToSearch = (location != NULL);
        }
        else if(compare_result_firstname &lt; 0) {
          moreToSearch = false;   
        }
      }
    }
    // Prepare node for insertion
    newNode = new NodeType;
    newNode-&gt;player = inputPlayer;
    // Insert node into list.
    if (predLoc == NULL)         // Insert as first
    {
      newNode-&gt;next = nflList;
      nflList = newNode;
    }
    else
    {
      newNode-&gt;next = location;
      predLoc-&gt;next = newNode;
    }
    length++;
    std::cout &lt;&lt; length;
  }
  void SortedNFL::DeleteItem(NFL playerDeleted)
  {
    NodeType* location = nflList;
    NodeType* tempLocation;
  
    // Locate node to be deleted.
    int compare_result_lastname = playerDeleted.lastName.compare(location-&gt;player.lastName);
    int compare_result_firstname = playerDeleted.firstName.compare(location-&gt;player.firstName);
    if (compare_result_lastname == 0 &amp;&amp; compare_result_firstname == 0)
    {
      tempLocation = location;
      nflList = nflList-&gt;next;    // Delete first node.
    }
    else
    {
      while (compare_result_lastname != 0)
        location = location-&gt;next;
  
      // Delete node at location-&gt;next
      tempLocation = location-&gt;next;
      location-&gt;next = (location-&gt;next)-&gt;next;
    }
    delete tempLocation;
    length--;
  }
  
  void SortedNFL::ResetList()
  {
    currentPos = NULL;
  } 
  
  NFL SortedNFL::GetNextItem()
  {
    NFL playerRequested;
    if (currentPos == NULL)
      currentPos = nflList;
    playerRequested = currentPos-&gt;player; 
    currentPos = currentPos-&gt;next;
    return playerRequested;
  
  }
  void SortedNFL::PrintList() {
        NFL tempPlayer; //holds player item
        ResetList(); //reset list to begin printing
        for (int i = 1; i &lt;= length; ++i)
        {
          tempPlayer = GetNextItem();
          std::cout &lt;&lt; "Player " &lt;&lt; i
               &lt;&lt; endl;
          std::cout &lt;&lt; "First Name: " &lt;&lt; tempPlayer.firstName
               &lt;&lt; endl;
          std::cout &lt;&lt; "Last Name: " &lt;&lt; tempPlayer.lastName
               &lt;&lt; endl;
          std::cout &lt;&lt; "Position: " &lt;&lt; tempPlayer.position
               &lt;&lt; endl;
          std::cout &lt;&lt; "School: " &lt;&lt; tempPlayer.school
               &lt;&lt; endl;
        }
  }
  SortedNFL::~SortedNFL() //class destructor
  {
    NodeType* tempPtr;
  
    while (nflList != NULL)
    {
      tempPtr = nflList;
      nflList = nflList-&gt;next;
      delete tempPtr;
    }
  }
  </code></pre>