代码无限循环,我无法弄清楚C ++的原因

时间:2014-04-25 02:11:40

标签: c++ loops infinite

我的程序中的一段代码无限循环,我无法解释为什么生活。我是一名非常新手的程序员,非常感谢任何见解。 我关注的输入如下:(它不是所有输入,但它足以甩开我的程序)

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

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

1 个答案:

答案 0 :(得分:1)

为什么不用

替换你正在阅读的循环
  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);

   }