Question
C++ program that counts number of occurrences of a word in a text file & Identifies the unique words in the text file as well.
C++ program that counts number of occurrences of a word in a text file & Identifies the unique words in the text file as well. I have the text file and the code to read in the text file below. My teacher said the Unique Identefiers are words that only occur once in the text file.
Text File:
7 \\ Number of transactions
1, 3, gum,bread,fish \\ Transactions ID, #of Items, Item 1,Item 2,Item 3
2, 2, fish,bread
3, 1, juice
4, 3, bread, mangos fish
5, 2, bread,mangos
6, 3, Icecream,bread,fish
7, 1, fish
Code:
#include
#include
using namespace std;
int main()
{
string line; // variable used to read a line from file
int lines =0; // variable conatining the number of transactions
ifstream myfile("/Users/TheKid/Library/Mobile Documents/com~apple~TextEdit/Documents/en.lproj/marketdata.txt"); //opening market.data file in read mode using ifstream and providing the path to the file. Either keep the file in the same path where the cpp file is else specify the full path of market.data
if (myfile.is_open()) // checking if the file was opened
{
while(getline(myfile,line)) //read the file line by line until end of file is reached
{
lines ++; // calculating the transactions
}
cout< myfile.close(); //closing the file ifstream myfile("/Users/TheKid/Library/Mobile Documents/com~apple~TextEdit/Documents/en.lproj/marketdata.txt"); // re-opening the file while(getline(myfile,line)) //read the file line by line until end of file is reached { cout< } myfile.close(); // closing the file }else cout<<"Unable to open file"; return 0; } // end of main
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