user3458030
user3458030

Reputation: 1

Code is looping infinitely and I can't figure out why C++

there's a section of code in my program that is looping infinitely and I can't for the life of my figure out why. I'm a very novice programmer and any insight would be very much appreciated. The input I'm focusing on is as follows: (it's not all of the input, but it's enough to throw off my program)

A NY 1717 FELSTEIN       SHAZZI
A IN 1764 ALSTOTT        GORDON
A WI 1679 SCANLAN        THOMAS

The A means that it's adding the player to the list, and it is, but it's not getting past Mr. Shazzi Felstein.

the culprit code is as follows:

class Tournament
{
private:
   RatingAdjustmentLevel chart[MAX_LEVELS];
   PlayerList            allPlayers;
   char command;

   void nametoPlayer()
   {
      string Last, First;
      cin >> Last >> First;
   }

   void processOneCommand(char command)
   {
      if (command == 'A')
      {
         int result;
         string lastName, firstName;
         Player entry, entry2;
         entry.Read();
         result = allPlayers.Add(entry);
         if (result == LIST_FULL)
            cout << "Cannot perform add operation - the list is FULL!";
         else if (result == IN_LIST)
         {
            cout << "Cannot perform add operation - ";
            cout << entry.GetFirst() << " " << entry.GetLast();
            cout << " is already in the list!" << endl;
         }
         else
         {
            cout << "Performed add operation - for ";
            cout << entry.GetFirst() << " " << entry.GetLast() << ".";
            cout << endl;
         }
      }
      else if (command == 'D')
      {
         int result;
         string lastName, firstName;
         Player entry, entry2;
         entry.ReadName();
         firstName = entry.GetFirst();
         lastName = entry.GetLast();
         allPlayers.Remove(entry);
         if (allPlayers.Remove(entry) == true)
         {
            cout << "Performed delete operation - for ";
            cout << entry.GetFirst() << " " << entry.GetLast() << ".";
            cout << endl;
         }
         else if (allPlayers.Remove(entry) == false)
         {
            cout << "Cannot perform delete operation - for ";
            cout << entry.GetFirst() << " " << entry.GetLast() << ".";
            cout << endl;
         }
      }
      else;
   }
public:
   // Reads the rating adjustment chart.
   void readRatingAdjustmentLevelChart()
   {
      for (int i = 0; i < MAX_LEVELS; i++)
         chart[i].Read();
   }

   // Reads the players' list to allPlayers.
   void readPlayerList()
   {
      allPlayers.Read();
   }

   // Displays all players in allPlayers.
   void printPlayerList() const
   {
      allPlayers.Print();
   }

   // Processes all transactions until the end of file.
   void processAllTransactions()
   {
      cin >> command;
      while (!cin.eof())
      {
         processOneCommand(command);
         cin >> command;
      }
   }
};   // Tournament

int main()
{
   Tournament tournament;

   tournament.readRatingAdjustmentLevelChart();

   tournament.readPlayerList();

   cout << "\nThe following is an echo of the original "
      << "players' list.\n" << endl;
   tournament.printPlayerList();

   cout << endl << "Processing transactions..." << endl;
   tournament.processAllTransactions();

   cout << "\nThe following is the final players' list." << endl;
   tournament.printPlayerList();

   return 0;
}

The issue lies in the processAllTransactions function. The while loop goes infinitely, it never actually recognizes the end of file. my output is as follows:

  13: Processing transactions...
  14: Performed add operation - for SHAZZI FELSTEIN.
  15: Cannot perform add operation -   is already in the list!
  16: Cannot perform add operation -   is already in the list!
  17: Cannot perform add operation -   is already in the list!
  18: Cannot perform add operation -   is already in the list!
  19: Cannot perform add operation -   is already in the list!
  20: Cannot perform add operation -   is already in the list!
  21: Cannot perform add operation -   is already in the list!

and so on forever. It correctly adds the first person to my list of people, and recognizes that he's already there all of the other times it loops, it just never reaches the desired end of file. Please help! I've trying all sorts of things for hours!

EDIT: Here is my complete code, so that all the functions can be viewed:

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

const int  NOT_FOUND = -1;
const int  MAX_PLAYERS = 25;
const int  MAX_LEVELS = 11;
const int  LIST_FULL = 0;
const int  IN_LIST = 1;
const int  ADDED = 2;

class RatingAdjustmentLevel
{
private:
   int ratingDiff;
   int adjAmtNotUpset;
   int adjAmtUpset;

public:
   void Read()
   {
      cin >> ratingDiff >> adjAmtNotUpset >> adjAmtUpset;
   }
   int  GetDiff() const
   {
      return ratingDiff;
   }
   int GetAdjAmtNotUpset() const
   {
      return adjAmtNotUpset;
   }
   int GetAdjAmtUpset() const
   {
      return adjAmtUpset;
   }
};

class Player
{
private:
   string state;
   int rating;
   string last;
   string first;

public:
   void Read()
   {
      cin >> state >> rating >> last >> first;
   }
   void ReadName()
   {
      cin >> last, first;
   }
   string GetFirst() const
   {
      return first;
   }
   string GetLast() const
   {
      return last;
   }
   int GetRating() const
   {
      return rating;
   }
   void Print() const
   {
      cout << setiosflags(ios::left);
      cout << setw(8) << rating << setw(17) << last;
      cout << setw(18) << first << setw(5) << state << endl;
   }
   bool Equals(const Player& p) const
   {
      if (p.GetFirst() == first && p.GetLast() == last)
         return true;
      else
         return false;
   }
   void UpdateRating(int points)
   {
      rating += points;
   }
};

class PlayerList
{
private:
   Player list[MAX_PLAYERS];
   int    numPlayers;
   int Find(const Player& p) const
   {
      for (int i = 0; i < numPlayers; i++)
      {
         if (list[i].Equals(p) == true)
         {
            return i;
         }
      }
      return NOT_FOUND;
   }


public:
   void Read()
   {
      cin >> numPlayers;
      for (int i = 0; i < numPlayers; i++)
      {
         list[i].Read();

      }
   }
   int Add(const Player& p)
   {
      if (numPlayers >= MAX_PLAYERS)
      {
         return LIST_FULL;
      }
      else if (Find(p) != NOT_FOUND)
      {
         return IN_LIST;
      }
      else
      {
         list[numPlayers].Read();
         numPlayers++;
         return ADDED;
      }
   }
   bool Remove(const Player& p)
   {
      int index = Find(p);
      if (index == NOT_FOUND)
      {
         return false;
      }
      else
      {
         for (int i = index; i < numPlayers; i++)
         {
            list[i] = list[i + 1];
         }
         numPlayers--;
         return true;
      }
   }
   void Print() const
   {
      cout << "The current number of table tennis player is ";
      cout << numPlayers << "." << endl;
      cout << setiosflags(ios::left);
      cout << setw(8) << "RATING" << setw(17) << "LAST NAME";
      cout << setw(18) << "FIRST NAME" << setw(5) << "STATE" << endl;
      for (int i = 0; i < numPlayers; i++)
      {
         list[i].Print();
      }
   }
   void ProcessMatchResult(const RatingAdjustmentLevel chart[],
      Player& p1, Player& p2, int& adjAmt)
   {
      int chartIndex = LIST_FULL;
      int decreaseFlip = NOT_FOUND;
      int p1index = Find(p1);
      int p2index = Find(p2);
      int p1rating = list[p1index].GetRating();
      int p2rating = list[p2index].GetRating();
      int difference = p1rating - p2rating;
      for (int i = 0; i < MAX_LEVELS; i++)
      {
         if (abs(difference) > chart[i].GetDiff())
            chartIndex++;
      }
      if (difference >= 0)
         adjAmt = chart[chartIndex].GetAdjAmtNotUpset();
      else
         adjAmt = chart[chartIndex].GetAdjAmtUpset();
      p1.UpdateRating(adjAmt);
      int loser = adjAmt * decreaseFlip;
      p2.UpdateRating(loser);
   }
};

class Tournament
{
private:
   RatingAdjustmentLevel chart[MAX_LEVELS];
   PlayerList            allPlayers;
   char command;

   void nametoPlayer()
   {
      string Last, First;
      cin >> Last >> First;
   }

   void processOneCommand(char command)
   {
      if (command == 'A')
      {
         int result;
         string lastName, firstName;
         Player entry, entry2;
         entry.Read();
         result = allPlayers.Add(entry);
         if (result == LIST_FULL)
            cout << "Cannot perform add operation - the list is FULL!";
         else if (result == IN_LIST)
         {
            cout << "Cannot perform add operation - ";
            cout << entry.GetFirst() << " " << entry.GetLast();
            cout << " is already in the list!" << endl;
         }
         else
         {
            cout << "Performed add operation - for ";
            cout << entry.GetFirst() << " " << entry.GetLast() << ".";
            cout << endl;
         }
      }
      else if (command == 'D')
      {
         int result;
         string lastName, firstName;
         Player entry, entry2;
         entry.ReadName();
         firstName = entry.GetFirst();
         lastName = entry.GetLast();
         allPlayers.Remove(entry);
         if (allPlayers.Remove(entry) == true)
         {
            cout << "Performed delete operation - for ";
            cout << entry.GetFirst() << " " << entry.GetLast() << ".";
            cout << endl;
         }
         else if (allPlayers.Remove(entry) == false)
         {
            cout << "Cannot perform delete operation - for ";
            cout << entry.GetFirst() << " " << entry.GetLast() << ".";
            cout << endl;
         }
      }
      else;
   }
public:
   // Reads the rating adjustment chart.
   void readRatingAdjustmentLevelChart()
   {
      for (int i = 0; i < MAX_LEVELS; i++)
         chart[i].Read();
   }

   // Reads the players' list to allPlayers.
   void readPlayerList()
   {
      allPlayers.Read();
   }

   // Displays all players in allPlayers.
   void printPlayerList() const
   {
      allPlayers.Print();
   }

   // Processes all transactions until the end of file.
   void processAllTransactions()
   {
      while (cin >> command)
      {
         processOneCommand(command);

      }
   }
};   // Tournament

int main()
{
   Tournament tournament;

   tournament.readRatingAdjustmentLevelChart();

   tournament.readPlayerList();

   cout << "\nThe following is an echo of the original "
      << "players' list.\n" << endl;
   tournament.printPlayerList();

   cout << endl << "Processing transactions..." << endl;
   tournament.processAllTransactions();

   cout << "\nThe following is the final players' list." << endl;
   tournament.printPlayerList();

   return 0;
}

Thank you guys for your help so far!

Upvotes: 0

Views: 119

Answers (1)

coder hacker
coder hacker

Reputation: 4868

Why not replace the loop in which you are reading by

  while (cin>>command)
  {
     processOneCommand(command);

  }

If the input lines are as suggested than better way to process data is

  while(cin>>command){
      if(command=='A'){
            cin>>location>>number>>name1>>name2;
      }
      else{
            cin>>read whatever for D
      }

      processOneCommand(all read values);

   }

Upvotes: 1

Related Questions