Write a C++ program. The input file is ooo Verizon LTE 9:59 AM 88% jeffco.blackb
ID: 3769434 • Letter: W
Question
Write a C++ program. The input file is
ooo Verizon LTE 9:59 AM 88% jeffco.blackboard.com C Final CIS155 Spring 2015 Input file FinallSummaryInput.txt Output file FinalSummary.txt Listed in the input file are monthly bills for a credit card statement. Calculate a Sum of all records in the input file and calculate an average for the entire year. Display the output FinalSummary.txt as shown below. Be sure to open the input file FinalSummaryInput.txt file to see the records you will read and calculate. You must use functions to calculate the Sum and average calculations and to read and write the files. After application is run the console will display the following inal Progranning Problen 11 done heck "FinalSumnary.txt" for results! ress any key to continue.. The application will produce the following output text file. Name the output file FinalSummary.txt FinalSummary- Notepad File Edit Format View Help Credit card Statement Total of Payments is $22390.86 Average monthly phone bil1 is $1865.91Explanation / Answer
#include <stdio.h>
/* for exit() */
#include <stdlib.h>
int main(void)
{
float value, total = 0, count = 0;
/* fileptrIn and fileptrOut are variables of type (FILE *) */
FILE * fileptrIn, * fileptrOut;
// For testing the files existence
errno_t err = 0, err1 = 0;
// For file names & paths
char filenameIn[100], filenameOut[100];
// Make sure you have already created the file that contains some integers
// at your desired location for this example C:input.txt that contains 445.14 645.14 347.72 748.43 845.14 9545.14 551.78 449.73 645.14 652.65 645.14 348.99
printf("Please enter an input filename (use path if needed): ");
scanf_s("%s", filenameIn, 100);
// Make sure you have already created this empty file, for this example C:output.txt
printf("Please enter an output filename (use path if needed): ");
// scanf("%s", filenameOut);
scanf_s("%s", filenameOut, 100);
/* open files for reading, "r" and writing, "w" */
// fopen(filenameIn, "r"), use secure version here
// Test the files existence
err = fopen_s(&fileptrIn, filenameIn,"r");
err1 = fopen_s(&fileptrOut, filenameOut,"w");
if(err !=0 )
{
printf(" Error opening %s for reading. ", filenameIn);
exit (1);
}
else
printf(" Opening %s for reading is OK. ", filenameIn);
if(err1 != 0)
{
printf("Error opening %s for writing. ", filenameOut);
exit (1);
}
else
printf("Opening %s for writing is OK. ", filenameOut);
/* fscanf() */
printf(" Credit card statement: ");
printf(" The data: ");
// EOF != fscanf(fileptrIn, "%i", &value) but using a secure version here
while(EOF != fscanf_s(fileptrIn, "%i", &value))
{
printf("%f", value);
total += value;
++count;
}
/* end of while loop */
printf(" Total of payments is: %d ", total);
/* Write the average value to the file. */
/* fprintf() */
printf(" Calculate the average... ");
fprintf(fileptrOut, "Average phone bill of %i months = %f ", count, total/(double)count);printf("Average phone bill of %i months = %f ", count, total/(double)count);
printf("Check also your %s file content ", filenameOut);
if(fclose(fileptrIn) == 0)
printf("%s closed successfully ", filenameIn);
if(fclose(fileptrOut) == 0)
printf("%s closed successfully ", filenameOut);
return 0;
}
Thank you!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.