While loop and adding

I'm trying to get this loop to work and for the numbers to add together for the end. Any help would be appreciated.

[code]
#include <iostream>
#include <iomanip>
using namespace std;


int main()
{

int cnt = 0 ;
int cnt2;
double stax = .07;
double pamnt = 0.0;
double Tt;
double stotal;
double total;

cout << "How many items are you buying? ";
cin >> cnt2;
cout << endl;

for(cnt =0;cnt != cnt2;cnt++)
{cout << "item " << cnt +1 << " cost : ";
cin >> pamnt;
cout << endl;
pamnt += pamnt;

}

Tt = pamnt*stax;
total = pamnt + Tt;
cout << endl;
cout << "subtotal: $" << pamnt << endl;
cout << "Tax : $" <<setprecision(2)<< Tt << endl;
cout << "Total: $" <<setprecision(2)<< total << endl;


system("pause");
return 0;
}
Hi @CleanerWings777,
First of all, I've noticed you write in the loop, "cin>>pamnt" and "pamnt+=pamnt". If I'm not mistaken, that will just double the pamnt's value everytime you input a new number. When you get to another execution of the loop, the number really isn't added anymore. If that was not your intention, then try doing:
1
2
3
4
5
6
7
8
...
double stotal=0.0;
...
for ()
{
    cin>>pamnt;
    stotal+=pamnt;
}

After that you should have:
1
2
3
Tt=pamnt*stax;
total=pamnt+Tt;
...

Hope this helps, and if I didn't get it right, then could you rephrase/comment the code lines so that I could answer better?
I'd do something like this for the output:
1
2
3
4
5
    cout << fixed << setprecision(2);
    
    cout << "subtotal: $" << setw(7) << subtotal << endl;
    cout << "Tax:      $" << setw(7) << tax      << endl;
    cout << "Total:    $" << setw(7) << total    << endl;

(I changed the variable names so I could more clearly understand what they represent).
that is exactly what it needed, thank you so much, i now just need to figure out how to, instead of just setprecision(), to set it to 2 decimal places.

Edit: putting the
cout << fixed << setprecision(2);
before the outputs did the trick thank y'all for the help.
Last edited on
Topic archived. No new replies allowed.