Question: PROGRAMMING C++ Hi, Please review the program below. Is there any other way I can write the following lines 27,29,45 and 49 ? More conserned

PROGRAMMING C++

Hi,

Please review the program below.

Is there any other way I can write the following lines 27,29,45 and 49 ?

More conserned about lines 27,29.

The reason I asked is because we didn't learn that commands, and we not suppose to use

something that we didn't learn in class.

The program should be written with functions, each function should have 5-8lines.

The book we use is Starting Out with C++, we on chapter 12 now.

line 27 - size_t found = line.find(word);

line 29- if (found!=string::npos)

line 45 - while( file >> candidate )

line 49 - if( word == candidate ) ++countwords ;

// /* Write a program that asks the user for a file name and a string to search for. The program should search the file for every occurrence of a specified string. When the string is found, the line that contains it should be displayed. After all the occurrences have been located, the program should report the number of times the string appeared in the file */

#include #include #include

using namespace std;

//this function will search word and print the whole line contains the word void searchWord(ifstream& file, string word){

//rewind the file pointer file.clear();

file.seekg(0);

string candidate,line;

while(getline(file, line)){ //row delimeter by space

size_t found = line.find(word);

if (found!=string::npos) cout << "Word "< "<

}

}

//this function will print number of time word came in file

void timesDispaly(ifstream& file,string word){

int countwords = 0 ;

string candidate ;

while( file >> candidate ) // for each candidate word read from the file

{

if( word == candidate ) ++countwords ;

}

cout << "The word '" << word << "' has been found " << countwords << " times. " ;

}

int main()

{

string path;

cout<< "Write the path of the file " ;

cin>> path ;

ifstream file(path);

if(file.is_open()){

string word;

cout << "File '" << path << "' opened. " ;

cout << "Write the word you're searching for " ;

cin >> word;

timesDispaly(file,word);

searchWord(file,word);

}else{

cout << "Error! File not found! " ;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!