Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include <iostream> #include <fstream> using namespace std; int main() { char ch

ID: 3831751 • Letter: #

Question

#include <iostream>

#include <fstream>

using namespace std;

int main()

{

char ch;

fstream dataFile ("sentence.txt", ios::out);

cout << "Type a sentence and be sure to end it with a period. ";

cin.get(ch);

while(ch != '.')

{

dataFile.put(ch);

cin.get(ch);

}

dataFile.put(ch);

dataFile.close();

return 0;

}

In this code why is cin.get(ch) used? I know that the put member function writes a single character to the file and that the get member function reads a single character from the file. But why is cin.get(ch) used if the goal of the code is to enter a sentence and have that sentence written to the file?

Explanation / Answer

Let me explain you that clearly .

Your goal is to read the sentence from user and sentinal code is dot(.) to end the input

if we read the line from user and find the dot in it . Its going to take long process so while reading each character by chracter you can find the dot.

where in this condition

while(ch != '.')

It checks if ch is not equal to dot to terminate the loop