Value returning function issues

I have been going around-and-around with this and can't seem to figure out what I'm doing wrong. I'm a beginner and have a lot of difficulty with functions that return values. Please let me know if anyone sees anything obvious.

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
using namespace std;

const double MO_SALES_TAX_RATE = 0.04316;
const double KS_SALES_TAX_RATE = 0.068;

double saleAmountCalc   (int quantity, double price);
double salesTaxCalc     (double totalSalesAmt, string stateCode);
void   displaySalesData (string custName, double totalSalesAmt, double salesTaxAmt, string stateCode);

int main()
{
	string custName;
	string stateCode;
	int    quantity         = 0;
	double price            = 0;
	double totalSalesAmt    = 0;
	double salesTaxAmt      = 0;

	ifstream inputFile;
	inputFile.open("SalesRegister.txt");

	if (!inputFile)
	{
		cout << endl << "Input file not found." << endl << endl;
		system("pause");
		return  -1;
	}
	inputFile >> custName >> stateCode >> quantity >> price;
	double saleAmountCalc (int quantity, double price);
	double salesTaxCalc   (double totalSalesAmt, string stateCode);
	void displaySalesData (string custName, double totalSalesAmt, double salesTaxAmt, string stateCode);
	
	inputFile >> custName >> stateCode >> quantity >> price;

	while (!inputFile.eof())
	{
		totalSalesAmt = saleAmountCalc(quantity, price);
		salesTaxAmt = salesTaxCalc(totalSalesAmt, stateCode);
		totalSalesAmt += salesTaxAmt;

		inputFile >> custName >> stateCode >> quantity >> price;
	}

	inputFile.close();
	system("pause");
	return 0;
}

double saleAmountCalc (int quantity, double price)
{
	double saleAmount;
	saleAmount = quantity * price;

	return saleAmount;

}

double salesTaxCalc (double totalSalesAmt, string stateCode)
{
	
	double salesTaxAmt;
	string KS;

	if (stateCode == "KS")
		salesTaxAmt = totalSalesAmt * KS_SALES_TAX_RATE;
	else
		salesTaxAmt = totalSalesAmt * MO_SALES_TAX_RATE;

	return salesTaxAmt;
}

void displaySalesData (string custName, double totalSalesAmt, double salesTaxAmt, string stateCode)
{
	cout << fixed << setprecision(2);
	cout << left << setw(22) << custName << right << setw(1) << "$"
		<< setw(10) << totalSalesAmt << setw(3) << "$" << setw(8)
		<< salesTaxAmt << "  " << stateCode << endl;
}


Last edited on
Hi,

I have few corrections to suggest

Why ru declaring below functions again in main()

double saleAmountCalc (int quantity, double price);
double salesTaxCalc (double totalSalesAmt, string stateCode);
void displaySalesData (string custName, double totalSalesAmt, double salesTaxAmt, string stateCode);

may be u should remove them.
What is in SalesRegister.txt ?
The way you read the file does not look right. Using eof() does not work as you expect.
I use the functions in main() to call them. This program is to pull the custName, stateCode, quantity, & price from the txt file, calculate the total sales, then the tax on the total sales, and output in the void function. There are I think around 20 lines in the txt file.

When this runs, there aren't any errors, but nothing is output.
Lines 29-31: These function prototypes are redundant and in any case do not belong inside main.

Lines 28,33: Your're reading twice, thereby losing the first record.

Line 62: Unnecessary variable.

When this runs, there aren't any errors, but nothing is output.

Where do you ever call displaySalesData() ?

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

@Thomas1965 - The structure of the OP's loop will avoid the usual problem of looping on !eof(), but it's still a practice that IMO should be avoided.




I was trying to figure out the code button, but it doesn't work. I just added the terms into it to change it. Thanks though. I have modified it somewhat, mainly in the "while" statement, but still nothing. Removed the 4 lines you had mentioned.

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
using namespace std;

const double MO_SALES_TAX_RATE = 0.04316;
const double KS_SALES_TAX_RATE = 0.068;

double saleAmountCalc   (int quantity, double price);
double salesTaxCalc     (double totalSalesAmt, string stateCode);
void   displaySalesData (string custName, double totalSalesAmt, double salesTaxAmt, string stateCode);

int main()
{
	string custName;
	string stateCode;
	int    quantity         = 0;
	double price            = 0;
	double totalSalesAmt    = 0;
	double salesTaxAmt      = 0;

	ifstream inputFile;
	inputFile.open("SalesRegister.txt");

	if (!inputFile)
	{
		cout << endl << "Input file not found." << endl << endl;
		system("pause");
		return  -1;
	}
		
	inputFile >> custName >> stateCode >> quantity >> price;

	while (!inputFile.eof())
	{
		totalSalesAmt = saleAmountCalc(quantity, price);
		salesTaxAmt = salesTaxCalc(totalSalesAmt, stateCode);
		totalSalesAmt += salesTaxAmt;

		inputFile >> custName >> stateCode >> quantity >> price;
	}

	inputFile.close();
	system("pause");
	return 0;
}

double saleAmountCalc (int quantity, double price)
{
	double saleAmount;
	saleAmount = quantity * price;

	return saleAmount;

}

double salesTaxCalc (double totalSalesAmt, string stateCode)
{
	
	double salesTaxAmt;
	
	if (stateCode == "KS")
		salesTaxAmt = totalSalesAmt * KS_SALES_TAX_RATE;
	else
		salesTaxAmt = totalSalesAmt * MO_SALES_TAX_RATE;

	return salesTaxAmt;
}

void displaySalesData (string custName, double totalSalesAmt, double salesTaxAmt, string stateCode)
{
	cout << fixed << setprecision(2);
	cout << left << setw(22) << custName << right << setw(1) << "$"
		<< setw(10) << totalSalesAmt << setw(3) << "$" << setw(8)
		<< salesTaxAmt << "  " << stateCode << endl;
}
I repeat my previous question.

AbstractionAnon wrote:
Where do you ever call displaySalesData() ?


You're not going to display the sales data, if you never call the function.
> The structure of the OP's loop will avoid the usual problem of looping on !eof(),
If the input file does not end on line break, then the last record would be successful read, but it would not be processed as it reached eof.


Loop on the reading operation instead
1
2
3
while (inputFile >> custName >> stateCode >> quantity >> price) {
   //...
}

I have finally got this fixed and working properly. Another question though......in the output, I want the first line to have a dollar sign, but not the lines after that during the loop. How can I remove "$" after first record is read? Tried several options. My void function displays my data, so not sure there's a way to remove the "$".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
while (!inputFile.eof())
	{
		totalSalesAmt = saleAmountCalc(quantity, price);
		salesTaxAmt = salesTaxCalc(totalSalesAmt, stateCode);
		displaySalesData(custName, totalSalesAmt, salesTaxAmt, stateCode);
		salesTotal += totalSalesAmt;
		taxTotal += salesTaxAmt;

		if (stateCode == "KS")
			ksTax += salesTaxAmt;
		else
			moTax += salesTaxAmt;

		inputFile >> custName >> stateCode >> quantity >> price;
	}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Haroldson                $   180.60         $    7.79   MO
Holder                   $   733.50         $   31.66   MO
Bascomb                  $   395.55         $   26.90   KS
Maartens                 $   431.46         $   18.62   MO
Stivers                  $   418.00         $   18.04   MO
Tomlin                   $   167.40         $   11.38   KS
DuPuy                    $    75.90         $    5.16   KS
Pendleton                $   531.00         $   22.92   MO
Trenton                  $   369.99         $   25.16   KS
Bourne                   $  1621.75         $  110.28   KS
Madison                  $  3746.25         $  161.69   MO
Benton                   $   189.98         $    8.20   MO
Graham                   $   226.40         $    9.77   MO
Penniman                 $   111.75         $    7.60   KS
Wurtz                    $   670.00         $   28.92   MO
Granville                $   475.00         $   32.30   KS
Holder                   $   838.60         $   57.02   KS
Saville                  $   410.88         $   17.73   MO
Duncan                   $   370.90         $   16.01   MO
Brent                    $   810.00         $   34.96   MO
Last edited on
Topic archived. No new replies allowed.