Need help with nested for loops!!!

C++ Programming

How do you write a program that displays a tax rate table.

Amount Tax Rate
4.0% 4.5% 5.0% 5.5% 6.0% 6.5% 7.0%
100.00
200.00
300.00
1000.00

Use nested for loops to produce the body of the table.
I am not comprehending an important concept here. someone help please!






#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
double amtA = 100.00, amtB = 200.00, amtC = 300.00, amtD = 1000.00;
double rate;
double interest;

cout<<"Amount \tTax Rate"<<endl;
cout<<"\t___________________________________________________ " << endl;
cout<<"\t4.0% 4.5% 5.0% 5.5% 6.0% 6.5% 7.0%"<<endl;

cout << "100";
for (rate = 4.0; rate <= 7.0; rate += 0.5)
{
interest = rate * amtA / 100;
cout <<"\t" << interest;
}

cout << endl;

cout <<"200";
for (rate = 4.0; rate <= 7.0; rate += 0.5)
{
interest = rate * amtB / 100;
cout <<"\t" << interest;
}
cout << endl;

cout <<"300";
for (rate = 4.0; rate <= 7.0; rate += 0.5)
{
interest = rate * amtC / 100;
cout <<"\t" << interest;
}
cout << endl;

cout << "1000";
for (rate = 4.0; rate <= 7.0; rate += 0.5)
{
interest = rate * amtD / 100;
cout <<"\t" << interest;
}

cout << endl;
system("pause");


return 0;
}




@dantecreed

You could use an array for the amount, and then need only one nested for loop, like so.

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
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;


int main()
{
	double amt[] = {100.00, 200.00, 300.00, 1000.00};
	double rate;
	double interest;
	int x;

	cout<<"Amount \tTax Rate"<<endl;
	cout<<"\t___________________________________________________ " << endl;
	cout<<"\t4.0%\t4.5%\t5.0%\t5.5%\t6.0%\t6.5%\t7.0%"<<endl;
	cout<<"\t___________________________________________________ " << endl;

	for (x=0;x<4;x++)
	{
		cout << amt[x];
		for (rate = 4.0; rate <= 7.0; rate += 0.5)
		{
			interest =  (amt[x] * rate) / 100;  // Added parentheses to better show the math order 
			cout <<"\t" << interest;
		}
		cout<<"\n\t___________________________________________________ " << endl; 
               // Added underlines to make reading of Rate Table, easier
	}

	cout << endl;
	system("pause");

	return 0;
}
Topic archived. No new replies allowed.