代码无限循环,我不知道为什么C++



我的程序中有一段代码在无限循环,我一辈子都想不出原因。我是一个非常新手的程序员,任何见解都将不胜感激。我关注的输入如下:(这不是所有的输入,但足以让我的程序中断)

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

A意味着它正在将球员添加到名单中,事实确实如此,但它并没有超过Shazzi Felstein先生。

罪魁祸首代码如下:

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

问题在于processAllTransactions函数。while循环是无限的,它从来没有真正识别出文件的末尾。我的输出如下:

  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!

等等。它正确地将第一个人添加到我的人员列表中,并识别出他在其他所有时间都已经在那里了,它只是永远不会到达所需的文件结尾。请帮忙!我已经试了好几个小时了!

编辑:这是我的完整代码,这样所有的功能都可以查看:

#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;
}

感谢你们到目前为止的帮助!

为什么不用替换正在读取的循环

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

如果输入线路如建议的那样,那么处理数据的更好方法是

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

相关内容

最新更新