我已经开始研究一个程序,在我的一生中,我找不到一个不断搜索 eof 并且没有结束的错误。这肯定是我的 fileread 和 widthcount 函数的问题,而且它们很可能有相同的错误。有解决办法吗?它只是不断循环。
文件输入是这个
12.43 62.38 Los Angeles
21 59 Columbus, Ohio
0 15.58 Green Bay, Wisconsin
这将持续另外 10 行。
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;
void fileread(ifstream &ifFile, char newline, double &temp1, double &temp2, string city);
void widthcount(ifstream &ifFile, int &maxCount, char newline);
int main() {
double temp1, temp2, minTemp, maxTemp;
int maxCount;
char newline=' ';
string FileRead, city;
ifstream ifFile;
cout << "Please enter the location of the input file: ";
cin >> FileRead;
ifFile.open(FileRead.c_str());
if (ifFile.fail()) {
perror(FileRead.c_str());
exit(1);
}
widthcount(ifFile, maxCount, newline);
while(!ifFile.eof()) {
widthcount(ifFile, maxCount, newline);
}
ifFile.close();
ifFile.open(FileRead.c_str());
fileread(ifFile, newline, temp1, temp2, city);
while(!ifFile.eof()) {
fileread(ifFile, newline, temp1, temp2, city);
cout << left << setw(20) << city
<< right << setw(6) << fixed << setprecision(2) << temp1
<< right << setw(6) << fixed << setprecision(2) << temp2 << endl;
}
}
void fileread(ifstream &ifFile, char newline, double &temp1, double &temp2, string city) {
int charcount = 0;
ifFile >> temp1 >> temp2;
while (newline == ' ')
ifFile.get(newline);
while (newline != '\n' || !ifFile.eof()) {
charcount++;
ifFile.get(newline);
}
}
void widthcount (ifstream &ifFile, int &maxCount, char newline) {
double temp1, temp2;
int charcount = 0;
ifFile >> temp1 >> temp2;
ifFile.get(newline);
while (newline != '\n' && !ifFile.eof()) {
charcount++;
ifFile.get(newline);
cout << newline;
}
if (charcount > maxCount)
maxCount = charcount;
}