2013-04-15 20 views
1

据我所知,声明丢失,代码编译罚款,但输出不正确输出...而不是一个字母,我越来越¿而是。我认为这个问题是在初始化函数,我似乎无法弄清楚它是什么....C++代码编译,但给出错误的输出....初始化函数是错误的?

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

你的代码中有很多'<<' - 哪一个是给出问题的? – user93353

+0

它是printResults功能,我的输出文件,而不是打印是这样的: 信OCCURENCES百分比 A 4 8.33% 乙1 2.08% 等我得到: 信OCCURENCES百分比 ¿0 0.00 % 所有52行 –

+0

这是我需要我的程序做的确切的事情,但我无法更改我的printResults编码.. http://www.cplusplus.com/forum/beginner/53106/ –

回答

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); 
+0

改变这并没有改变我的任何输出,并且我有一个driver.cpp文件,其中包含我所有的调用,程序执行,但是输出没有正确输出 –

+0

@SebastianABaker:'driver.cpp'调用初始化函数? – wallyk

+0

是的,至少它似乎是 –

0

您的功能正在做的恰到好处。

在驱动程序代码,你的这些行调用这些函数与错argumnets

检查,他们应该是这样的

initialize(&letterList[0]); 
//initalizes the letter A-Z, and a-z */ 


count(inFile, &letterList[0], totalBig, totalSmall); 

虽然经过letterList阵列的功能,你应该把指针传递给第一个元素在数组中。你应该通过&letterList[0]或者简单地letterList