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

Recommended Answers

All 23 Replies

I think you are going to have a problem in this function:

void OpenFiles(ifstream &inp, ofstream &out, string filename);

You are passing it one filename and then trying to use that same filename to open an ifstream AND an ofstream (lines 32 and 40). Seems to me that you want to pass the function two filenames: one filename to go with the ifstream and one filename to go with the ofstream. Looks like you want "p05.txt" to always be associated with an ifstream and "p05out.txt" to always be associated with an ofstream. However that is not how you are treating them in your program. Pass your function one filename for the ifstream and one filename for the ofstream. Don't open an ofstream with "p05.txt" or an ifstream with "p05out.txt".

Yes I know I would think the logical idea would be to put two sep functions but from the instructions they can be only one function

so not sure how to do this if I can only use one function

It can be done in one function as well as it can be done in two. Add a parameter to the function:

void OpenFiles(ifstream &inp, ofstream &out, string insFilename, string outsFilename);

Pass both filenames to the function in the call:

OpenFiles(inp, out, "p05.txt", "p05Out.txt");

everything look good that is done so far...

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

void OpenFiles(ifstream &inp, ofstream &out, string inpfilename, string outfilename);
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", "p05out.txt");
	OpenFiles(inp, out, "p05.txt", "p05out.txt");

	OutputHeaders();
	
	inp.close();
	out.close();
	
	return 0;
}

void OpenFiles(ifstream &inp, ofstream &out, string inpfilename, string outfilename)
{
	{
		inp.open(inpfilename.c_str());
		if (inp.fail())
		{
			cerr << "Error\n";
			exit(1);
		}
	}
	{
		out.open(outfilename.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;
}

everything look good that is done so far...

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

void OpenFiles(ifstream &inp, ofstream &out, string inpfilename, string outfilename);
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", "p05out.txt");
	OpenFiles(inp, out, "p05.txt", "p05out.txt");

	OutputHeaders();
	
	inp.close();
	out.close();
	
	return 0;
}

void OpenFiles(ifstream &inp, ofstream &out, string inpfilename, string outfilename)
{
	{
		inp.open(inpfilename.c_str());
		if (inp.fail())
		{
			cerr << "Error\n";
			exit(1);
		}
	}
	{
		out.open(outfilename.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;
}

Well, what happens when you run it? You can lose the excess brackets in OpenFiles. They don't hurt anything, but they don't do anything either. In Calculate_Cost, you pass everything by value, so line 63 will have no effect, and you might as well not pass the total_cost and final_cost parameters because you're immediately writing over total_cost in line 62, and like I said, since final_cost is passed by value and not by reference, your calculation in line 63 will be lost as soon as the function has finished executing.

having a little bit of a problem running the output file and just seeing if anyone could take a look and let me know if I am doing something wrong...

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

void OpenFiles(ifstream &inp, ofstream &out, string inpfilename, string outfilename);
void OutputHeaders();
void ReadFromFile(ifstream &inp, int &cost, int &quantity, double &total_cost, double &final_cost);
double CalculateCost(double cost, int quantity, double total_cost, double &final_cost);
void WritetoFile(ofstream &out, int &cost, int &quantity, double &total_cost, double &final_cost);

int main()
{
	ifstream inp;
	ofstream out;
	double cost, total_cost, final_cost;
	int quantity;

	OpenFiles(inp, out, "p05.txt", "p05out.txt");
	OpenFiles(inp, out, "p05.txt", "p05out.txt");

	OutputHeaders();
	ReadFromFile(inp, cost, quantity, total_cost, final_cost);
	CalculateCost(cost, quantity, total_cost, final_cost);
	WritetoFile(out, cost, quantity, total_cost, final_cost);

	inp.close();
	out.close();
	
	return 0;
}

void OpenFiles(ifstream &inp, ofstream &out, string inpfilename, string outfilename)
{
	{
		inp.open(inpfilename.c_str());
		if (inp.fail())
		{
			cerr << "Error\n";
			exit(1);
		}
	}
	{
		out.open(outfilename.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;
}

void ReadFromFile(ifstream &inp, int &cost, int &quantity, double &total_cost, double &final_cost)
{
	while(!inp.eof())
	{
		inp >> cost >> quantity >> total_cost >> final_cost;
	}
}

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;
}

void WritetoFile(ofstream &out, int &cost, int &quantity, double &total_cost, double &final_cost)
{
	out << cost << quantity << total_cost << final_cost;
}

having a little bit of a problem running the output file

Could you be more specific about the problem(s)?

Could you be more specific about the problem(s)?

i think everything looks good from the previous code but just getting these two errors:

1>c:\documents and settings\don & diane kruep\desktop\project 5\p05out.cpp(29) : error C2664: 'ReadFromFile' : cannot convert parameter 2 from 'double' to 'int &'

1>c:\documents and settings\don & diane kruep\desktop\project 5\p05out.cpp(31) : error C2664: 'WritetoFile' : cannot convert parameter 2 from 'double' to 'int &'

I got the errors to fix just unable to get the output file to work could anyone take a look...

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

void OpenFiles(ifstream &inp, ofstream &out, string inpfilename, string outfilename);
void OutputHeaders();
void ReadFromFile(ifstream &inp, double &cost, int &quantity, double &total_cost, double &final_cost);
double CalculateCost(double &cost, int &quantity, double &total_cost, double &final_cost);
void WritetoFile(ofstream &out, double &cost, int &quantity, double &total_cost, double &final_cost);

int main()
{
	ifstream inp;
	ofstream out;
	double cost, total_cost, final_cost;
	int quantity;

	OpenFiles(inp, out, "p05.txt", "p05out.txt");
	OpenFiles(inp, out, "p05.txt", "p05out.txt");

	OutputHeaders();
	ReadFromFile(inp, cost, quantity, total_cost, final_cost);
	CalculateCost(cost, quantity, total_cost, final_cost);
	WritetoFile(out, cost, quantity, total_cost, final_cost);

	inp.close();
	out.close();
	
	return 0;
}

void OpenFiles(ifstream &inp, ofstream &out, string inpfilename, string outfilename)
{
	{
		inp.open(inpfilename.c_str());
		if (inp.fail())
		{
			cerr << "Error\n";
			exit(1);
		}
	}
	{
		out.open(outfilename.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;
}

void ReadFromFile(ifstream &inp, double &cost, int &quantity, double &total_cost, double &final_cost)
{
	while(!inp.eof())
	{
		inp >> cost >> quantity >> total_cost >> final_cost;
	}
}

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;
}

void WritetoFile(ofstream &out, double &cost, int &quantity, double &total_cost, double &final_cost)
{
	out << cost << quantity << total_cost << final_cost;
}

I made some updates today but still can't get the program to input to the output file and not sure what I am doing wrong

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

void OpenFiles(ifstream &inp, ofstream &out, string inpfilename, string outfilename);
void OutputHeaders();
void ReadFromFile(ifstream &inp, double &cost, int &quantity, double &total_cost, double &final_cost);
double CalculateCost(double &cost, int &quantity, double &total_cost, double &final_cost);
void WritetoFile(ofstream &out, double &cost, int &quantity, double &total_cost, double &final_cost);

int main()
{
	ifstream inp;
	ofstream out;
	double cost, total_cost, final_cost;
	int quantity;

	OpenFiles(inp, out, "p05.txt", "p05out.txt");
	OpenFiles(inp, out, "p05.txt", "p05out.txt");

	OutputHeaders();

	while(!inp.eof())
	{
		ReadFromFile(inp, cost, quantity, total_cost, final_cost);
		CalculateCost(cost, quantity, total_cost, final_cost);
	}	WritetoFile(out, cost, quantity, total_cost, final_cost);

	inp.close();
	out.close();
	
	return 0;
}

void OpenFiles(ifstream &inp, ofstream &out, string inpfilename, string outfilename)
{
	{
		inp.open(inpfilename.c_str());
		if (inp.fail())
		{
			cerr << "Error\n";
			exit(1);
		}
	}
	{
		out.open(outfilename.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;
}

void ReadFromFile(ifstream &inp, double &cost, int &quantity, double &total_cost, double &final_cost)
{
	while(inp.good())
	{
		cost = inp.get();
		quantity = inp.get();
		total_cost = inp.get();
		final_cost = inp.get();
	}
}

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;
}

void WritetoFile(ofstream &out, double &cost, int &quantity, double &total_cost, double &final_cost)
{
	out << cost << quantity << total_cost << final_cost;
}

does anyone not know what I am talking about because I can try to explain it a little better if that is the case

I can try to explain it a little better if that is the case

A good idea, so please do that ...

OK please someone tell me if this is still confusing and I can try explaining it some other way. What my problem is is that a compile with no errors...when I run my program however this is the out put that I get....

error
press any button to continue...

The error is coded in my program as you can see...the problem however when the box that comes up saying "This file has been modified outside of the source editor.
Do you want to reload it?"

yes yes to all no no to all

error
press any button to continue...

So the problem seems to be in opening the file(s) .. delete one of the OpenFiles() calls, i.e. you should do with only a single

OpenFiles(inp, out, "p05.txt", "p05out.txt");

in your program.

Ok I got the output file to run without an error but nothing comes up in the output file...can anyone take a look to see why the output file is blank

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

void OpenFiles(ifstream &inp, ofstream &out, string inpfilename, string outfilename);
void OutputHeaders(ifstream &inp, ofstream &out);
void ReadFromFile(ifstream &inp, double &cost, int &quantity, double &total_cost, double &final_cost);
double CalculateCost(double &cost, int &quantity, double &total_cost, double &final_cost);
void WritetoFile(ofstream &out, double &cost, int &quantity, double &total_cost, double &final_cost);

int main()
{
	ifstream inp;
	ofstream out;
	double cost, total_cost, final_cost;
	int quantity;
	
	OpenFiles(inp, out, "P05.txt", "P05out.txt");
	
	OutputHeaders(inp, out);
	
	while(!inp.eof())
	{
		
		ReadFromFile(inp, cost, quantity, total_cost, final_cost);
		CalculateCost(cost, quantity, total_cost, final_cost);
		WritetoFile(out, cost, quantity, total_cost, final_cost);
	}

	inp.close();
	out.close();
	
	return 0;
}

void OpenFiles(ifstream &inp, ofstream &out, string inpfilename, string outfilename)
{
	{
		inp.open(inpfilename.c_str());
		if (inp.fail())
		{
			cerr << "Error\n";
			exit(1);
		}
	}
	{
		out.open(outfilename.c_str());
		if (out.fail())
		{
			cerr << "Error\n";
			exit(1);
		}
	}
}

void OutputHeaders(ifstream &inp, ofstream &out)
{
	inp.open("P05.txt");
	out.open("P05out.txt");
	out << "Programmed by Jim Johnson";
	out << endl << endl << endl;
	out << setw(15) << "Item" << setw(18) << "Cost" << setw(21) << "Quantity" << setw(24) << "Total Cost" << endl;
	out.setf(ios::right);
	out << setw(15) << "----" << setw(18) << "----" << setw(21) << "--------" << setw(24) << "----------" << endl;
	out.setf(ios::right);
	out << endl;
}

void ReadFromFile(ifstream &inp, double &cost, int &quantity, double &total_cost, double &final_cost)
{
	while(inp.good())
	{
		cost = inp.get();
		quantity = inp.get();
		total_cost = inp.get();
		final_cost = inp.get();
	}
}

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;
}

void WritetoFile(ofstream &out, double &cost, int &quantity, double &total_cost, double &final_cost)
{
	out << cost << quantity << total_cost << final_cost;
}

.however when I run it, it does not run anything from the input file I will show you what runs along with my code:

output is:

Programmed by Jim Johnson

       Item              Cost             Quantity              Total Cost
       ----              ----             --------              ----------

                           -1                   -1                       1

The only thing I can get from this is the calculation function is obviously working...

here is my code:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

void OpenFiles(ifstream &inp, ofstream &out, string inpfilename, string outfilename);
void OutputHeaders(ifstream &inp, ofstream &out);
void ReadFromFile(ifstream &inp, string item, double &cost, int &quantity, double &total_cost, double &final_cost);
double CalculateCost(double &cost, int &quantity, double &total_cost, double &final_cost);
void WritetoFile(ofstream &out, string item, double &cost, int &quantity, double &total_cost, double &final_cost);

int main()
{
    ifstream inp;
    ofstream out;
    double cost = 0.0,
    total_cost = 0.0,
    final_cost = 0.0;
    int quantity = 0;
    string item = " ";

    OpenFiles(inp, out, "P05.txt", "P05out.txt");

    OutputHeaders(inp, out);

    while(!inp.eof())
    {

        ReadFromFile(inp, item, cost, quantity, total_cost, final_cost);
        CalculateCost(cost, quantity, total_cost, final_cost);
        WritetoFile(out, item, cost, quantity, total_cost, final_cost);
    }

    inp.close();
    out.close();

    return 0;
}

void OpenFiles(ifstream &inp, ofstream &out, string inpfilename, string outfilename)
{
    {
        inp.open(inpfilename.c_str());
        if (inp.fail())
        {
            cerr << "Error\n";
            exit(1);
        }
    }
    {
        out.open(outfilename.c_str());
        if (out.fail())
        {
            cerr << "Error\n";
            exit(1);
        }
    }
}

void OutputHeaders(ifstream &inp, ofstream &out)
{

    out << "Programmed by Jim Johnson";
    out << endl << endl;
    out << setw(15) << "Item" << setw(18) << "Cost" << setw(21) << "Quantity" << setw(24) << "Total Cost" << endl;
    out.setf(ios::right);
    out << setw(15) << "----" << setw(18) << "----" << setw(21) << "--------" << setw(24) << "----------" << endl;
    out.setf(ios::right);
    out << endl;
}

void ReadFromFile(ifstream &inp, string item, double &cost, int &quantity, double &total_cost, double &final_cost)
{
    while(inp.good())
    {   
        item = inp.get();
        cost = inp.get();
        quantity = inp.get();
        inp >> item >> cost >> quantity;
    }
}

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;
}

void WritetoFile(ofstream &out, string item, double &cost, int &quantity, double &total_cost, double &final_cost)
{

    if(item == " ")
    {
        out << setw(15) << item << setw(18) << cost << setw(21) << quantity << setw(24) << total_cost; 
        out.setf(ios::right);
    }
}

I wanna say it is something in my writetofile function because I think the read to file which is possibly the only other place which could present a problems seems right any help?

The major failure seems to be in the ReadFromFile(), you have a while loop in it, which most certainly should not be there.

I suggest you to radically change the ReadFromFile().

1) make it return a value that indicates whether the read was successful
2) pass the 'item' by reference
3) remove the total_cost and final_cost arguments
4) remove the while() loop

[B]bool[/B] ReadFromFile(ifstream &inp, string [B]&[/B] item, double &cost, int &quantity)
{
    // The following returns true as long as inp is capable of reading in
    // data for all three arguments
    return (bool)(inp >> item >> cost >> quantity);
}

Then, in your main() function you can use the function like

while(ReadFromFile(inp, item, cost, quantity))
{
    CalculateCost(cost, quantity, total_cost, final_cost);
    WritetoFile(out, item, cost, quantity, total_cost);
}

Lastly, I think you should remove the

if(item == " ")

block and the final_cost argument from WritetoFile(), i.e. WritetoFile() would be

void WritetoFile(ofstream &out, string item, double &cost, int &quantity, double &total_cost)
{
    out.setf(ios::right);

    out << setw(15) << item << setw(18) << cost 
          << setw(21) << quantity << setw(24) << total_cost;
}

to an extent it works but with the exception of 2 strange issues...first when I compile and run it everything is fine but it is only taking the second 4th and 6th output from my input file....

For example my input file is:

purse 45.3 6
shoes 34.83 2
blouse 18.99 12
socks 2.5 51
jeans 23.7 34
shoelace .99 105

my output file shows:

Item Cost Quantity Total Cost
---- ---- -------- ----------

shoes 34.83 2 69.66
socks 2.5 51 127.5
shoelace 0.99 105 103.95


(dont worry about the formatting for it I just copied and pasted the text)

as you can see it skips each like...


Also one other thing maybe its jsut early but for some reason I am unable to get the 2 digits of precision which I should have coded under the calculation function....the rest of the issues I will be able to finish up on my own.....

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

void OpenFiles(ifstream &inp, ofstream &out, string inpfilename, string outfilename);
void OutputHeaders(ifstream &inp, ofstream &out);
bool ReadFromFile(ifstream &inp, string &item, double &cost, int &quantity);
double CalculateCost(double &cost, int &quantity, double &total_cost, double &final_cost);
void WritetoFile(ofstream &out, string item, double &cost, int &quantity, double &total_cost, double &final_cost);

int main()
{
	ifstream inp;
	ofstream out;
	double cost = 0.00,
	total_cost = 0.00,
	final_cost = 0.00;
	int quantity = 0;
	string item = " ";
	
	OpenFiles(inp, out, "P05.txt", "P05out.txt");
	
	OutputHeaders(inp, out);
	
	while(ReadFromFile(inp, item, cost, quantity))
	{
		CalculateCost(cost, quantity, total_cost, final_cost);
		WritetoFile(out, item, cost, quantity, total_cost, final_cost);
	}

	inp.close();
	out.close();
	
	return 0;
}

void OpenFiles(ifstream &inp, ofstream &out, string inpfilename, string outfilename)
{
	{
		inp.open(inpfilename.c_str());
		if (inp.fail())
		{
			cerr << "Error\n";
			exit(1);
		}
	}
	{
		out.open(outfilename.c_str());
		if (out.fail())
		{
			cerr << "Error\n";
			exit(1);
		}
	}
}

