Question

This Question is from the book(Problem Solutions with C++ 10th Walter Savitch(ISBN-10 1292222824) Programming project 8 chapter 6.

m this book 8 This program numbers the lines found in a text file. Write a reads text from a file and outputs each line to th

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

#include<iostream>

#include<fstream>

#include<iomanip>

using namespace std;

int main(){

                //asking for input file name

                cout<<"Enter input file name: ";

                string infilename;

                //reading input file name

                getline(cin, infilename);

                //opening file in read mode

                ifstream inFile(infilename.c_str());

                //checking if file opened correctly

                if(!inFile){

                                //error, file not found / not opened

                                cout<<"Error! Input file not found"<<endl;

                                return 0; //exiting

                }

                //opening output.txt file

                ofstream outFile("output.txt");

                //starting line number from 1

                int lineNum=1;

                char c;

                bool beginning=true; //beginning of a line

                inFile>>noskipws; //allowing white spaces when reading

                //looping and reading each and every character from file

                while(inFile>>c){

                                //checking if this is the beginning

                                if(beginning){

                                               //printing line number to console and output file, with 3 spaces for line number

                                               //and right aligned

                                               cout<<setw(3)<<right<<lineNum<<": ";

                                               outFile<<setw(3)<<right<<lineNum<<": ";

                                               //checking if c is white space

                                               if(c==' ')

                                               //looping until a non white space character is found

                                               while(inFile.peek()==' '){

                                                               inFile>>c;

                                               }

                                               //setting beginning to false

                                               beginning=false;

                                               //printing c to output file and console if it is not blank space

                                               if(c!=' '){

                                                               cout<<c;

                                                               outFile<<c;

                                                               //incrementing line number and setting beginning to true if

                                                               //c is newline character

                                                               if(c=='\n'){

                                                                               lineNum++;

                                                                               beginning=true;

                                                               }

                                               }

                                }else{

                                               //in other cases, just printing the character to the console and file

                                               cout<<c;

                                               outFile<<c;

                                               //incrementing line number and setting beginning to true if

                                               //c is newline character

                                               if(c=='\n'){

                                                               lineNum++;

                                                               beginning=true;

                                               }

                                }

                               

                }

                //closing output file saving changes

                outFile.close();

                return 0;

}

/*OUTPUT*/

Enter input file name: input.txt 1: some text 2: in this file 3: some 4: more text 5: following 6: 7: some text 8: in this fi

/*input.txt (file used for running above program)*/

some text

in this file

some

more text

following

some text

   in this file

some

    more text

following

Add a comment
Answer #2

Thank you very much this is very helpful I am still lost with everything however Ill definitely get the hang of it by today. How can I find you to ask more questions?

source: Google
answered by: Mark
Add a comment
Know the answer?
Add Answer to:
This Question is from the book(Problem Solutions with C++ 10th Walter Savitch(ISBN-10 1292222824) Programming project 8...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Lab #10 C++ Write a C++ program that reads text from a file and encrypts the...

    Lab #10 C++ Write a C++ program that reads text from a file and encrypts the file by adding an encryption factor (EF) to the ASCII value of each character. The encryption factor is 1 for the first line and increases by 1 for each line up to 4 and then starts over at 1. So, for the 4 th line the EF is 4, for the 5th line it is 1, for the 10th line it is 2. In...

  • This is programming project 1 chapter 9(Book ISBN:1292222824 (1-292-22282-4) Walter Savitch Problem Solving with C++: Global...

    This is programming project 1 chapter 9(Book ISBN:1292222824 (1-292-22282-4) Walter Savitch Problem Solving with C++: Global Edition, 10/E) Question Do Programming Project 7 in Chapter 7 using a dynamic array. In this version of the problem, use dynamic arrays to store the ditits in each large integer. Allow an arbitrary number of digits instead of capping the number of digits at 20. TER 7/ Arrays time. For example digit at a the integer 1234 could be stored in the array...

  • Language: C Write an encoder and a decoder for a modified "book cipher." A book cipher...

    Language: C Write an encoder and a decoder for a modified "book cipher." A book cipher uses a document or book as the cipher key, and the cipher itself uses numbers that reference the words within the text. For example, one of the Beale ciphers used an edition of The Declaration of Independence as the cipher key. The cipher you will write will use a pair of numbers corresponding to each letter in the text. The first number denotes the...

  • Language: C Write an encoder and a decoder for a modified "book cipher." A book cipher uses a doc...

    Language: C Write an encoder and a decoder for a modified "book cipher." A book cipher uses a document or book as the cipher key, and the cipher itself uses numbers that reference the words within the text. For example, one of the Beale ciphers used an edition of The Declaration of Independence as the cipher key. The cipher you will write will use a pair of numbers corresponding to each letter in the text. The first number denotes the...

  • C++ please Write a program that reads the following sentences from a file: I am Sam...

    C++ please Write a program that reads the following sentences from a file: I am Sam Sam I am That Sam I am That Sam I am I do not like that Sam I am Do you like green eggs and ham I do not like them But I do like spam! You will first have to create the text file containing the input, separate from your program. Your program should produce two output files: (i) (ii) one with the...

  • Write a Java program called Flying.java that, firstly, prompts (asks) the user to enter an input...

    Write a Java program called Flying.java that, firstly, prompts (asks) the user to enter an input file name. This is the name of a text file that can contain any number of records. A record in this file is a single line of text in the following format: Num of passengers^range^name^manufacturer^model where: Num of passengers is the total number of people in the plane. This represents an integer number, but remember that it is still part of the String so...

  • c++ question, i put the txt attachment picture for this question... please include comments on calculations...

    c++ question, i put the txt attachment picture for this question... please include comments on calculations and varibles Description In this program, you will write a program to emulate a vending machine (somewhat). In this version of the program, you will use a struct type defined below struct snackType string name; string code; double price; int remaining; Where name contains the snack's name, code contains the 2 digit code for the item, price holds the item's price, and remaining holds...

  • Kindly follow the instructions provided carefully. C programming   Project 6, Program Design One way to encrypt...

    Kindly follow the instructions provided carefully. C programming   Project 6, Program Design One way to encrypt a message is to use a date’s 6 digits to shift the letters. For example, if a date is picked as December 18, 1946, then the 6 digits are 121846. Assume the dates are in the 20th century. To encrypt a message, you will shift each letter of the message by the number of spaces indicated by the corresponding digit. For example, to encrypt...

  • A. File I/O using C library functions File I/O in C is achieved using a file...

    A. File I/O using C library functions File I/O in C is achieved using a file pointer to access or modify files. Processing files in C is a four-step process: o Declare a file pointer. o Open the desired file using the pointer. o Read from or write to the file and finally, o Close the file. FILE is a structure defined in <stdio.h>. Files can be opened using the fopen() function. This function takes two arguments, the filename and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT