宣言が欠落していることは理解していますが、コードは正常にコンパイルされますが、出力は正しく出力されません...文字の代わりに¿が表示されます。問題は初期化関数にあると思いますが、それが何であるかを理解できないようです....
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