Write a c++ program that prompts user for file name and displays the number of w
ID: 3788191 • Letter: W
Question
Write a c++ program that prompts user for file name and displays the number of words in the file. Then prompts user to enter a number and then displayed that specified word.( Ex: user enter 4 program display the fourth word in the file) **** DO NOT USE AN ARRAY**** it's supposed to be a simple program but I have no idea how to do this since I can't use an array Write a c++ program that prompts user for file name and displays the number of words in the file. Then prompts user to enter a number and then displayed that specified word.( Ex: user enter 4 program display the fourth word in the file) **** DO NOT USE AN ARRAY**** it's supposed to be a simple program but I have no idea how to do this since I can't use an array Write a c++ program that prompts user for file name and displays the number of words in the file. Then prompts user to enter a number and then displayed that specified word.( Ex: user enter 4 program display the fourth word in the file) **** DO NOT USE AN ARRAY**** it's supposed to be a simple program but I have no idea how to do this since I can't use an arrayExplanation / Answer
//C++ program to find specfic number word and count number of words in file
#include<fstream>
#include<iostream>
#include<string>
using namespace std;
//main function begins
int main()
{
ifstream inFile,inFile2; //Declares a file stream object
string fileName;
string word;
int count = 0,num;
//prompt for file name with extension
cout << "Please enter the file name "<<endl;
getline(cin,fileName);
//open file which is given input
inFile.open(fileName.c_str());
//loop to count the number of words
while(!inFile.eof())
{
inFile >> word;
count++;
}
cout << "Number of words in file is " << count<<endl;
cout << "Enter number to dispaly respective word"<<endl;
cin >>num;
if(num>count)
{
cout<<"No word correspond to that number "<<endl;
}
else
{
//dipaly part for corrsponding word to given number
int count=0;
inFile2.open(fileName.c_str());
while(!inFile2.eof())
{
inFile2 >> word;
count++;
//of user number matches with number count of word
if(num==count)
{
cout <<"The word with number "<<num<<" is"<<word;
break;
}
}
}
inFile.close();
inFile2.close();
cin.get();
return 0;
}
given input file text in a.txt is :
A Ab
D 0
A 7
A 4
Output :
Please enter the file name
data1.txt
Number of words in file is 8
Enter number to dispaly respective word
2
The word with number 2 is Ab
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.