/*
4-20 for Sales Tax
Re-do the Sales Tax assignment for Week 2 with a for loop
*/
#include <iostream>
#include <string>
usingnamespace std;
int main ()
{
string x;
double items;
cout << "Hi! Welcome to my Sales Tax Calculator!\n";
cout << "The tax will be 9.75% of the total cost!\n";
cout << "Please tell me if it is taxable or not first by saying either yes or no.\n";
cin >> x;
if ( x == "yes" )
{ cout << "How many items do you have: \n";
cin >> items;
double amount, total, tax;
for (int n = 1; n <= items; n++)
{
cout << "Please enter how much you paid here and I will tell you the tax amount and the total including the tax: \n";
cin >> amount;
amount += amount;
}
tax = (amount* .0975);
cout << "Your tax is $"
<< tax
<< "."
<< endl;
total = tax+ amount;
cout << "Your total is $"
<< total
<< "."
<< endl;}
elseif (x == "no")
{cout << "Have a good day with your Non-Taxable goods!"
<< endl;}
system ("pause");
}
heey, so my problem is i'm trying to use the for loop to add up the total amount before taking the tax, but what this is actually doing is doing the tax for each amount and then adding themup. can someone help me?
Why is "items" a double?
You can't have 5.342 items, can you? :)
Oh, and
1 2 3 4 5 6
for (int n = 1; n <= items; n++)
{
cout << "Please enter how much you paid here and I will tell you the tax amount and the total including the tax: \n";
cin >> amount;
amount += amount;
}
surely can't be what you wanted it to be...
You also didn't initialize "amount" to 0 when you declared it.
To add up the total amount, you need another variable (not "amount") to store the input.
Then you add that to "amount".
Otherwise, by writing
1 2
cin >> amount;
amount += amount;
you're just going to overwrite "amount" every time.
/*
4-20 for Sales Tax
Re-do the Sales Tax assignment for Week 2 with a for loop
*/
#include <iostream>
#include <string>
usingnamespace std;
int main ()
{
string x;
int items;
cout << "Hi! Welcome to my Sales Tax Calculator!\n";
cout << "The tax will be 9.75% of the total cost!\n";
cout << "Please tell me if it is taxable or not first by saying either yes or no.\n";
cin >> x;
if ( x == "yes" )
{ cout << "How many items do you have: \n";
cin >> items;
double amount = 0;
double total, tax;
for (int n = 1; n <= items; n++)
{
double price = 0;
cout << "Please enter how much you paid here and I will tell you the tax amount and the total including the tax: \n";
cin >> price;
amount += price;
}
tax = (amount* .0975);
cout << "Your tax is $"
<< tax
<< "."
<< endl;
total = tax+ amount;
cout << "Your total is $"
<< total
<< "."
<< endl;}
elseif (x == "no")
{cout << "Have a good day with your Non-Taxable goods!"
<< endl;}
system ("pause");
}
/*
4-20 for Sales Tax
Re-do the Sales Tax assignment for Week 2 with a for loop
*/
#include <iostream>
#include <string>
#include <iomanip>
#include <limits>
usingnamespace std;
int main ()
{
string x;
int items;
cout << "Hi! Welcome to my Sales Tax Calculator!\n";
cout << "The tax will be 9.75% of the total cost!\n";
cout << "Please tell me if it is taxable or not first by saying either yes or no.\n";
cin >> x;
cout<<fixed<<setprecision(2);
if ( x == "yes" )
{ cout << "How many items do you have: \n";
cin >> items;
double amount=0, total=0, tax=0,temp=0;
for (int n = 0; n < items; n++)
{
cout << "Please enter how much you paid here and I will tell you the tax amount and the total including the tax: \n";
cin >> temp;
amount += temp;
}
tax = (amount* .0975);
cout << "Your tax is $"
<< tax
<< "."
<< endl;
total = tax+ amount;
cout << "Your total is $"
<< total
<< "."
<< endl;}
elseif (x == "no")
{cout << "Have a good day with your Non-Taxable goods!"
<< endl;}
cout<<"Press Enter Key To Exit"<<endl;
cin.clear();
cin.sync();
cin.ignore( numeric_limits<streamsize>::max(),'\n' );
return 0;
}