void OutputHeaders(ifstream &inp, ofstream &out)
{

	out << "Programmed by Jim Johnson";
	out << endl << endl;
	out << setw(15) << "Item" << setw(18) << "Cost" << setw(21) << "Quantity" << setw(24) << "Total Cost" << endl;
	out.setf(ios::right);
	out << setw(15) << "----" << setw(18) << "----" << setw(21) << "--------" << setw(24) << "----------" << endl;
	out.setf(ios::right);
	out << endl;
}

bool ReadFromFile(ifstream &inp, string &item, double &cost, int &quantity)
{
	inp >> item;
	inp >> cost;
	inp >> quantity;


	

	return (inp >> item >> cost >> quantity);
}

double CalculateCost(double &cost, int &quantity, double &total_cost, double &final_cost)
{
	total_cost = quantity * cost;
	final_cost = final_cost + total_cost;


	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(2);

	return total_cost;


}

void WritetoFile(ofstream &out, string item, double &cost, int &quantity, double &total_cost, double &final_cost)
{
	out.setf(ios::right);
	out << setw(15) << item << setw(18) << cost << setw(21) << quantity << setw(24) << total_cost;
	out << endl;
}

Hmm, you have made a major (negative) change to the ReadFromFile() ...
I try to explain ...

bool ReadFromFile(ifstream &inp, string &item, double &cost, int &quantity)
{
    inp >> item;     //<- extract a string from the file
    inp >> cost;     //<- extract a double from the file
    inp >> quantity; //<- extract a int from the file

    return (inp >> item >> cost >> quantity); //<- Do the above three operations again
}

So, you read two sets of items each time in that function, to remedy it you should
have it like

bool ReadFromFile(ifstream &inp, string &item, double &cost, int &quantity)
{
    // extract, string, double and int from the file
    // returning a value which indicates whether all three items were
    // successfully extracted
    return (inp >> item >> cost >> quantity);
}

I am unable to get the 2 digits of precision which I should have coded under the calculation function...

You are setting the precision on the 'cout' object, that has no impact on the ofstream object you are using. So configure the 'ofstream out' instead.

ok everything shows up but i have get and it only shows 2 characters instead of the whole name and for some reason my precision is still a little off...I am looking through my book and really dont see anything for it to be...

#include <iostream>
#include <string>
using namespace std;
int Get_Name_and_Num_Classes(string& name, int classes);
double Calculate_GPA(char& grade, int number, int classes, double sum);
void Get_Letter_Grade(char& grade, int classes);
int Convert_Let_to_Num(int number, int classes);
void Print_Results(string name, double gpa);

int main()
{
	string name;
	int sum = 0, number = 0;
	int classes = ' ';
	char grade = ' ';
	double gpa = 0.0;

	cout << "Programmed by Jim Johnson";
	cout << endl << endl << "Type xxx for the name to Exit" << endl << endl;

	while (name != "xxx")
	{
		classes = Get_Name_and_Num_Classes(name, classes);

		if (classes)
		{
			gpa = Calculate_GPA(grade, number, classes, sum);
		}
		
		Print_Results(name, gpa);
		}
}

int Get_Name_and_Num_Classes(string& name, int classes)
{
	cout << "Enter the student's last name: ";
	cin >> name;
	if(name == "xxx") {return 0;}

	cout << endl << "How many classes taken: ";
	cin >> classes;

	return (classes);
}

double Calculate_GPA(char& grade, int number, int classes, double sum)
{
	string name;
	int count = 1;
	double gpa = 0.00;

	while (count <= classes)
{
    number = Convert_Let_to_Num(number, classes);
	sum = sum + number;
    count = count + 1;
}
	
gpa = sum / classes;
	
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);

return gpa;
}

void Get_Letter_Grade(char& grade, int classes)
{
	string name;
	int count = 1;
	{
	cout << endl << "Enter letter grade " << count << ": ";
	cin >> grade;
	count ++;
	}
}
int Convert_Let_to_Num(int number, int classes)
{
	char grade = ' ';
	Get_Letter_Grade(grade, classes);
	
	if ((grade == 'a') || (grade == 'A'))
	{
		number = 4;
	}
	else if ((grade == 'b') || (grade == 'B'))
	{
		number = 3;
	}
	else if ((grade == 'c') || (grade == 'C'))
	{
		number = 2;
	}
	else if ((grade == 'd') || (grade == 'D'))
	{
		number = 1;
	}
	else if ((grade == 'f') || (grade == 'F'))
	{
		number = 0;
	}
 else
  number = 0;

	return (number);
}

void Print_Results(string name, double gpa)
{
	if (name == "xxx")
	{
	}
	else
cout << endl << "Student " << name << " has a semester GPA of " << gpa << endl << endl;
}

great everything works thanks much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.