我理解声明丢失了,代码编译得很好但是输出没有正确输出...而不是字母,我得到了¿我相信问题出在初始化函数中,我似乎无法弄清楚它是什么......
void printResult(ofstream& outFile, letterType letterList[], int listSize)
{
int i;
int sum = 0;
double Percentage = 0;
cout << "PRINT" << endl;
for (i = 0; i < 52; i++)
sum += letterList[i].count;
outFile << fixed << showpoint << setprecision(2) << endl;
outFile << "Letter Count Percentage of Occurrence" << endl;
for (i = 0; i < 52; i++)
{
outFile << " " << letterList[i].letter << " "
<< setw(5) << letterList[i].count;
if (sum > 0)
Percentage = static_cast<double>(letterList[i].count) /
static_cast<double>(sum) * 100;
/*
Calculates the number of Occurrence by dividing by the total number of
Letters in the document.
*/
outFile << setw(15) << Percentage << "%" << endl;
}
outFile << endl;
}
void openFile(ifstream& inFile, ofstream& outFile)
{
string inFileName;
string outFileName;
cout << "Enter the path and name of the input file (with extension): ";
getline(cin, inFileName);
inFile.open(inFileName);
cout << endl;
cout << "Your input file is " << inFileName << endl;
cout << endl;
cout << "Enter the path and name of the output file (with extension): ";
getline(cin, outFileName);
outFile.open(outFileName);
cout << endl;
cout << "The name of your output file is " << outFileName << endl;
cout << endl;
}
void initialize(letterType letterList[])
{
//Loop to initialize the array of structs; set count to zero
for(int i = 0; i < 26; i++)
{
//This segment sets the uppercase letters
letterList[i].letter = static_cast<char>('A' + i);
letterList[i].count = 0;
//This segment sets the lowercase letters
letterList[i + 26].letter = static_cast<char>('a' + i);
letterList[i + 26].count = 0;
}
}
void count(ifstream& inFile, letterType letterList[], int& totalBig, int& totalSmall)
{
cout << "COUNT WORKING" << endl;
char ch;
//read first character
inFile >> ch;
//Keep reading until end of file is reached
while( !inFile.eof() )
{
//If uppercase letter or lowercase letter is found, update data
if('A' <= ch && ch <= 'Z')
{
letterList[static_cast<int>(ch) - 65].count++;
}
else if('a' <= ch && ch <= 'z')
{
letterList[static_cast<int>(ch) - 97].count++;
}
//read the next character
inFile >> ch;
} //end while
} //end function
===============
int main()
{
struct letterType letterList[52]; //stores the 52 char we are going to track stats on
int totalBig = 0; //variable to store the total number of uppercase
int totalSmall = 0; //variable to store the total number of lowercase
ifstream inFile;
//defines the file pointer for the text document
ofstream outFile;
//the file pointer for the output file
cout << "MAIN WORKING" << endl;
openFile(inFile, outFile);
//allow the user to specify a file for reading and outputting the stats
/*if (!inFile || !outFile)
{
cout << "***ERROR*** /n No such file found" << endl;
return 1;
}
else
return 1;
*///Check if the files are valid
initialize(&letterList[52]);
//initalizes the letter A-Z, and a-z */
count(inFile, &letterList[52], totalBig, totalSmall);
// counts the letters
printResult(outFile, letterList, 52);
//writes out the stats
//Close files
inFile.close();
outFile.close();
return 0;
}
=====================
void count(ifstream& inFile, letterType letterList[], int& totalBig, int& totalSmall)
{
cout << "COUNT WORKING" << endl;
char ch;
//read first character
inFile >> ch;
//Keep reading until end of file is reached
while( !inFile.eof() )
{
//If uppercase letter or lowercase letter is found, update data
if('A' >= ch && ch <= 'Z')
{
letterList[ch - 'A'].count++;
}
else if('a' >= ch && ch <= 'z')
{
letterList[(ch - 'a') + 26].count++;
}
//read the next character
inFile >> ch;
} //end while
} //end function
答案 0 :(得分:1)
计数逻辑容易混淆,并且掩盖了一个错误(它折叠了案例):
if('A' <= ch && ch <= 'Z')
{
letterList[static_cast<int>(ch) - 65].count++;
}
else if('a' <= ch && ch <= 'z')
{
letterList[static_cast<int>(ch) - 97].count++; // <--- a bug here
}
这会通过递增第一个元素的计数来对'a'
作出反应,这看起来像是'A'
的计数。这可以通过偏移小写来轻松修复,也可以重写它以便更清楚地完成所做的事情:
if ('A' <= ch && ch <= 'Z')
{
letterList[static_cast<int>(ch - 'A')].count++; // count uppercase
}
else if ('a' <= ch && ch <= 'z')
{
letterList[static_cast<int>(ch - 'a') + 26].count++; // count lowercase
}
至于主要错误, initialize()
不会在任何地方被调用。
初始化被错误地调用为initialize(&letterList[52]);
这会尝试初始化条目52,53,... 103.我很惊讶它不会出现段错误。
应该称为
initialize(letterList);
答案 1 :(得分:0)
你的功能正常。
在您的驱动程序代码中,您使用错误的argumnets
调用这些函数检查这些行,它们应该是这样的
initialize(&letterList[0]);
//initalizes the letter A-Z, and a-z */
count(inFile, &letterList[0], totalBig, totalSmall);
将letterList
数组传递给函数时,应将指针传递给数组中的第一个元素。您应该通过&letterList[0]
或仅letterList