I'm making an app that reads through an input text file and shows the number of alphanumerical characters, punctuation marks, spaces, etc in the text. The problem is I can't figure out how to make it print out the contents of the file. It is only printing the amount of characters in the title of the text.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

I'm making an app that reads through an input text file and shows the number of alphanumerical characters, punctuation marks, spaces, etc in the text. The problem is I can't figure out how to make it print out the contents of the file. It is only printing the amount of characters in the title of the text.

 

```cpp
// Close input file
inFile.close();
cout << endl << lineCount << " line(s) read from file '"
     << INPUT_FILE_NAME << "'" << endl << endl;

// Count number of alphanumeric char
alnum = 0;
while (isalnum(INPUT_FILE_NAME[alnum])) alnum++;
printf("Alphanumeric chars: %d \n", alnum);

// Show application close
cout << "End of Text Turtles" << endl;
```

### Explanation

- **Close Input File:** This section of the code is responsible for closing the file that was opened for reading. It outputs to the console how many lines were read from the file specified by `INPUT_FILE_NAME`.

- **Count Alphanumeric Characters:** This part initializes a counter `alnum` to zero. It uses a `while` loop to iterate over the string `INPUT_FILE_NAME` to count the number of alphanumeric characters (letters and numbers) present. The count is displayed using `printf`.

- **Show Application Close:** At the end of the program, there’s a message printed to indicate the end of the operation, displaying "End of Text Turtles".
Transcribed Image Text:```cpp // Close input file inFile.close(); cout << endl << lineCount << " line(s) read from file '" << INPUT_FILE_NAME << "'" << endl << endl; // Count number of alphanumeric char alnum = 0; while (isalnum(INPUT_FILE_NAME[alnum])) alnum++; printf("Alphanumeric chars: %d \n", alnum); // Show application close cout << "End of Text Turtles" << endl; ``` ### Explanation - **Close Input File:** This section of the code is responsible for closing the file that was opened for reading. It outputs to the console how many lines were read from the file specified by `INPUT_FILE_NAME`. - **Count Alphanumeric Characters:** This part initializes a counter `alnum` to zero. It uses a `while` loop to iterate over the string `INPUT_FILE_NAME` to count the number of alphanumeric characters (letters and numbers) present. The count is displayed using `printf`. - **Show Application Close:** At the end of the program, there’s a message printed to indicate the end of the operation, displaying "End of Text Turtles".
## C++ Program to Read from a Text File

This C++ code demonstrates how to read lines from a text file and display a message in the console. Below is a detailed breakdown of the code:

### Code

```cpp
using namespace std; // So "std::cout" may be abbreviated to "cout"

int main()
{
    // Declare constants
    const string INPUT_FILE_NAME = ("SampleText1.txt");

    // Declare variables
    ifstream inFile;
    int lineCount;
    string line;
    int anum;

    // Show application header
    cout << "Welcome to Text Turtles" << endl;
    cout << "-----------------------" << endl << endl;

    // Attempt to open input file
    inFile.open(INPUT_FILE_NAME);
    if (!inFile.is_open())
        cout << "Error: unable to open file '" 
             << INPUT_FILE_NAME << "'." << endl << endl;
    else
    {
        // Loop to read from input file
        cout << "Reading lines from file '" << INPUT_FILE_NAME
             << "' ..." << endl << endl;
        lineCount = 0;
        while (inFile.good())
        {
            getline(inFile, line);

            lineCount = lineCount + 1;
        }

        // Close input file
        inFile.close();
        cout << endl << lineCount << " line(s) read from file '"
             << INPUT_FILE_NAME << "'." << endl << endl;
    }
}
```

### Explanation

- **Include Directive**: The `using namespace std;` directive allows you to use standard library objects like `cout` without prefixing them with `std::`.

- **Main Function**: The `main()` function is the starting point of the program, where execution begins.

- **Constants and Variables**: Inside the main function, the `INPUT_FILE_NAME` constant is declared, which stores the name of the text file to read. Other variables include `inFile` for file input, `lineCount` to count the lines, `line` for the current line read, and `anum` (though `anum` seems unused in the snippet).

- **Output Header**: The program outputs a welcome message, "Welcome to Text Turtles", and a separator line for clarity.

- **File Operations**: 
  - Attempts to open the file specified by `INPUT_FILE_NAME`.
  - If the file cannot
Transcribed Image Text:## C++ Program to Read from a Text File This C++ code demonstrates how to read lines from a text file and display a message in the console. Below is a detailed breakdown of the code: ### Code ```cpp using namespace std; // So "std::cout" may be abbreviated to "cout" int main() { // Declare constants const string INPUT_FILE_NAME = ("SampleText1.txt"); // Declare variables ifstream inFile; int lineCount; string line; int anum; // Show application header cout << "Welcome to Text Turtles" << endl; cout << "-----------------------" << endl << endl; // Attempt to open input file inFile.open(INPUT_FILE_NAME); if (!inFile.is_open()) cout << "Error: unable to open file '" << INPUT_FILE_NAME << "'." << endl << endl; else { // Loop to read from input file cout << "Reading lines from file '" << INPUT_FILE_NAME << "' ..." << endl << endl; lineCount = 0; while (inFile.good()) { getline(inFile, line); lineCount = lineCount + 1; } // Close input file inFile.close(); cout << endl << lineCount << " line(s) read from file '" << INPUT_FILE_NAME << "'." << endl << endl; } } ``` ### Explanation - **Include Directive**: The `using namespace std;` directive allows you to use standard library objects like `cout` without prefixing them with `std::`. - **Main Function**: The `main()` function is the starting point of the program, where execution begins. - **Constants and Variables**: Inside the main function, the `INPUT_FILE_NAME` constant is declared, which stores the name of the text file to read. Other variables include `inFile` for file input, `lineCount` to count the lines, `line` for the current line read, and `anum` (though `anum` seems unused in the snippet). - **Output Header**: The program outputs a welcome message, "Welcome to Text Turtles", and a separator line for clarity. - **File Operations**: - Attempts to open the file specified by `INPUT_FILE_NAME`. - If the file cannot
Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY