REVIEW - Functions and Streams In this worksheet, you will practice writing functions involving problems that use streams (stringstreams, file streams) and String methods (find, substr) Exercise 1 Following is a code snippet for a program that reads several lines from a file¹. It prints words ignoring commas and whitespace in each line. Lastly, it prints the number of such words. //Extract each line while (getline(namelFS, line)) { } int count = 0; //From the line, extract words up to comma istringstream lineSS(line); string words; while (getline(lineSS, words, ',')) { //Extract each word from the words string word2; istringstream wordSS(words); while (wordSS >> word2) { } cout << word2 << " "; ++count; } cout << count << endl; Example file data.txt: why did the chicken cross the playground? Because, it wanted to go to the other slide! Ha, ha ha ha " Output file data_parse.txt: why did the chicken cross the playground? 7 Because it wanted to go to the other slide! 9 Ha ha ha ha 4 Let us divide the above code into two functions and rewrite it. Such a process is called refactoring (which you also did for the last problem in Lab 03). The two functions you need to write are in the following two pages.
REVIEW - Functions and Streams In this worksheet, you will practice writing functions involving problems that use streams (stringstreams, file streams) and String methods (find, substr) Exercise 1 Following is a code snippet for a program that reads several lines from a file¹. It prints words ignoring commas and whitespace in each line. Lastly, it prints the number of such words. //Extract each line while (getline(namelFS, line)) { } int count = 0; //From the line, extract words up to comma istringstream lineSS(line); string words; while (getline(lineSS, words, ',')) { //Extract each word from the words string word2; istringstream wordSS(words); while (wordSS >> word2) { } cout << word2 << " "; ++count; } cout << count << endl; Example file data.txt: why did the chicken cross the playground? Because, it wanted to go to the other slide! Ha, ha ha ha " Output file data_parse.txt: why did the chicken cross the playground? 7 Because it wanted to go to the other slide! 9 Ha ha ha ha 4 Let us divide the above code into two functions and rewrite it. Such a process is called refactoring (which you also did for the last problem in Lab 03). The two functions you need to write are in the following two pages.
C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter8: I/o Streams And Data Files
Section: Chapter Questions
Problem 8PP: (Data processing) A bank’s customer records are to be stored in a file and read into a set of arrays...
Related questions
Question
![REVIEW - Functions and Streams
In this worksheet, you will practice writing functions involving problems that use streams
(stringstreams, file streams) and String methods (find, substr)
Exercise 1
Following is a code snippet for a program that reads several lines from a file¹. It prints words
ignoring commas and whitespace in each line. Lastly, it prints the number of such words.
//Extract each line
while (getline(namelFS, line))
{
int count = 0;
//From the line, extract words up to comma
istringstream lineSS(line);
string words;
while (getline(lineSS, words, ','))
{
//Extract each word from the words
string word2;
istringstream wordSS(words);
while (wordSS >> word2)
{
}
cout << word2 << " ";
++count ;
}
cout << count << endl;
Example file data.txt:
why did the chicken cross the playground?
Because, it wanted to go to the other slide!
Ha, ha, ha ha
Output file data_parse.txt:
why did the chicken cross the playground? 7
Because it wanted to go to the other slide! 9
Ha ha ha ha 4
Let us divide the above code into two functions and rewrite it. Such a process is called
refactoring (which you also did for the last problem in Lab 03). The two functions you need to
write are in the following two pages.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F1bfda259-7429-4717-b69c-7067c249bba0%2Ffd6c4c1b-936a-4650-a105-91240cc3acf8%2Fld0fc7g_processed.png&w=3840&q=75)
Transcribed Image Text:REVIEW - Functions and Streams
In this worksheet, you will practice writing functions involving problems that use streams
(stringstreams, file streams) and String methods (find, substr)
Exercise 1
Following is a code snippet for a program that reads several lines from a file¹. It prints words
ignoring commas and whitespace in each line. Lastly, it prints the number of such words.
//Extract each line
while (getline(namelFS, line))
{
int count = 0;
//From the line, extract words up to comma
istringstream lineSS(line);
string words;
while (getline(lineSS, words, ','))
{
//Extract each word from the words
string word2;
istringstream wordSS(words);
while (wordSS >> word2)
{
}
cout << word2 << " ";
++count ;
}
cout << count << endl;
Example file data.txt:
why did the chicken cross the playground?
Because, it wanted to go to the other slide!
Ha, ha, ha ha
Output file data_parse.txt:
why did the chicken cross the playground? 7
Because it wanted to go to the other slide! 9
Ha ha ha ha 4
Let us divide the above code into two functions and rewrite it. Such a process is called
refactoring (which you also did for the last problem in Lab 03). The two functions you need to
write are in the following two pages.
Expert Solution
![](/static/compass_v2/shared-icons/check-mark.png)
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 5 steps with 1 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
![C++ for Engineers and Scientists](https://www.bartleby.com/isbn_cover_images/9781133187844/9781133187844_smallCoverImage.gif)
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
![Systems Architecture](https://www.bartleby.com/isbn_cover_images/9781305080195/9781305080195_smallCoverImage.gif)
Systems Architecture
Computer Science
ISBN:
9781305080195
Author:
Stephen D. Burd
Publisher:
Cengage Learning
![C++ for Engineers and Scientists](https://www.bartleby.com/isbn_cover_images/9781133187844/9781133187844_smallCoverImage.gif)
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
![Systems Architecture](https://www.bartleby.com/isbn_cover_images/9781305080195/9781305080195_smallCoverImage.gif)
Systems Architecture
Computer Science
ISBN:
9781305080195
Author:
Stephen D. Burd
Publisher:
Cengage Learning