calculates sales tax

ok so I was able to read the file but stuck on calculating the sales tax by tax by total and tax by item

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
  #include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;
int main()
{
  // double TaxTot=0;
  //double TaxItem=0;
  string filename="input.dat";
  ifstream inFile;
  string line;
  inFile.open (filename.c_str());
  if (inFile.fail())
  {
    cout<<" the file was not successfully opened"
        <<"please check that the file currently exists."
        <<endl;
    exit(1);
  }
 
  while (getline(inFile,line))
    cout << line<< endl;

  inFile.close();

  //cout<< "Welcome to target"<<endl;

  return 0;
}


this is what I get if I were to run it reads it from the file
3.5 (its the sales tax percent)
Movie 20.00
soda 2.25
control 5.00
and I know im suppose to use nested loops for getting the two receipts but stuck on how to start
Last edited on
What does the input.dat file look like if it has two receipts? I think it is impossible to answer your question without knowing how the receipts are separated in the data file.

First off, you are reading the whole line using getline - that won't work unless you were to write some code to parse the whole line extracting the price from it. Read in the individual items on that line instead.

inFile >> item;
inFile >> price;

Remember that the price will be ascii, therefore before you can run any calculations on it you will need to convert it to a double. check out stod: http://www.cplusplus.com/reference/string/stod/ (assuming you have a c++ 11 compiler otherwise see atof)

Just to give you an idea I quickly wrote some changes to your code:

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

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

int main()
{

	ifstream inFile;
	string item;
	double price;
	double net = 0;

	inFile.open("input.dat");
	if (inFile.fail())
		cout << " the file was not successfully opened, "
		<< "please check that the file currently exists."
		<< endl;
	else
	{
		// we dont know how many items so just set up a never-ending loop
		// and check for the end of file to break out.

		while (1) {
			// read first/next item
			inFile >> item;
			// its possible we have read in end of file,
			// check for it and break.
			if (inFile.eof())	break;
			// no, not end of file, grab the price 
			inFile >> price;
			// add price to "net".
			net += price;

			// display the item contents.
			cout << "Product: [" << item << "]    Price: $" << price << endl;
		}

		// net holds total of prices. lets for example sake show the total nett
		cout.precision(2);
		cout << fixed << "Net Value:  $" << net << endl;

		// finally close the file
		inFile.close();

	}

	return 0;
}

Product: [Item1]    Price: $23.99
Product: [Item2]    Price: $10.30
Product: [Item3]    Price: $12.43
Net Value:  $46.72




Last edited on
well this is what I have in my input.dat file
3.5
Movie 20.00
soda 2.25
control 5.00
and this is an example of how my code should run but im having trouble understanding how to start it it would be helpful if you can guide me on how to start it

Welcome to MY STORE
Tax-by-total
*******************
Socks $ 15.00
Book $ 2.15
Game $ 5.05
Sub-total $ 22.20
Sales Tax $ 1.67
_______
Total $ 23.87
*******************
Thank you.


Welcome to MY STORE
Tax-by-item
*******************
Item 1 $ 15.00
Sales Tax $ 1.13
Item 2 $ 2.15
Sales Tax $ .16
Item 3 $ 5.05
Sales Tax $ .38
_______
Total $ 23.87
When I am unsure how to get a program started, I try to answer the following questions in order:

1. What does the program need to output?
2. What data is needed to generate this output?
3. How can the output be calculated from the given data?

So, I start by writing the code for the output first. For any unknown values required in the output, I use variables.

My calculations tax @ 3.5% on 22.20 is 0.777 i.e:

(22.20 / 100) * 3.5 = 0.777 (rounded up in the UK would be 0.78) therefore total would be $22.98 and not $23.87, unless I am completely missing something here.

Anyway, I am not going to write the whole thing for you but only a snippet from it - hopefully this will give you a point in the right direction and you will learn from it.. the rest is up to 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

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
	ifstream inFile;
	string item;		// for file read

	// calculations
	double taxrate;		// tax rate, i.e. 3.5%
	double taxtotal = 0;	// holds total tax
	double net;		// holds net read from file
	double subtotal = 0;	// holds total net
	double grosstotal;	// holds gross subtotal+tax

	// set 2 decimal places
	cout.precision(2);

	// open file and check if we managed ok
	inFile.open("input.dat");
	if (inFile.fail())
		cout << " the file was not successfully opened, "
		<< "please check that the file currently exists."
		<< endl;
	else
	{
		// we opened, so grab the tax rate - as per your
		// post the first entry is the tax rate at 3.5
		inFile >> taxrate;

		// show heading.
		cout << "Welcome to MY STORE" << endl;
		cout << "Tax-by-total" << endl;
		cout << "*******************" << endl;

		// we dont know how many items so we will just loop
		// until we find the end of file.
		while (true) {
			// read the first/next item description
			inFile >> item;
			// did we get a end of file, if so we need to 
			// break out of the loop.
			if (inFile.eof()) break;
			// no, so carry on and  get the price
			inFile >> net;			// read
			subtotal += net;		// add to subtotal
			// display item
			cout << fixed << item << " $ " << net << endl;
		}

		// calc the final summary
		taxtotal = (subtotal / 100)*taxrate;
		grosstotal = subtotal + taxtotal;

		// show it
		cout << fixed << "Sub-Total $ " << subtotal << endl;
		cout << fixed << "Sales Tax $ " << taxtotal << endl;
		cout << "___________" << endl;
		cout << fixed << "Total $ " << grosstotal << endl;

		// finally close the file
		inFile.close();

	}
	return 0;
}

Welcome to MY STORE
Tax-by-total
*******************
Socks $ 15.00
Book $ 2.15
Game $ 5.05
Sub-Total $ 22.20
Sales Tax $ 0.78
___________
Total $ 22.98


Last edited on
ok well I got it to work but my sales tax came out a bit weird well I don't know if its right or wrong please help
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
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;
void taxByTotal()
{
  double movie=0.0;
  double soda=0.0;
  double control=0.0;
  double tax=0.0;
  double total=0.0;
  double totalTax=0.0;

  string movieA="";
  string sodaB="";
  string controlC="";
  /**
     cout<< "This program will calculate the pice of the 3 items and the t\
ax involved.\n It's going to display the total price and the total tax com\
bined."<<endl;**/
  ifstream fin;
  fin.open("input.dat");

  if(fin.fail())
    {
      cout<<"file failed to open."<<endl;
      return;
    }
  fin>>tax>>movieA>> movie>> sodaB>>soda>>controlC>>control;
  fin.close();

  cout<<setw(10)<<"Welcome to store goodies:"<<endl;
  cout<<setw(10)<<"Tax-by-total"<<endl;
  cout<<"****************************"<<endl;
  cout<<fixed<<movieA<< setw(12)<<setprecision(2)<<"$"<<movie<<endl;
  cout<<fixed<<sodaB<< setw(13)<<setprecision(2)<<"$"<<soda<<endl;
  cout<<fixed<<controlC<< setw(10)<<setprecision(2)<<"$"<<control<<endl;

  total=movie+soda+control;
  cout<<fixed<<"\nSub-total"<<setprecision(2)<<setw(8)<<"$"<<total<<endl;
 tax=tax * .01;
  totalTax=(movie*tax)+(soda*tax)+(control*tax);
  cout<<fixed<<"Sales-Tax"<<setprecision(2)<<setw(8)
<<"$"<<totalTax<<endl;
  cout<<"________________________"<<endl;
  total=total+totalTax;
  cout<<fixed<<"Total"<<setprecision(2)<<setw(12)<<"$"<<total<<endl;
  cout<<"*****************************"<<endl;
  cout<<"Thank you for shopping at store goodies"<<endl;

return;

}

void taxbyItem()
{
  double movie=0.0;
  double soda=0.0;
  double control=0.0;
  double tax=0.0;
  double total=0.0;

string movieA="";
  string sodaB="";
  string controlC="";

  /** cout<< "This program will calculate the pice of the 3 items and the \
tax \
involved.\n It's going to display the total price and the total tax combin\
\
ed."<<endl;**/
  ifstream fin;
  fin.open("input.dat");

  if(fin.fail())
    {
      cout<<"file failed to open."<<endl;
      return;
    }
  fin>>tax>>movieA>> movie>> sodaB>>soda>>controlC>>control;
  fin.close();

  cout<<setw(10)<<"Welcome to store goodies:"<<endl;
  cout<<setw(10)<<"Tax-by-item"<<endl;
  cout<<"****************************"<<endl;

  cout<<fixed<< movieA<<setprecision(2)<<setw(12)<<"$"<<movie<<endl;
  cout<<fixed<<"Sales-Tax"<<setprecision(2)<<setw(8)<<"$"<<movie*tax<<endl\
;

  cout<<fixed<<sodaB<<setprecision(2)<<setw(13)<<"$"<<soda<<endl;
  cout<<fixed<<"Sales-Tax"<<setprecision(2)<<setw(8)<<"$"<<soda*tax<<endl;

  cout<<fixed<<controlC<<setprecision(2)<<setw(10)<<"$"<<control<<endl;
  cout<<fixed<<"Sales-Tax"<<setprecision(2)<<setw(8)<<"$"<<control*tax<<en\
dl;
  cout<<"_________________________"<<endl;
  cout<<fixed<<"\nTotal"<<setprecision(2)<<setw(12)<<"$"<<(movie*tax)+(sod\
a*tax)+(control*tax)<<endl;
  return;
}
int main()
{
  taxByTotal();
  taxbyItem();

  return 0;
}

Last edited on
and this is what I get when I run the program as you can see the sales tax in tax-by-item don't seem right to me but that's just me
Welcome to store goodies:
Tax-by-total
****************************
Movie $20.00
soda $2.25
control $5.00

Sub-total $27.25
Sales-Tax $0.95
________________________
Total $28.20
*****************************
Thank you for shopping at store goodies
Welcome to store goodies:
Tax-by-item
****************************
Movie $20.00
Sales-Tax $70.00
soda $2.25
Sales-Tax $7.88
control $5.00
Sales-Tax $17.50
_________________________

Total $95.38

What you need to be doing is take the price, divide it by 100 and multiply by the tax rate which will give you the amount of tax payable on that item.

For example, Movie is $20.00 so.. 20.00 / 100 is 0.2, then multiply by the tax rate giving you 0.70 - so on $20.00 you would be paying $0.70 tax. i.e. (20.00 / 100)*3.5.
Topic archived. No new replies allowed.