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

C++ input stream classes haave two member functions,unget() and putback(), thatc

ID: 3714183 • Letter: C

Question

C++ input stream classes haave two member functions,unget() and putback(), thatcan be used to"undo" an operation performed by the get () function. Research these functions son the internet, and then use one of them to rewrite program 13-9without using the peek () function.

I am using viusal studios 2017

please help, I have tried everything! Thanks in advace

PROGRAM 13.9:

//This program demonstrates the peek member function.

#include <iostream>

#include <string>

#include <fstream>

using namespace std;

int main()

{

//Variables needed to read characters and numbers

char ch;

int number;

//Variables for file handling

string fileName;

fstream inFile,outFile;

//Open the file to be modified

cout << "Enter a file name:";

cin >> fileName;

inFile.open(fileName.c_str(), ios : : in);

if (!inFile)

{

cout << "Cannot open file" << fileName;

return 1;

}

//Open thefile to receive themodifed copy

outFile.open(modified.txt",ios::out);

if(!outFile)

{

cout<<"Cannot open the output file.";

return 2;

}

//Copy the input file one character at a time

//except numbers in the input file must have 1

//added to them

//Peek at the first character

ch = inFile.peek();

while (ch != EOF)

{

//Examine current character

if (isdigit(ch))

{

//Numbers should be read with >>

inFile >> number;

outFile << number+1;

}

else

{

//Just a simple character, read it and copy it

ch = inFile.get();

outFile << ch;

}

//Peek at the next character from input file

ch = inFile.peek();

}

//Close the files

inFile.close();

outfile.close();

return 0;

}

Explanation / Answer

Hello, here is the modified code which uses unget() method instead of using peek() method. Comments are included wherever code has been modified. Thanks.

//code.cpp

#include <iostream>

#include <string>

#include <fstream>

using namespace std;

int main()

{

//Variables needed to read characters and numbers

char ch;

int number;

//Variables for file handling

string fileName;

fstream inFile,outFile;

//Open the file to be modified

cout << "Enter a file name:";

cin >> fileName;

inFile.open(fileName.c_str(), ios :: in);

if (!inFile)

{

cout << "Cannot open file" << fileName;

return 1;

}

//Open thefile to receive themodifed copy

outFile.open("modified.txt",ios::out);

if(!outFile)

{

cout<<"Cannot open the output file.";

return 2;

}

//Copy the input file one character at a time

//except numbers in the input file must have 1

//added to them

//get the first character

ch = inFile.get();

//loop until EOF character is fetched

while (ch != EOF)

{

//Examine current character

if (isdigit(ch))

{

//Numbers should be read with >>, so we are ungetting the character,

// and reading with >>

inFile.unget();

inFile >> number;

outFile << number+1;

}

else

{

//Just a simple character, since we already read it, appending

//to the output file

outFile << ch;

}

//getting the next character from input file

ch = inFile.get();

}

//Close the files

inFile.close();

outFile.close();

return 0;

}

//output

Enter a file name:numbers.txt

--------------------------------

Process exited after 4.66 seconds with return value 0

Press any key to continue . . .

//numbers.txt (input file used)

1x 2y 8z 1a 2b 7c 1d 4e 3f

//modified.txt (after running program)

2x 3y 9z 2a 3b 8c 2d 5e 4f

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote