Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am creating a sentence filter that changes a sentence written capitals to an o

ID: 3643046 • Letter: I

Question

I am creating a sentence filter that changes a sentence written capitals to an ordinary sentence. Ex. THIS IS A TEST. --> This is a test. . But I get an extra char at the end where am I making this extra char.


#include <iostream>

#include <fstream>

#include <cctype>


using namespace std;


int main()

{

const int LENGTH = 81;

char fileName[LENGTH], ch, ch2;

fstream sourceFile("Test.txt"); //Source File

fstream destFile("dest.txt"); //Destination File


while (!sourceFile.fail())

{ sourceFile.get(ch);

ch2 = toupper(ch);

destFile.put(ch2);

while(!sourceFile.fail())

{ sourceFile.get(ch);

ch2 = tolower(ch);

destFile.put(ch2);

if(ch2 == '.')

{ sourceFile.get(ch);

ch2 = toupper(ch);

destFile.put(ch2);

break;

}

}

}

//CLOSES FILES

sourceFile.close();

destFile.close();

cout << "File conversion done. ";

return 0;

}

Explanation / Answer

Here is the modification. It is highlighted in red.

#include <iostream>
#include <fstream>
#include <cctype>

using namespace std;

int main()
{
    const int LENGTH = 81;
    char fileName[LENGTH], ch, ch2;
    fstream sourceFile("Test.txt"); //Source File
    fstream destFile("dest.txt"); //Destination File

    while (!sourceFile.fail())
    {
        sourceFile.get(ch);
        ch2 = toupper(ch);
        //cout<<ch2;
        destFile.put(ch2);
        while(!sourceFile.fail())
        {
            sourceFile.get(ch);
            ch2 = tolower(ch);
            if(ch2 == '.')
            {
                sourceFile.get(ch);
                ch2 = toupper(ch);
                //cout<<ch2;
                destFile.put(ch2);
                continue;
            }
           
            //cout<<ch2;
            destFile.put(ch2);

        }
    }

    //CLOSES FILES

    sourceFile.close();
    destFile.close();
    cout << endl<<"File conversion done. ";
    return 0;

}