I made a test program to mess around with a feature I'm trying to put into my class project but I can't seem to figure out what I'm doing wrong. What I'm trying to do is to line up the numbers right of the decimal.
this is my test source file:
#include<iostream>
#include<iomanip>
using namespace std;
void test();
int main()
{
//MAIN FUNCTION TO REPEAT TEST FUNCTION
char quit = 'N';
do
{
test();
cout << "Do you wish to repeat the function? (Y/N)";
cin >> quit;
while(quit !='Y' && quit !='y' && quit !='N' && quit !='n')
{
cout << "Do you wish to repeat the function?" << endl;
cout << "Enter Y for yes, and N for no:";
cin >> quit;
}
}while(quit != 'N' && quit != 'n');
}
void test()
{
double x;
double y;
double z;
int space1 = 0;
if(0 < x < 10)
space1 = 1;
else if(10 <= x < 100)
space1 = 2;
else if(100 <= x < 1000)
space1 = 3;
else if(1000 <= x < 10000)
space1 = 4;
int space2 = 0;
if(0 < y < 10)
space2 = 1;
else if(10 <= y < 100)
space2 = 2;
else if(100 <= y < 1000)
space2 = 3;
else if(1000 <= y < 10000)
space2 = 4;
int space3 = 0;
if(0 < z < 10)
space3 = 1;
else if(10 <= z < 100)
space3 = 2;
else if(100 <= z < 1000)
space3 = 3;
else if(1000 <= z < 10000)
space3 = 4;
cout << "Enter price 1:";
cin >> x;
cout << "Enter price 2:";
cin >> y;
cout << "Enter price 3:";
cin >> z;
cout << setprecision(2) << fixed << showpoint;
cout << setw(20 - (space1 + 3)) << right << "$" << x << endl;
cout << setw(20 - (space1 + 3)) << right << "$" << y << endl;
cout << setw(20 - (space1 + 3)) << right << "$" << z << endl;
}
Now the problem I'm facing is that when I type in the three numbers and get a result, all the '$' signs line up, but the '$' isn't what I want to line up, what I want to line up is the two numbers right of the decimal.
This is what I'm getting with this code:
Enter price 1:5
Enter price 2:50
Enter price 3:500
$5.00
$50.00
$500.00
Do you wish to repeat the function? (Y/N)y
Enter price 1:500
Enter price 2:50
Enter price 3:5
$500.00
$50.00
$5.00
Do you wish to repeat the function? (Y/N)