简体   繁体   中英

C++ code compiles, but gives wrong output…initialize function is wrong?

I understand that declarations are missing, the code compiles fine however, the output does not output correctly... instead of a letter, I am getting ¿ instead. I believe the problem is in the initialize function, I just cannot seem to figure out what it is....

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

===============

driver code

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;
}

=====================

Entire Count function

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

The count logic is confusingly written and that is masking one bug (where it folds the case):

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
}

This reacts to 'a' by incrementing the count for the first element, which looks like it is intended to be the count for 'A' . This is easily fixed by offsetting lowercase, also rewriting it so it is clearer what is being done:

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
}

As for the main bug, initialize() is not called anywhere.

Initialize is being called incorrectly as initialize(&letterList[52]); This attempts to initialize entries 52, 53, ... 103. I am surprised it doesn't segfault.

It should be called as

initialize(letterList);

Your functions are doing just right.

In your driver code you are calling these functions with wrong argumnets

Check these lines they should be like this

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


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

While passing letterList array to the functions you should pass the pointer to first element in the array. you should pass &letterList[0] or simply letterList

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM