I am working on a program with function files and I am not sure how to ask this so this might be kind of interesting. We were given an input file that I saved to my desktop. This is all I did with it and I am not sure if I am to put it somewhere in my visual studio program or not. When I run it it only shows error so I must have done something wrong just trying to figure out if everything I have looks good so far and just have not programmed it enough or I already have an error....
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
void OpenFiles(ifstream &inp, ofstream &out, string filename);
void OutputHeaders();
double CalculateCost(double cost, int quantity, double total_cost, double final_cost);
int main()
{
ifstream inp;
ofstream out;
OpenFiles(inp, out, "p05.txt");
OpenFiles(inp, out, "p05out.txt");
OutputHeaders();
inp.close();
out.close();
return 0;
}
void OpenFiles(ifstream &inp, ofstream &out, string filename)
{
{
inp.open(filename.c_str());
if (inp.fail())
{
cerr << "Error\n";
exit(1);
}
}
{
out.open(filename.c_str());
if (out.fail())
{
cerr << "Error\n";
exit(1);
}
}
}
void OutputHeaders()
{
cout << "Programmed by Jim Johnson";
cout << endl << endl << endl;
cout << setw(15) << "Item" << setw(18) << "Cost" << setw(21) << "Quantity" << setw(24) << "Total Cost" << endl;
cout.setf(ios::right);
cout << setw(15) << "----" << setw(18) << "----" << setw(21) << "--------" << setw(24) << "----------" << endl;
cout.setf(ios::right);
cout << endl;
}
double CalculateCost(double cost, int quantity, double total_cost, double final_cost)
{
total_cost = quantity * cost;
final_cost = final_cost + total_cost;
return total_cost;
}
Assignment says:
Write a program that reads in the name of an item, the cost of an item and quantity of how many were purchased from a text file, and writes the cost, quantity, total cost and a grand total to a text file. All input and output are being done with text files. The input file, P05.txt is available on blackboard. The output file you create should be called P05out.txt
Your output should be neatly displayed in columns that are right justified.
There should be five functions:
OpenFiles – opens and tests the input and output file
OutputHeaders – writes your name and the column headers to the file
ReadFromFile – reads the information from the input file
CalculateCost – calculates and returns the total cost
WritetoFile – write the information to the output file
The final total calculations and the writing of the final total to the output file can be done in main.
Given below are the input file that is on the website and the output file that your program should create.
Input File:
purse 45.3 6
shoes 34.83 2
blouse 18.99 12
socks 2.5 51
jeans 23.7 34
shoelace .99 105
Output File:
Programmed by <Your Name>
Item Cost Quantity Total Cost
---- ---- -------- ----------
purse 45.30 6 271.80
shoes 34.83 2 69.66
blouse 18.99 12 227.88
socks 2.50 51 127.50
jeans 23.70 34 805.80
shoelace 0.99 105 103.95
-------------
Final Total 1606.59
If I didn't explain what I am asking please let me know and I will attempt to be more clear