Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I need help writing a C++ code for counting words in a text file. The instruction as follows: Thank you! Count the number of occurrences
I need help writing a C++ code for counting words in a text file. The instruction as follows:
Thank you!
Count the number of occurrences of: if, else, while, case, for, && and // within a text file. Another option is to use your lab code. (You can conveniently copy and paste the name from the first line.) When we grade, we'll use some sample.cpp from the class folder. You can assume your keywords are lower case because that's the way they would be in a .cpp. Count the total number of words in the file. Be careful not to add an extra word at the end of file. I've provided two samples for you, test.txt (output below) and test2.txt. test2.txt will help you check that your program is properly handling a keyword at the end of a line. Use the string class for any text variables. Allow the user to enter the file name. If the open fails because they entered a bad name let them know and stop. (Please be kind to your end user.) Output your counts in a reasonably pretty way including telling the user the file name. Your main function can be no more than 15 lines long. Use functions instead. Don't cram to get to the 15 lines. Mine is 11 lines including the int main and the closing}. Don't wait to get started on this. You can do all the file 10 with what you learned in 116. Do please do this incrementally. In the program, I added comments with most of the steps that I did. I kept in mind that you wouldn't know about structures until the second week. My steps are only a suggestion. Just do a small bit and compile and test. Use a structure to store your data. A total count of words. A count for each of the keywords either as separate variables or as a single array of counts. The name of the file read, i.e. test.txt or test2.txt. Make the following functions that take your structure as a parameter. A print function that is call by value. A data initialization function that is call by reference. Output Here is an example of the output from my program. You don't have to copy it exactly. Just be self-documenting and not ugly. Please enter the input file name including extension : test.txt In test.txt there are: 2 for 2 while 2 if 1 else && 0 IL out of 100 words read. test2 - Notepad File Edit Format View Help if else while do for && || case else while do for && || case if while else do for && || case if
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started