#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <iomanip>
usingnamespace 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
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:
#include <iostream>
#include <string>
#include <fstream>
usingnamespace 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;
}
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
(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.
#include <iostream>
#include <string>
#include <fstream>
usingnamespace 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
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
_________________________
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.