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

I can find c++ code online to do this but I would like to understand how it work

ID: 3839681 • Letter: I

Question

I can find c++ code online to do this but I would like to understand how it works. Can you write a program and include comments explaining what each line of code is doing? It will help me understad how this works.

Write a C++ program that reads a speech file and counts the number of alphabetic characters, number of uppercase letters, number of punctuation characters, and the number of whitespace characters in the file. Utilize the get function to process the file one character at a time. The text of the speech is shown below. The program should produce a report containing the above information.

The speech:

Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.

Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this.

But, in a larger sense, we cannot dedicate -- we cannot consecrate -- we cannot hallow -- this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us -- that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion -- that we here highly resolve that these dead shall not have died in vain -- that this nation, under God, shall have a new birth of freedom -- and that government of the people, by the people, for the people, shall not perish from the earth.

Explanation / Answer

#include <iostream>

   #include <fstream>

   #include <string>

   #include <cstdio>

   #include <cstdlib>

   using namespace std;

   #ifdef _WIN32

        const char CLRSCR[] = {"cls"};

        const char PAUSE[] = {"pause"};

   #else

        const char CLRSCR[] = {"clear"};

        const char PAUSE[] = {"sleep(20)"};

   #endif

   int main(void)

   {
        ifstream fin;

        ofstream fout;

        string filename;
        cout << "Welcome " << endl;

        cout << "This program will analyze the file content &" << endl;

        cout << "compute the statistics of the file you input. " << endl;

        system(PAUSE);

        system(CLRSCR);
  
        do{

            cout << "Enter input data file name: ";

            cin >> filename;

            cout << " ";    // Get the file name.

            fin.open(filename.c_str()); // Convert to C-string and open it.

            if (!fin)

                 {          // Will fail if didn't exist.

                    cout << "Unable to open " << filename << endl;

                    cin.get();

                    system(CLRSCR);

                 }

        }while(!fin);

        char next;

        int characters = 0;

        int digits = 0;

        int upper = 0;

        int lower = 0;

        int space = 0;

        int eospm = 0;

        int others = 0;

        while((next = fin.get()) != EOF)

        {
              //calculate total numbers of characters including space

              //---> it doesnt count all the characters, what should i use to count every characters including space?

              //The function isalnum() returns non-zero if its argument

              //is a numeric digit or a letter of the alphabet. Otherwise, zero is returned

              if(isalnum(next))

                characters++;         

              //calculate total numbers of digits   ---> correct

              if(isdigit(next))

                ++digits;

              //calculate total numbers of uppercase   ---> correct

              if(isupper(next))

               ++upper;

              //calculate total numbers of lowercase   ---> correct

              if(islower(next))

               ++lower;

              //calculate total numbers of space    ---> this 1 doesnt count the correct whitespace either.

              if(isspace(next))

              ++space;

              //calculate total numbers of punctuation   ---> correct

              if(ispunct(next)){

                  ++others;

                  switch(next){

                      case '?':

                      case '.':

                      case '!':

                          ++eospm;

                          break;

                      default:

                          break;

                  }

              }         

        }

        // Print out what is counted.

        cout << "Total Number of Characters: " << space+digits+upper+lower+others << endl;

        cout << "" << endl;

        cout << "Total Number of Uppercase: " << upper << endl;

        cout << "Total Number of Lowercase: " << lower << endl;

        cout << "Total Number of Digits: " << digits << endl;

        cout << "Total Number of Space: " << space << endl;

        cout << "Total Number of End-of-Sentence Punctuation Marks: " << eospm << endl;

        cout << "Total Number of Other Characters: " << others << endl;   

        cout << "" << endl;

           system(PAUSE);

        return 0;

   }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote