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...
icon
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.
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
steps

Step by step

Solved in 5 steps with 1 images

Blurred answer
Knowledge Booster
Stack operations
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.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
Systems Architecture
Systems Architecture
Computer Science
ISBN:
9781305080195
Author:
Stephen D. Burd
Publisher:
Cengage Learning