Question

In: Computer Science

Create a Visual Studio console project named exercise101. The main() function should prompt for the name...

Create a Visual Studio console project named exercise101. The main() function should prompt for the name of a text file located in the same directory as exercise101.cpp, and search for a word in the text file as follows:

Enter text file name:

Enter search word:

The program should print the number of occurrences of the word in the file:

occurrences of were found in If the file could not be opened then display:

File not found Use Stream file I/O as a guide to opening and reading a stream input file.

You should read the file word by word, where a word is a set of characters preceded and followed by white space including newlines.

Test you program by searching for the word "government" in the usdeclar.txt file available in Files / Source code / Exercise 10.1 Upload your exercise101.cpp source file to Canvas.

Solutions

Expert Solution

The Visual Studio C++ Program is given below. You are required to create a CPP project within your Visual Studio IDE and copy the below code and paste into the CPP source file of the project and save. Then put the text file (you want to be read by the program) in your projects folder where the cpp file exists within the Project directory. Then run the program and enter the name of the text file and word to be searched when prompted at the console.


#include "pch.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
        // declare the variables
        string filename; // variable to store the filename from user
        ifstream infile; // variable to store the file pointer
        string search_word; // variable to store the word to be searched
        string word; // variable to store the word read from file
        int occurences = 0; // to store the count of search word

        // inputting the input file name
        cout << "Enter text file name: ";
        getline(cin >> ws, filename);
        cout << "Enter search word: ";
        cin >> search_word;

        // opening the file
        infile.open(filename.c_str());
        // check if file was opened or not
        // if not then print error message and exit
        if (!infile.is_open())
        {
                cout << "File not found!" << endl;
                return -1;
        }

        // read each word from the file
        while (infile >> word)
        {
                // compare each word to search word ignoring case
                // if matches then increment the occurence value by 1
                if ( _strcmpi(word.c_str(), search_word.c_str()) == 0)
                        occurences++;
        }
        infile.close();

        // check if count is 0, if yes then print word not found
        if (occurences == 0)
                cout << "The word '" << search_word << "' was not found in the text file!" << endl;
        else
                // else print the frequency of the word found within the file
                cout << "The word '" << search_word << "' was found " << occurences << " time(s) within the text file" << endl;
}

Input File

Console Output

NOTE: If you encounter any issue then kindly comment within the comments section and kindly give a thumbs up if you liked the answer.


Related Solutions

Create a Visual Studio console project named exercise093. In the exercise093.cpp file first define a function...
Create a Visual Studio console project named exercise093. In the exercise093.cpp file first define a function void lowerToUpper(std::string & sentence) that iterates over all characters in the sentence argument. Any lowercase letter should be converted to uppercase. This can be done by including <cctype> and testing each character in sentence with the islower() function. If islower(sentence[i]) returns true then sentence[i] should be replaced with toupper(sentence[i]). The main() function should assign "Hello how are you doing?" to sentence, call lowerToUpper(sentence), and...
Create a new Visual Studio console project named assignment042, and translate the algorithm you developed in...
Create a new Visual Studio console project named assignment042, and translate the algorithm you developed in Assignment 04.1 to C++ code inside the main() function. Your program should prompt for a single 9-digit routing number without spaces between digits as follows: Enter a 9-digit routing number without any spaces: The program should output one of: Routing number is valid Routing number is invalid A C++ loop and integer array could be used to extract the routing number's 9 digits. However...
Create a Visual Studio console project (c++) containing a main() program that declares a const int...
Create a Visual Studio console project (c++) containing a main() program that declares a const int NUM_VALUES denoting the array size. Then declare an int array with NUM_VALUES entries. Using a for loop, prompt for the values that are stored in the array as follows: "Enter NUM_VALUES integers separated by blanks:" , where NUM_VALUES is replaced with the array size. Then use another for loop to print the array entries in reverse order separated by blanks on a single line...
ceate a Visual Studio console project named exercise093. In the exercise093.cpp file first define a function...
ceate a Visual Studio console project named exercise093. In the exercise093.cpp file first define a function void lowerToUpper(std::string & sentence) that iterates over all characters in the sentence argument. Any lowercase letter should be converted to uppercase. This can be done by including and testing each character in sentence with the islower() function. If islower(sentence[i]) returns true then sentence[i] should be replaced with toupper(sentence[i]). The main() function should assign "Hello how are you doing?" to sentence, call lowerToUpper(sentence), and use...
Create a C# .NET Core Console project in Visual Studio. (This is the same kind of...
Create a C# .NET Core Console project in Visual Studio. (This is the same kind of project we have been doing all semester.) Do all of the following in the Program class. You do not need to add any other classes to this project. 2. If it exists, remove the Console.WriteLine(“Hello World!”); line that Visual Studio created in the Program class. 3. At the very top of the Program.cs page you should see using System; On the empty line below...
ONLY USE VISUAL STUDIO (NO JAVA CODING) VISUAL STUDIO -> C# -> CONSOLE APPLICATION In this...
ONLY USE VISUAL STUDIO (NO JAVA CODING) VISUAL STUDIO -> C# -> CONSOLE APPLICATION In this part of the assignment, you are required to create a C# Console Application project. The project name should be A3<FirstName><LastName>P2. For example, a student with first name John and Last name Smith would name the project A1JohnSmithP2. Write a C# (console) program to calculate the number of shipping labels that can be printed on a sheet of paper. This program will use a menu...
1. Make a Console App (.NET Core) in Visual Studio and name it as A1YourFirstnameLastname. 2....
1. Make a Console App (.NET Core) in Visual Studio and name it as A1YourFirstnameLastname. 2. Implement a vehicle rental management system which is capable of doing the following: • View all, available and reserved vehicles. • Reserve vehicle or cancel a reservation. 3. The application must be menu-based app: 1 - View all vehicles 2 - View available vehicles 3 - View reserved vehicles 4 - Reserve a vehicle 5 - Cancel reservation 6 - Exit 4. NOTE: At...
Create a Visual Studio console project using c++ void lowerToUpper(std::string & sentence) that iterates over all...
Create a Visual Studio console project using c++ void lowerToUpper(std::string & sentence) that iterates over all characters in the sentence argument. Any lowercase letter should be converted to uppercase. This can be done by including <cctype> and testing each character in sentence with the islower() function. If islower(sentence[i]) returns true then sentence[i] should be replaced with toupper(sentence[i]). The main() function should assign "Hello how are you doing?" to sentence, call lowerToUpper(sentence), and use an if statement to check the new...
answer the following using C# Design and program a Visual Studio Console project in C# that...
answer the following using C# Design and program a Visual Studio Console project in C# that allows your user to enter a number. The program will examine the number to see if it is prime. If it is prime, it will print the next higher prime and the next lower primes to the console. If the number entered by the user is not prime, display a message to that effect. All code should be written by you. Do not copy/paste...
Create a new “Area” project. Create a new Visual Studio Project and call it “Area”. This...
Create a new “Area” project. Create a new Visual Studio Project and call it “Area”. This project will be used to calculate the area of certain figures, like circles, squares and rectangles. So add a title to the Form. The Form Title should say “Area”. Also add 3 labels, 3 Buttons, 3 Textboxes and 3 RadioButtons. The 3 Buttons should be near the bottom of the Form and say “Calc Area”, “Clear” and “Exit”. Make sure to give all your...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT