Project using pass by value

This code is very lengthy, but I almost have it finished and ready to go. I have been testing the code here for awhile now, and i am having a couple small problems. 1, my math at the end isn't correct, and I have been looking through my logic but I can't seem to find what is wrong with the code. 2nd, when I switch the code to run through user inputs, I can't seem to get my code to output anything. Any suggestions or pointers would be appreciated. Thank you.

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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Project #2
//   complete the following program
//   test the program
//   first letting the computer generate the input
//   second letting user input only two sets of data
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
using namespace std;

const bool KEYBOARD = false;
ofstream Out("con");

int    getNumberOfSales(void);
int    getItemNumber(void);
double getPrice(int itemNumber);
bool   isTaxable(int itemNumber);
int    getQuantity(int itemNumber);
double getCost(int itemNumber, int count, double price);
double getTax(double sales);
void   printLine(ostream & w, int iNumber, int qty, double price, double cost, bool taxable);
void   printTotal(ostream & w, int loopCount, double grandTotal, double taxDue);
void   headings(ostream & w);
void   startRandom(void);
void   prepareOutput(ostream & w);

void main()
{
	int differentItems;       // Number of items to purchase
	int	iNumber;    // Item number code
	int	qty;        // Number of a particular item purchased
	double price;             // Price of a particular item
		double	cost;              // The cost of the item purchase = price * qty
		double		taxableTotal = 0;      // Total of all taxable purchases
		double		nonTaxableTotal = 0;   // Total of all nontaxable purchases
		double	taxDue = 0;            // Tax due on the taxable total
	double	grandTotal;        // Sum of taxableTotal, nonTaxableTotal
	bool   taxable;                   // Flag to indicate if the item is taxable

									  //  Initialize ONLY those TOTALS that need an initial value
	
	prepareOutput(Out);
	if (!KEYBOARD) startRandom();
	headings(Out); // for computer generated
	differentItems = getNumberOfSales();
	for (int i = 0; i < differentItems; i++)
	{
		iNumber = getItemNumber();
		qty = getQuantity(iNumber);
		price = getPrice(iNumber);
		cost = getCost(iNumber, qty, price);
		taxable = isTaxable(iNumber);
		// headings(Out);   // for keyboard input
		printLine(Out, iNumber, qty, price, cost, taxable);
		//  accumulate
		if (taxable != true)//   ******* -  taxable total
		{
			nonTaxableTotal = cost;
		}
		else
		{
			taxableTotal = cost * taxDue;
		}
									//   ********  -  nontaxable total
			
	}  // ENDFOR
	
	if (taxable = true)
	{
		taxableTotal = taxableTotal + cost;
	}
	else
	{
		nonTaxableTotal = nonTaxableTotal + cost;
	}
	taxDue = getTax(taxableTotal);
	grandTotal = taxableTotal + nonTaxableTotal;
	printTotal(Out, differentItems, grandTotal, taxDue);

}  //ENDMAIN


void startRandom(void)
{
	int seed;
	cout << "Enter seed value for random number generator: ";
	cin >> seed;
	srand(seed);
}
int getItemNumber(void)
{                             //  item number should be a 4-digit integer
	int num;
	if (KEYBOARD)
	{
		cout << "Enter item number: ";
		cin >> num;
	}
	else
		num = rand() % 9000 + 1000;
	return num;
}
double getPrice(int num)
{                            //  price should be between .10 and 10.09
	double price;
	if (KEYBOARD)
	{
		cout << "Enter price for item  " << num << " : ";
		cin >> price;
	}
	else
		price = double(rand() % 1000 + 10) / 100;
	return price;
}
bool isTaxable(int itemNumber)
{
	char user;
	bool f = false;
	bool t = true;
	if (KEYBOARD)//  ask the user
	{
		cout << "Press t for taxable and f for non-taxable" << endl;
		cin >> user;
	}
	else if (itemNumber / 5 != 0)
	{
		return f;
	}
	else
		return t;

	
}
int getQuantity(int num)
{
	int number;
	if (KEYBOARD)//  ask the user
	{
		cout << "Please enter a quantity" << endl;
		cin >> number;
	}
	else
		number = rand() % 8+1;
	//       OR
	return number; 
}
int getNumberOfSales(void)
{
	int number;
	if (KEYBOARD)//  ask the user
	{
		cout << "Please enter a number" << endl;
		cin >> number;
	}
	else
		number = rand() % 15 + 1;
	return number;
}
double getCost(int itemNumber, int count, double price)
{
	return count*price;
}
double getTax(double sales)
{
	double taxRate = 0.0725;//   define a const for the sales tax rate  - USE a rate of  0.0725
	return sales*taxRate;
}
void printLine(ostream & w, int iNumber, int qty, double price, double cost, bool taxable)
{
	char j;
	if (taxable != true)
	{
		j = '*';
	}
	else
	{
		j = ' ';
	}
	w <<  endl; // print a "*" for the item which is
	w << setw(10) << iNumber << setw(10) << qty << setw(10) << price << setw(10) << cost << j;
										   
}

void printTotal(ostream & w, int loopCount, double grandTotal, double taxDue)
{
	double taxPlusTotal = grandTotal + taxDue;
	w << endl << endl << "Purchase: " << grandTotal << endl << "Tax: " << taxDue << endl << "Grand Total: " << taxPlusTotal << endl << "Number of Items Purchased: " << loopCount << endl;  //DUMMY PRINTER LINE

	w << endl << " * indicates item was not taxable ." << endl;
}

void headings(ostream & w)
{
	w << setw(10) << "item #" << setw(10) << "quantity" << setw(10) << "price"
		<< setw(10) << "cost" << endl << endl;
}

void prepareOutput(ostream & w)
{
	w << setiosflags(ios::showpoint | ios::fixed) << setprecision(2);
}
Hi,

First stop is to compile with a high level of warnings, I used cpp.sh with all 3 warning options on:

28:11: error: '::main' must return 'int' In function 'int main()': 
69:20: warning: suggest parentheses around assignment used as truth value [-Wparentheses] 
At global scope: 
134:21: warning: unused parameter 'num' [-Wunused-parameter] 
159:20: warning: unused parameter 'itemNumber' [-Wunused-parameter]


The first one is pretty obvious.

The second one: = is for assignment, == is for comparison

I haven't looked through every detail of your code, but an easy way to find problems is to use a debugger - hopefully there is a GUI one in your IDE.

If you configure your editor to convert tabs to spaces, your code will display a lot better here. This site converts tabs to 8 spaces, so there is excessive indenting.

You know that cout statements can be built up in stages? You don't have to have the whole thing in one giant statement. Could at least press enter after some of the << , like this:

1
2
3
	w << endl << endl << "Purchase: " << grandTotal << endl << "Tax: " << taxDue 
<< endl << "Grand Total: " << taxPlusTotal << endl 
<< "Number of Items Purchased: " << loopCount << endl;  //DUMMY PRINTER LINE 


The compiler doesn't care about the enter key, it is looking for the semicolon to denote the end of the statement.

Use '\n' rather than endl, endl flushes the bufffer each time, so can be slow. It will make your output statements shorter too:

1
2
3
	w << "\n\nPurchase: " << grandTotal "\nTax: " << taxDue 
<<"\nGrand Total: " << taxPlusTotal 
<< "\nNumber of Items Purchased: " << loopCount << "\n";  //DUMMY PRINTER LINE 


This:
taxableTotal = taxableTotal + cost;

can be written:
taxableTotal += cost;

There are a bunch of other ones too: http://en.cppreference.com/w/cpp/language/expressions#Operators

Good Luck !!
I will look into these changes. When I compile though, I do not get any errors at all.
When I compile though, I do not get any errors at all.


That's because you don't have enough warning options turned on. Which compiler does your IDE use?

With g++ use these as a minimum:

-Wall -Wextra -pedantic-errors

On cpp.sh - the gear icon top right of your code, select all 3 warning options.

http://www.cplusplus.com/forum/general/183731/#msg899203

Warnings are your friend, they tell you where your program is likely to go wrong. Some IDE's have no default warning level at all, which is a disaster IMO.

When you do resolve all the warnings, then it's time to use the debugger, it helps with logical errors and seg faults etc..
Topic archived. No new replies allowed.