Problem Solving with C++ (10th Edition)
10th Edition
ISBN: 9780134448282
Author: Walter Savitch, Kenrick Mock
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Expert Solution & Answer
Chapter 6.1, Problem 2STE
Explanation of Solution
Explanation:
//Include the needed headers
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
//main() method
int main ()
{
//a handle for opening the input file
fin.open("stuff1.dat");
//check the condition
if (fin.fail( ))
{
//display the required data
cout << "Input file opening failed.\n";
//stop and exit
exit(1);
}
//a handle for opening the input file
fout...
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Modify your program from Learning Journal Unit 7 to read dictionary items from a file and write the inverted dictionary to a file. You will need to decide on the following:
How to format each dictionary item as a text string in the input file.
How to covert each input string into a dictionary item.
How to format each item of your inverted dictionary as a text string in the output file.
Create an input file with your original three-or-more items and add at least three new items, for a total of at least six items.
Include the following in your Learning Journal submission:
The input file for your original dictionary (with at least six items).
The Python program to read from a file, invert the dictionary, and write to a different file.
The output file for your inverted dictionary.
A description of how you chose to encode the original dictionary and the inverted dictionary in text files.
Show more...
Modify your program from Learning Journal Unit 7 to read dictionary items from a…
As in Self-Test Exercise 5, write some code that will create a stream namedoutStream that is a member of the class PrintWriter, and that connects thisstream to a text file named sam so that your program can send output to the file.This time, however, do it in such a way that, if the file sam already exists, the oldcontents of sam will not be lost, and the program output will be written after theold contents of the file.
First, take your program and add one more feature: at the end, just before you close the file, write the word “END” all by itself on a line. Run that program once to create a file of items with “END”. Instead of reading from cin and writing to a file, you read from your file and write to cout. The only tricky part is deciding when to stop reading. In your previous code, you stopped reading when the user said they didn’t want to continue. For the new code, keep reading until you read “END” for the name of the produce. You can use “==” on strings, so if (product name == ”END”) will tell you that you can stop reading.
My code:
#include<iostream>#include<string.h>#include<fstream>using namespace std;
int main(){ string filename; cout<<"Enter the name of the file to store the information"<<endl; getline(cin,filename); ofstream fileptr(filename.c_str()); if(!fileptr.is_open()) { cout<<"Couldn't open a file"<<endl; }…
Chapter 6 Solutions
Problem Solving with C++ (10th Edition)
Ch. 6.1 - Prob. 1STECh. 6.1 - Prob. 2STECh. 6.1 - Suppose that you are still writing the same...Ch. 6.1 - Prob. 4STECh. 6.1 - Prob. 5STECh. 6.1 - Prob. 6STECh. 6.1 - Suppose bla is an object, dobedo is a member...Ch. 6.1 - Prob. 8STECh. 6.1 - Prob. 9STECh. 6.1 - A program has read half of the lines in a file....
Ch. 6.1 - Prob. 11STECh. 6.2 - Prob. 12STECh. 6.2 - Prob. 13STECh. 6.2 - Prob. 14STECh. 6.2 - What output will be sent to the stuff.dat when the...Ch. 6.2 - Prob. 16STECh. 6.2 - In formatting output, the following flag constants...Ch. 6.2 - Here is a code segment that reads input from...Ch. 6.2 - Prob. 19STECh. 6.2 - Write the definition for a void function called...Ch. 6.2 - (This exercise is for those who have studied the...Ch. 6.3 - Suppose c is a variable of type char. What is the...Ch. 6.3 - Suppose c is a variable of type char. What is the...Ch. 6.3 - Prob. 24STECh. 6.3 - Consider the following code (and assume that it is...Ch. 6.3 - Consider the following code (and assume that it is...Ch. 6.3 - Suppose that the program described in Self-Test...Ch. 6.3 - Consider the following code (and assume that it is...Ch. 6.3 - Prob. 29STECh. 6.3 - Define a function called copyLine that takes one...Ch. 6.3 - Prob. 31STECh. 6.3 - (This exercise is for those who have studied the...Ch. 6.3 - (This exercise is for those who have studied the...Ch. 6.3 - Suppose ins is a file input stream that has been...Ch. 6.3 - Write the definition for a void function called...Ch. 6.3 - Consider the following code (and assume that it is...Ch. 6.3 - Write some C++ code that will read a line of text...Ch. 6 - Write a program that will search a file of numbers...Ch. 6 - Write a program that takes its input from a file...Ch. 6 - a. Compute the median of a data file. The median...Ch. 6 - Write a program that takes its input from a file...Ch. 6 - Write a program that gives and takes advice on...Ch. 6 - Write a program that reads text from one file and...Ch. 6 - Prob. 7PCh. 6 - Write a program to generate personalized junk...Ch. 6 - Write a program to compute numeric grades for a...Ch. 6 - Enhance the program you wrote for Programming...Ch. 6 - Prob. 4PPCh. 6 - Write a program that will correct a C++ program...Ch. 6 - Write a program that allows the user to type in...Ch. 6 - This project is the same as Programming Project 6,...Ch. 6 - This program numbers the lines found in a text...Ch. 6 - Write a program that computes all of the following...Ch. 6 - The text file babynames2012.txt, which is included...Ch. 6 - To complete this problem you must have a computer...Ch. 6 - Write a program that prompts the user to input the...Ch. 6 - The following is an old word puzzle: Name a common...
Knowledge Booster
Similar questions
- Write a program that prompts the user for an input file name, reads all words from the input file, and writes the words to the output file sentences.txt. Start a new line whenever a word ends in a period, question mark, or exclamation mark. C++arrow_forward(Practice) a. Write a set of two statements declaring the following objects as ifstream objects, and then opening them as text input files: inData.txt, prices.txt, coupons.dat, and exper.dat. b. Rewrite the two statements for Exercise 2a, using a single statement.arrow_forwardWrite a C# program that reads a text file and outputs the contains to the screen. The text file should be created outside of your code. Your program should do the following: Create a StreamReader that will read your text file for example // Open the input file for reading using (StreamReader reader = new StreamReader("input.txt")) Output each line from the text file Output one last line that says: "THE END OF FILE"arrow_forward
- Write a program that prompts the user for an input file name, reads all words from the input file, and writes the words to the output file named sentences.txt. Start a new line whenever a word ends in a period, question mark, or exclamation mark.arrow_forwardWrite a program that reads two input files whose lines are ordered by a key data field. Your program should merge these two files, writing an output file that contains all lines from both files ordered by the same key field. As an example, if two input files contain student names and grades for a particular class ordered by name (the key field), merge the information as shown below. File 1 and file 2 are supplied. Here is an algorithm to merge the data from two files: Read a line from each data file While the end of both files has not been reached While the end of both files has not been reached Write the line from file 1 to the output file and read a new line from file 1. Else Write the line from file 2 to the output file and read a new line from file 2. Write the remaining lines (if any) from file 1 to the output file. Write the remaining lines (if any) from file 2 to the output file. See the Merging Filesslides attachedto the project in Canvasfor a visual look at this algorithm.arrow_forwardWrite a program to read two file names from the command line. If the number of arguments in the command line are more than or less than two, then the program should terminate with an appropriate error message. Otherwise, it should append the contents of the second file to the first file.arrow_forward
- Write an automated censor program that reads in the text from a file andcreates a new file where all of the four-letter words have been replaced by11 **** 11• You can ignore punctuation, and you may assume that no wordsin the file are split across multiple lines.arrow_forwardSuppose you are given a text file that contains the names of people. Every name in the file consists of a first name and last name. Unfortunately, the programmer that created the file of names did not guarantee that each name was on a single line of the file. Read this file of names and write them to a new text file sorted according to first name, one name per line. For example, if the input file contains Ed Marston Bob Jones Jeff Williams Fred Charles The output file should be Bob Jones Ed Marston Fred Charles Jeff Williams Use arrays to solve the problem.arrow_forwardWrite a Python program that reads from the user the name of two files (an input file and an output file). The program will then read each line of the input file and save it in the output file preceded with the line number and removing any of the following punctuations (. , ! ? ; :) found at the end of the line. For example, if the input file is:Mary had a little lamb;Whose fleece was white as snow; And everywhere that Mary went; The lamb was sure to go!Then the output file the content of the input file and saves it in the produced output file will beLine 1: Mary had a little lambLine 2: Whose fleece was white as snow Line 3: And everywhere that MaryLine 4: The lamb was sure to goarrow_forward
- After every word on a line is spellchecked, it should be written to an output file called checked.txt. The arrangement of words on lines should be the same in the document and in checked.txt. The difference between the document and checked.txt is that in checked.txt each word is in lowercase and there is no punctuation. Also, some words might be changed by the spellcheck process. For example, suppose the document contains the following: The Hunter Moon waxed round in the night sky, and put to flight all the lesser stars. But low in the South one star shone red. Then checked.txt will contain: the hunter moon waxed round in the night sky and put to flight all the lesser stars but low in the south one star shone red (In a real spellchecker, the output will be in the same case as the original, and all punctuation will be retained. For simplicity, we will not worry about case and punctuation.) How to Process Words Which Are Not in the Dictionary The spellcheck method also needs to handle…arrow_forwardWrite a Python program to combine each line from the first file with the corresponding line in the second file and then save it in a 3rd file. Consider, both the files have same number of lines. Assume, each line has a newline(\n) at the end. ========================================================= Example:Input from file 1:HelloHiNice Input from file 2:BadWorseWorst Output in 3rd FIle:Hello BadHi WorseNice Worst ========================================================= Hint(1):Use the write() function what will be the answer of this in python programming languagearrow_forwardWrite a program that prompts the user to enter the name ofan author and the title of one their books. It writes the author’s name to a file.Their book’s titleis written at the end of a file Books.txt with the line number of the author’sname in Authors.txt inserted before the title. Assume that the title is notalready stored in Books.txt and the author name is already in Authors.txt.arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr