C++ Write a program named Lab5b.cpp . This program reads expense data from a fil
ID: 3866138 • Letter: C
Question
C++
Write a program named Lab5b.cpp. This program reads expense data from a file in a predefined format and produces an expense report.
Follow the steps described here to complete the program.
Collecting Data From The Data File
Define a named constant for the size of the arrays. For example, const int NUM_DAYS = 7;
Define an array of strings for the days and an array of doubles for the expenses using the named constant as the array size. Initialize the array of doubles with 0.0, not 0. Each day in the array of days corresponds to the expense in the same position in the array of expenses.
Check. The arrays have been defined using meaningful identifiers.
Check. The arrays have been defined using the named constant as the array size.
Check. The array of doubles have been initialized with 0.0.
Define a named string constant for the file expenses.txt which was created by lab3a. You may also create the expense file by hand using a text editor.
Open the file using the named string constant.
Read the data from the expense file into the arrays. The code for reading the expense records should not assume there are exactly seven records in the file. Use a counter variable to keep track of the number of records received from the file. Other than reading and counting the number of expense records, don't do any data processing and reporting at this step.
Close the file.
Check. The number 7 is not used in any form in the process of reading the file.
Check. No calculation for total, average, and largest at this step.
Check. No reporting at this step.
Check. File is closed.
Process Data
Process the data in the arrays. Calculate the total, average, and largest expense.
Create Report
Generate the expenses report as shown below.
Explanation / Answer
Answer:
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
const num_days=7;
int main(int argc, char * argv[])
{
std::fstream myfile("numbers.txt", std::ios_base::in);
string weekdays[num_days];
double sum,avg,largest;
double expense[num_days];
for(int i=0;i<num_days;i++){
expense[i]=0.0;
}
int cnt=0;
fin.open(file_name.c_str(),ios::in);
if (!fin.is_open())
{
cerr<<"Unable to open file "<<file_name<<endl;
exit(10);
}
int max=0;
while (!fin.fail())
{
cout<<"Read integer: "<<x<<endl; // display number again
expense[cnt]=x;
sum=sum+x;
if(max>x)
max=x;
}
fin.close();
cout<<sum<<endl;
cout<<max<<endl;
cout<<(sum/7)<<
return 0;
}
create the file with given name and add the expense in the file for seven days.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.