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)

Recommended Answers

All 4 Replies

your test() is doing too much work! The solution is much simpler.

void test()
{
    double x;
    double y;
    double z;

#if 0
    cout << "Enter price 1:";
    cin >> x;
    cout << "Enter price 2:";
    cin >> y;
    cout << "Enter price 3:";
    cin >> z;
#endif
    x = 5.0;
    y = 50.0;
    z = 500.0;
    cout << fixed << setprecision(2);
    cout  << "$" << setw(20) << right << x << endl;
    cout  << "$" << setw(20) << right << y << endl;
    cout  << "$" << setw(20) << right << z << endl;
}

The result of that code is this:

$                5.00
$               50.00
$              500.00
Do you wish to repeat the function? (Y/N)

Thats exactly what I could already do, my problem is getting the decimal and trailing zero's to line up while keeping the '$' next to the

ex:

        $5.00
       $50.00
      $500.00

All that extra work my function is doing is to determine how many digits are in the number left of the decimal. I also need to keep it simple, or sorta primitive, I can only use stuff I've learned in class, "#if 0" "#endif" is nothing that we have used.

I am fully aware that line 71 and 72 have space1, even though line 71 should have space2, and 72 should have space3... I corrected the code on eclipse and I'm still getting the same result.
lines 70-72 should read:

    cout << setw(20 - (space1 + 3)) << right << "$" << x << endl;
    cout << setw(20 - (space2 + 3)) << right << "$" << y << endl;
    cout << setw(20 - (space3 + 3)) << right << "$" << z << endl;

still doesn't fix my problem.

Figured it out myself... made a really dumb mistake... 2 things needed to be fixed...
1. the relationship expression... cannot be somenumber < x < somenumber...
it has to be x > some number && x < some number.
2. I had to put the if statements after there has been some sort of input, otherwise its just going to edit a nonexisting number..

For future reference if anyone has a similar problem... this is the fixed code... sorry if its a bit messy. Keep in mind, this program was made so I could figure out a problem with my actual project program, so all it does is this one feature.

#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 = 23;
    int space2 = 23;
    int space3 = 23;

    cout << "Enter price 1:";
    cin >> x;
    cout << "Enter price 2:";
    cin >> y;
    cout << "Enter price 3:";
    cin >> z;
    //Space for X
    if(x > 0 && x < 10)
        space1 -= 1;
    else if(x >= 10 && x < 100)
        space1 -= 2;
    else if(x >= 100 && x < 1000)
        space1 -= 3;
    else if(x >= 1000 && x < 10000)
        space1 -= 4;

    //Space for Y
    if(y > 0 && y < 10)
        space2 -= 1;
    else if(y >= 10 && y < 100)
        space2 -= 2;
    else if(y >= 100 && y < 1000)
        space2 -= 3;
    else if(y >= 1000 && y < 10000)
        space2 -= 4;

    //Space for Z
    if(z > 0 && z < 10)
        space3 -= 1;
    else if(z >= 10 && z < 100)
        space3 -= 2;
    else if(z >= 100 && z < 1000)
        space3 -= 3;
    else if(z >= 1000 && z < 10000)
        space3 -= 4;

    cout << setprecision(2) << fixed << showpoint;
    cout << space1 << endl << space2 << endl << space3 << endl;
    cout << setw(space1) << right << "$" << x << endl;
    cout << setw(space2) << right << "$" << y << endl;
    cout << setw(space3) << right << "$" << z << endl;
}

This is the result:

Enter price 1:5
Enter price 2:50
Enter price 3:500
22
21
20
                     $5.00
                    $50.00
                   $500.00
Do you wish to repeat the function? (Y/N)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.