[英]Code is looping infinitely and I can't figure out why 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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.