for loop problem

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
/*
4-20 for Sales Tax

Re-do the Sales Tax assignment for Week 2 with a for loop 
*/
#include <iostream>
#include <string>
using namespace 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;}

		else if (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.
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
/*
4-20 for Sales Tax

Re-do the Sales Tax assignment for Week 2 with a for loop 
*/
#include <iostream>
#include <string>
using namespace 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;}

		else if (x == "no")
	{cout << "Have a good day with your Non-Taxable goods!"
		<< endl;}
	
		system ("pause");

}


i have editted my code to this now. would
 
amount += price; 


solve the problem?
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

/*
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>
using namespace 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;}

		else if (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;
	
}










Last edited on
It looks good to me. Does it compile and work for you?
Azagaros (326) Mar 6, 2012 at 8:53pm
It looks good to me. Does it compile and work for you?


YAAY! IT WORKS. :DDD THAANKS GUYS.

Subzero03020 (24)

what does cout<<fixed<<setprecision(2); mean?

and
1
2
 cin.sync();
cin.ignore( numeric_limits<streamsize>::max(),'\n' ); 
?
cout << fixed << setprecision(2);
tells cout to display exactly 2 digits after the decimal point. (you're displaying money values, remember)
http://www.cplusplus.com/reference/iostream/manipulators/fixed/
http://www.cplusplus.com/reference/iostream/manipulators/setprecision/

1
2
cin.sync();
cin.ignore(numeric_limits<streamsize>::max(), '\n');

is a better alternative to system("pause");.
Basically, it waits under the user presses the enter key.
Topic archived. No new replies allowed.