C++ C++;代码编译,但输出错误…初始化函数错误?
我知道声明丢失,代码编译很好,但是输出不正确。。。我收到的不是一封信,而是。我相信问题出在初始化函数中,我似乎无法理解它是什么C++ C++;代码编译,但输出错误…初始化函数错误?,c++,C++,我知道声明丢失,代码编译很好,但是输出不正确。。。我收到的不是一封信,而是。我相信问题出在初始化函数中,我似乎无法理解它是什么 void printResult(ofstream& outFile, letterType letterList[], int listSize) { int i; int sum = 0; double Percentage = 0; cout << "PRINT" << endl; for
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
void打印结果(流文件和输出文件,字体-字母列表[],int-listSize)
{
int i;
整数和=0;
双倍百分比=0;
cout计数逻辑写得很混乱,这掩盖了一个bug(在折叠情况下):
对于主bug,initialize()
不会在任何地方调用
初始化被错误地调用为Initialize(&letterList[52]);
这试图初始化条目52、53、…103。我很惊讶它没有出现故障
它应该被称为
initialize(letterList);
计数逻辑写得很混乱,这掩盖了一个bug(在这里它会折叠大小写):
对于主bug,initialize()
不会在任何地方调用
初始化被错误地调用为Initialize(&letterList[52]);
这试图初始化条目52、53、…103。我很惊讶它没有出现故障
它应该被称为
initialize(letterList);
你的功能做得很好
在驱动程序代码中,您使用错误的参数调用这些函数
检查这些线,它们应该是这样的
initialize(&letterList[0]);
//initalizes the letter A-Z, and a-z */
count(inFile, &letterList[0], totalBig, totalSmall);
将字母列表
数组传递给函数时,您应该将指针传递给数组中的第一个元素。您应该传递字母列表[0]
,或者只传递字母列表
,您的函数做得正好
在驱动程序代码中,您使用错误的参数调用这些函数
检查这些线,它们应该是这样的
initialize(&letterList[0]);
//initalizes the letter A-Z, and a-z */
count(inFile, &letterList[0], totalBig, totalSmall);
向函数传递字母列表时,应将指针传递给数组中的第一个元素。应传递字母列表[0]
或者干脆字母列表
你有很多这是printResults函数,我的输出文件,而不是打印这样的东西:字母出现率百分比A 4 8.33%B 1 2.08%,依此类推,我得到:字母出现率百分比0所有52行上的0.00%这正是我需要我的程序做的事情,但是我无法更改我的打印结果编码..使用infle.good()检查输入流的状态
打开后,或使用is_open
方法确保文件被有效打开。@didierc文件很好您有很多这是printResults函数,我的输出文件,而不是打印这样的内容:字母发生率百分比A 4 8.33%B 1 2.08%等等,我得到:字母出现率百分比?52行0.00%这正是我需要我的程序做的事情,但是我不能更改我的打印结果编码..用infle.good()检查输入流的状态
打开后,或使用is_open
方法确保文件被有效打开。@didierc文件良好更改此操作不会更改我的任何输出,并且我有一个driver.cpp文件,其中包含我的所有调用,程序会执行,但是输出不会出来correctly@SebastianABaker:Dosdriver.cpp
调用初始化函数?成功!!非常感谢您所有更改这并没有更改我的任何输出,我有一个driver.cpp文件,其中包含我所有的调用,程序执行,但是输出没有出来correctly@SebastianABaker:driver.cpp是否调用初始化函数?成功!!非常感谢大家
initialize(letterList);
initialize(&letterList[0]);
//initalizes the letter A-Z, and a-z */
count(inFile, &letterList[0], totalBig, totalSmall);