Question

write a c++ code to read multiple integers from input.dat until the end of file. Edit...

write a c++ code to read multiple integers from input.dat until the end of file.

Edit input.dat and put several integers in it. Compile and execute the code then check

output.dat to verify all the integers are in there.

Update the code to check to see if input.dat exists before reading from it. If it

doesn’t exist print an error message (and don’t read!). Compile and execute your code.

Delete input.dat and output.dat and execute – did you see your error message? Is output.dat

there (and empty)? Should it be there at all?

Add code to get the names of both input and output files to the program.

Compile and execute your code. Test it with an input file that does exist. Test it with an

input file that does not exist. Add two small functions to your modified program. One that reads the

number from the input file. One that writes the number to the output file. Write the numbers

formatted as dollar amounts, with a $ and exactly 2 digits to the right of the decimal. Add code to test to see if the input file is empty or not. Print a message if it is and end the program.

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

Code

#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<iomanip>
using namespace std;
//function prototypes
void readFile(ifstream &, vector<double>&);
void writeFile(ofstream &, vector<double>);
int main()
{
   ifstream in;
   string inputFile, outputFile;
   vector<double> values;
   //taking input and output file names
   cout << "Enter input filename: ";
   cin >> inputFile;  
   cout << "Enter output filename: ";
   cin >> outputFile;
   in.open(inputFile);
   if (!in)//checks if file opened or not
   {
       cout << inputFile << " is not found! Exiting program." << endl << endl;
       system("pause");
       return 0;
   }
   if (in.peek() == std::ifstream::traits_type::eof())//checks if file is empty or not
   {
       cout << inputFile << " is empty. Exiting program" << endl << endl;
       system("pause");
       return 0;
   }

   ofstream out(outputFile);
   readFile(in,values);//call the function to read the integer into vector
   writeFile(out, values);//call the function to write to the output file
   in.close();
   out.close();
   system("pause");
   return 0;
}
//function that will read the number and add to the vectors
void readFile(ifstream &in, vector<double>& values)
{
   double val;
   while (!in.eof())
   {
       in >> val;
       values.push_back(val);
   }
}
//function that will write all the number with dollar sign to output file
void writeFile(ofstream &out, vector<double>values)
{
   out << fixed << setprecision(2);
   for (int i = 0; i < values.size(); i++)
       out <<"$" <<values.at(i) << endl;
}

output

where file is not found

when file is empty

when input file has some data

input.dat

output.dat

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
write a c++ code to read multiple integers from input.dat until the end of file. Edit...
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
  • C++ 3. Write a program that reads integers from a file, sums the values and calculates...

    C++ 3. Write a program that reads integers from a file, sums the values and calculates the average. a. Write a value-returning function that opens an input file named in File txt. You may "hard-code" the file name, i.e., you do not need to ask the user for the file name. The function should check the file state of the input file and return a value indicating success or failure. Main should check the returned value and if the file...

  • Please do in java as simply as possible with code available for copy and comments Write...

    Please do in java as simply as possible with code available for copy and comments Write a program that reads a file (provided as attachment to this assignment) and writes the file to a different file with line numbers inserted at the beginning of each line. Throw an exception if the file does not exist. An error message should result. For example if file input is: This is a test File output should be 1. This is a test I...

  • The name of the C++ file must be search.cpp Write a program that will read data...

    The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...

  • Write a program that takes a file as input and checks whether or not the content...

    Write a program that takes a file as input and checks whether or not the content of the file is balanced. In the context of this assignment “balanced” means that your program will check to make sure that for each left bracket there is a closing right bracket. Examples:             [ ( ) ] { } à balanced.             [ ( ] ) { } à unbalanced. Here is the list of brackets/braces that program must support: (: left parentheses...

  • JAVA Write a program that prompts the user to enter a file name, then opens the...

    JAVA Write a program that prompts the user to enter a file name, then opens the file in text mode and reads it. The input files are assumed to be in CSV format. The input files contain a list of integers on each line separated by commas. The program should read each line, sort the numbers and print the comma separated list of integers on the console. Each sorted list of integers from the same line should be printed together...

  • Must be written in JAVA Code Write a program that will read in a file of student academic credit data and create a list of students on academic warning. The list of students on warning will be written...

    Must be written in JAVA Code Write a program that will read in a file of student academic credit data and create a list of students on academic warning. The list of students on warning will be written to a file. Each line of the input file will contain the student name (a single String with no spaces), the number of semester hours earned (an integer), the total quality points earned (a double). The following shows part of a typical...

  • Please go to the Practice Exercises (at the end of Chapter 3 in your text on...

    Please go to the Practice Exercises (at the end of Chapter 3 in your text on page 129) and do Exercise E 3.14 which asks you to compute and print the current season of the year depending on the month and day (input as integers). Be sure to review Java boolean operators, multi-way IF-ELSE statements, Switch statements, and String comparison methods before implementing your Java code. The algorithm for this program has been written for you and is included in...

  • Linux & Unix Write a bash program to indent the code in a bash source file....

    Linux & Unix Write a bash program to indent the code in a bash source file. Conditions:     The source file will contain only printing characters, spaces, and newlines. It is not necessary to check for invalid input.     The source file will not contain comments (words beginning with #). Requirements:     Read from standard input, write to standard output.     Code inside while statements should be indented 2 spaces. Be sure your program includes all of the following:    ...

  • Write in Java please. The purpose of this program is to read a file, called WaterData.csv....

    Write in Java please. The purpose of this program is to read a file, called WaterData.csv. It will output date and gallons. So it would look something like "2/04/15 40 Gallons". The pseudo code is as follows. prompt the user for a file name open the file if that file cannot be opened display an error message and quit endif create a String variable 'currentDate' and initialize it to the empty string. create a variable gallonsUsed and initialize it to...

  • Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The progra...

    Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs opening any of the 3 For example, (user input shown in caps in first line) Enter first...

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
ADVERTISEMENT