Question

In: Computer Science

Query the user for the name of a file. Open the file, read it, and count...

Query the user for the name of a file. Open the file, read it, and count and report the number of vowels found in the file. Using C++.

Solutions

Expert Solution

C++ Program:

/* C++ Program that counts number of vowels in the file */

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

//Main function
int main()
{
    char ch;
    string fileName;
    int vowelCount=0, aCnt=0, eCnt=0, iCnt=0, oCnt=0, uCnt=0;

    //Reading file name
    cout << "\n Input file name: ";
    cin >> fileName;

    //Opening file in read mode
    fstream fin(fileName, ios::in);

    //Checking for file existence
    if(fin.fail())
    {
        //Printing error message
        cout << "\n Error!!! Cannot read input file... \n";
        return -1;
    }

    //Fetching data from file
    while(fin.good())
    {
        //Reading character from file
        fin >> ch;

        //Checking for character
        switch(ch)
        {
            //Vowel 'a'
            case 'a':
            case 'A':   aCnt++; vowelCount++; break;

            //Vowel 'e'
            case 'e':
            case 'E':   eCnt++; vowelCount++; break;

            //Vowel 'i'
            case 'i':
            case 'I':   iCnt++; vowelCount++; break;

            //Vowel 'o'
            case 'o':
            case 'O':   oCnt++; vowelCount++; break;

            //Vowel 'u'
            case 'u':
            case 'U':   uCnt++; vowelCount++; break;

            default:    break;
        }
    }

    //Printing final report
    cout << "\n\n Total number of Vowels: " << vowelCount;
    cout << "\n\n Vowel 'A': " << aCnt;
    cout << "\n\n Vowel 'E': " << eCnt;
    cout << "\n\n Vowel 'I': " << iCnt;
    cout << "\n\n Vowel 'O': " << oCnt;
    cout << "\n\n Vowel 'U': " << uCnt;

    cout << "\n\n";
    return 0;
}

----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Sample Output:


Related Solutions

File IO Java question • Ask the user to specify the file name to open for...
File IO Java question • Ask the user to specify the file name to open for reading • Get the number of data M (M<= N) he wants to read from file • Read M numbers from the file and store them in an array • Compute the average and display the numbers and average.
Write a C++ program to open and read a text file and count each unique token...
Write a C++ program to open and read a text file and count each unique token (word) by creating a new data type, struct, and by managing a vector of struct objects, passing the vector into and out of a function. Declare a struct TokenFreq that consists of two data members: (1) string value; and (2) int freq; Obviously, an object of this struct will be used to store a specific token and its frequency. For example, the following object...
Please Use Python 3.The Problem: Read the name of a file and then open it for...
Please Use Python 3.The Problem: Read the name of a file and then open it for reading. Read the name of another file and then open it for output. When the program completes, the output file must contain the lines in the input file, but in the reverse order. • Write the input, output, and the relationship between the input and output. • Develop the test data (A single test will be alright). Make the input file at least 3...
Write a MIPS Assembly language program to request a file name from the user, open the...
Write a MIPS Assembly language program to request a file name from the user, open the file, read the contents, and write out the contents to the console. This is what I have so far: .data    fileName:   .space 100    prompt1:   .asciiz "Enter the file name: "    prompt2:    .asciiz ""    prompt3:   .asciiz "\n"    buffer:    .space 4096 .text    main:        #        li $v0, 4        la $a0, prompt1       ...
In this lab, you open a file and read input from that file in a prewritten...
In this lab, you open a file and read input from that file in a prewritten C++ program. The program should read and print the names of flowers and whether they are grown in shade or sun. The data is stored in the input file named flowers.dat. Instructions Ensure the source code file named Flowers.cpp is open in the code editor. Declare the variables you will need. Write the C++ statements that will open the input file flowers.dat for reading....
Write a program that asks the user for a file name. The file contains a series...
Write a program that asks the user for a file name. The file contains a series of scores(integers), each written on a separate line. The program should read the contents of the file into an array and then display the following content: 1) The scores in rows of 10 scores and in sorted in descending order. 2) The lowest score in the array 3) The highest score in the array 4) The total number of scores in the array 5)...
Write a program that prompts the user to enter a file name, then opens the file...
Write a program that prompts the user to enter a file name, then opens the file in text mode and reads names. The file contains one name on each line. The program then compares each name with the name that is at the end of the file in a symmetrical position. For example if the file contains 10 names, the name #1 is compared with name #10, name #2 is compared with name #9, and so on. If you find...
Write a program that asks the user to enter the name of a file, and then...
Write a program that asks the user to enter the name of a file, and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the file. Use Notepad or another text editor to create a sample file that can be used to test the program. Sample Run java FileLetterCounter Enter file name: wc4↵ Enter character to count: 0↵ The character '0' appears in the file...
In python, read the file credit_cards.txt into a dictionary with the count of how many cards...
In python, read the file credit_cards.txt into a dictionary with the count of how many cards of each type of card are in the file. credit_cards.txt contains the following data: John Smith, Discover Helen Jones, Visa Jerry Jones, Master Card Julio Jones, Diners Club Fred Jones, Diners Club Anthony Rendon, Platinum Visa Juan Soto, Platinum Visa George Jones, American Express Brandon Allen, Visa Henry Beureguard, Visa Allen Jackson, Master Card Faith Hill, Platinum Visa David Smith, Master Card Samual Jackson,...
Write a program that prompts the user for a file name, make sure the file exists...
Write a program that prompts the user for a file name, make sure the file exists and if it does reads through the file, count the number of times each word appears and then output the word count in a sorted order from high to low. The program should: Display a message stating its goal Prompt the user to enter a file name Check that the file can be opened and if not ask the user to try again (hint:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT