Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 代码无限循环,我可以';我不明白为什么是C++;_C++_Loops_Infinite - Fatal编程技术网

C++ 代码无限循环,我可以';我不明白为什么是C++;

C++ 代码无限循环,我可以';我不明白为什么是C++;,c++,loops,infinite,C++,Loops,Infinite,在我的程序中有一段代码是无限循环的,我一辈子都不知道为什么。我是一个非常新手的程序员,任何洞察都将非常感激。 我关注的输入如下:(它不是所有的输入,但足以摆脱我的程序) A意味着它正在将球员加入名单,确实如此,但它不会超过沙齐·费尔斯坦先生 罪犯代码如下: class Tournament { private: RatingAdjustmentLevel chart[MAX_LEVELS]; PlayerList allPlayers; char comm

在我的程序中有一段代码是无限循环的,我一辈子都不知道为什么。我是一个非常新手的程序员,任何洞察都将非常感激。 我关注的输入如下:(它不是所有的输入,但足以摆脱我的程序)

A意味着它正在将球员加入名单,确实如此,但它不会超过沙齐·费尔斯坦先生

罪犯代码如下:

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;
}
  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;
}
#包括
#包括
#包括
使用名称空间std;
const int NOT_FOUND=-1;
const int MAX_PLAYERS=25;
const int MAX_LEVELS=11;
const int LIST_FULL=0;
列表中的常量int=1;
常量int ADDED=2;
分级调整级别
{
私人:
积分差;
int ADJAMTNOTPURSE;
int-adjAmtUpset;
公众:
无效读取()
{
cin>>分级差异>>调整未翻转>>调整设置;
}
int GetDiff()常量
{
回报率差异;
}
int getAdjamtNotUpper()常量
{
返回ADJAMTNOTPRESE;
}
int GetAdjAmtUpset()常量
{
返回adjAmtUpset;
}
};
职业选手
{
私人:
字符串状态;
国际评级;
最后一串;
先串;
公众:
无效读取()
{
cin>>状态>>评级>>最后>>第一;
}
void ReadName()
{
最后,第一;
}
字符串GetFirst()常量
{
先返回;
}
字符串GetLast()常量
{
最后返回;
}
int GetRating()常量
{
回报率;
}
void Print()常量
{

cout为什么不将您正在阅读的循环替换为

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

   }

while(!cin.eof())
更改为
while(cin)
。除了
eof
,还有其他可能的错误。可能是
Player::Read()
,或者您没有显示的另一个输入函数之一,是在
cin
上设置错误标志,例如,如果它试图提取一个数字但失败。顺便说一下,我发现设置
cin.exceptions(std::ifstream::failbit | std::ifstream::badbit)非常有用
@user3458030:这表示流的读取失败,很可能是由于试图解析和
int
而没有或类似的内容。您需要解决该问题。更改循环条件会将该问题转化为无限循环。问题可能在processOneCommand中。您正在使用许多此处未提供其定义的函数,例如entry.Read()。可能在entry.Read()中有错误你能提供它的代码吗?如果我这样做,循环只执行一次。这肯定是processOneRead的问题。如果processOneRead被保证工作,循环肯定会工作。我的读取函数很短,我认为它没有问题。它只读取人的状态、号码和姓名。然后它将排序后的数据传递给他们各自的人请尝试我的第二个建议
  while(cin>>command){
      if(command=='A'){
            cin>>location>>number>>name1>>name2;
      }
      else{
            cin>>read whatever for D
      }

      processOneCommand(all read values);

   }