Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Basics Computer EngineeringInformation Warehouse Basics From Science

Authors: Odiljon Jakbarov ,Anvarkhan Majidov

1st Edition

620675183X, 978-6206751830

More Books

Students also viewed these Databases questions