所以我在我的客户端代码中的while循环中有一个off-by-one错误 - 由于某种原因,代码在执行10时执行了11次(即使它实际上没有打印出第11项)。我不确定错误在哪里(我甚至打印出条件测试并且它正确递增,它只是要求另一个玩家不应该这样做)
client.cpp
//Client File
#include "sortedNFL.h"
#include <iostream>
#include <string>
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 << "Enter your player's first name: ";
cin >> teamPlayer.firstName;
cout << "Enter your player's last name: ";
cin >> teamPlayer.lastName;
cout << "Enter your player's position: ";
cin.ignore();
getline(cin,teamPlayer.position);
cout << "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 << "Would you like to enter another player? (Y/N): "; //check if user wants to add another player
cin >> inputFlag;
if(inputFlag == 'N' || inputFlag == 'n') { //if user is done adding players
inputCheck = false; //set check to false (break while loop)
cout << "Would you like to print out the current roster? (Y/N): "; //check if user wants to print roster
cin >> 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 << "The roster is full! We will print it out below."
<< endl
<< 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
}
}
sortedNFL.cpp
#include "sortedNFL.h"
#include <iostream>
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->next;
delete tempPtr;
}
length = 0;
}
NFL SortedNFL::GetItem(NFL& playerRequested, bool& 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->player.lastName);
while (moreToSearch && !found) //while there is more to search and item is not found
{
if (compare_result > 0)
{
location = location->next;
moreToSearch = (location != NULL);
}
else if (compare_result == 0 && playerRequested.firstName.compare(location->player.firstName) == 0)
{
found = true;
playerRequested = location->player;
}
else if (compare_result < 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->player.lastName);
if (compare_result > 0)
{
predLoc = location;
location = location->next;
moreToSearch = (location != NULL);
}
else if (compare_result < 0)
{
moreToSearch = false;
}
else if (compare_result == 0) {
int compare_result_firstname = inputPlayer.firstName.compare(location->player.firstName);
if (compare_result_firstname > 0)
{
predLoc = location;
location = location->next;
moreToSearch = (location != NULL);
}
else if(compare_result_firstname < 0) {
moreToSearch = false;
}
}
}
// Prepare node for insertion
newNode = new NodeType;
newNode->player = inputPlayer;
// Insert node into list.
if (predLoc == NULL) // Insert as first
{
newNode->next = nflList;
nflList = newNode;
}
else
{
newNode->next = location;
predLoc->next = newNode;
}
length++;
std::cout << length;
}
void SortedNFL::DeleteItem(NFL playerDeleted)
{
NodeType* location = nflList;
NodeType* tempLocation;
// Locate node to be deleted.
int compare_result_lastname = playerDeleted.lastName.compare(location->player.lastName);
int compare_result_firstname = playerDeleted.firstName.compare(location->player.firstName);
if (compare_result_lastname == 0 && compare_result_firstname == 0)
{
tempLocation = location;
nflList = nflList->next; // Delete first node.
}
else
{
while (compare_result_lastname != 0)
location = location->next;
// Delete node at location->next
tempLocation = location->next;
location->next = (location->next)->next;
}
delete tempLocation;
length--;
}
void SortedNFL::ResetList()
{
currentPos = NULL;
}
NFL SortedNFL::GetNextItem()
{
NFL playerRequested;
if (currentPos == NULL)
currentPos = nflList;
playerRequested = currentPos->player;
currentPos = currentPos->next;
return playerRequested;
}
void SortedNFL::PrintList() {
NFL tempPlayer; //holds player item
ResetList(); //reset list to begin printing
for (int i = 1; i <= length; ++i)
{
tempPlayer = GetNextItem();
std::cout << "Player " << i
<< endl;
std::cout << "First Name: " << tempPlayer.firstName
<< endl;
std::cout << "Last Name: " << tempPlayer.lastName
<< endl;
std::cout << "Position: " << tempPlayer.position
<< endl;
std::cout << "School: " << tempPlayer.school
<< endl;
}
}
SortedNFL::~SortedNFL() //class destructor
{
NodeType* tempPtr;
while (nflList != NULL)
{
tempPtr = nflList;
nflList = nflList->next;
delete tempPtr;
}
}