Reference parameters and files...

Hi - beginner w/ C++ here. My professor gave us an assignment that I can't quite make heads or tails of:

Now you will edit the file project5.cpp.

You are going to rewrite your program to use reference parameters and also files.

Add a function to your program that asks the user for the name of the data file. This function is to open the file. If the open fails the program is to report this and exit.
Combine the functions that read the name of the item sold, its cost, and the number of items of this kind that was sold. The values that are read should be returned in reference parameters.
Change the function that reads the item sold price to read from the file.
A sentinel value is no longer needed because in the outer loop you will be able to test for the end of file.

The output of program #5 is the same as the output from program #4.

The main program should consist of a series of calls to functions that do the work. No input, processing, or output should be in the main function. No global variables may be used.

I completed the code for project 4 successfully, but am utterly lost re: getting it to read files + reference parameters. I've been reading about it but it's going over my head. Does anyone know where I can find resources specific to this assignment or have any advice? I'm really confused.

Here is my code for project 4:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
  #include <iostream>
#include <cmath>
using namespace std;


//Instructs the user.
void printInstructions()
{
	cout << "This program will calculate and display" << endl;
	cout << "the gross and net profits of today's sales." << endl << endl;
	cout << "Enter the name of the item, the cost, the number" << endl;
	cout << "of this item that sold, and the store's selling price." << endl << endl;
	cout << "Press <return> after each entry." << endl << endl;
	cout << "Enter 'fine' in the item line when there are no more items left to calculate." << endl << endl;
}


//Obtain name of item.
string getItem()
{
string item;
cout << "Enter name of item: ";
cin >> item;
return item;
}

//Obtain the price paid for the item.
double getPricePaid()
{
double pricePaid;
cout << "Enter the cost of item: ";
cin >> pricePaid;
return pricePaid;
}

//Obtain the number of this item that sold.
int numberSold()
{
int sold;
cout << "Enter the number of this item that sold: ";
cin >> sold;
return sold;
}

//Obtain the selling price.
double getSellingPrice()
{
double sellingPrice;
cout << "Enter the selling price of the item: ";
cin >> sellingPrice;
return sellingPrice;
}

//Calculate gross profit.
double calculateGrossProfit(double pricePaid, double sellingPrice, double grossProfit)
{
grossProfit = sellingPrice - pricePaid;
return grossProfit;
}

//Calculate net profit. 
double calculateNetProfit (double grossProfit, double sellingPrice, double netProfit)
{
netProfit = grossProfit - (sellingPrice * .16);
return netProfit;
}

//Display results.
void printResults(double pricePaid, double sellingPrice, double grossProfit, double netProfit)
{
cout << "Gross profit: $"
<< grossProfit << endl; 
cout << "Net profit: $"
<< netProfit << endl;
}



int main()
{
string item;										//Item name.
double pricePaid;									//Item cost.
int sold;											//Number of item that sold.
double sellingPrice;								//Selling price of the item.
double grossProfit;									//Calculated gross profit.
double netProfit;									//Calculated net profit.

printInstructions();

item = getItem();

while (item != "fine")

{

pricePaid = getPricePaid();

sold = numberSold();

sellingPrice = getSellingPrice();

grossProfit = calculateGrossProfit(pricePaid, sellingPrice, grossProfit);

netProfit = calculateNetProfit (grossProfit, sellingPrice, netProfit);

printResults(pricePaid, sellingPrice, grossProfit, netProfit);

item = getItem();
}

return 0;
}
About interact with file you can read here,
http://www.cplusplus.com/doc/tutorial/files/
Ref, http://www.cplusplus.com/reference/fstream/

Add a function to your program that asks the user for the name of the data file. This function is to open the file. If the open fails the program is to report this and exit.

> Just a new function for open file.
> see Open a file in the link., http://www.cplusplus.com/reference/fstream/ifstream/open/
> shouldn't forget about Closing a file

Combine the functions that read the name of the item sold, its cost, and the number of items of this kind that was sold. The values that are read should be returned in reference parameters. Change the function that reads the item sold price to read from the file.

> Apply read() to your input functions,
http://www.cplusplus.com/reference/istream/istream/read/
> About pass by reference, http://www.cplusplus.com/doc/tutorial/functions/, Arguments passed by value and by reference


A sentinel value is no longer needed because in the outer loop you will be able to test for the end of file.

> just apply while (item != "fine") with end-of-file condition
Topic archived. No new replies allowed.