help with decimal place printing

This is a discussion on help with decimal place printing within the C++ Programming forums, part of the General Programming Boards category; I have written code to print out 2 decimal places, however if the numbers in the 2 places are zero ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    38

    Exclamation help with decimal place printing

    I have written code to print out 2 decimal places, however if the numbers in the 2 places are zero they do not print
    ie $10.00 prints $10

    is there a code that is standard that can instist on the zeroes being printed

    here is what i have
    Code:
     cout.setf(ios::fixed);
          cout.setf(ios::showpoint);
          cout.precision(2);
    how would you suggest i change that to print the money decimals even if they are zero

    thanks for all the great help, you guys have been great

    jessie

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    What are you couting? Try checking if (num*100)%10 == 0, if it does (I think), the number has two trailing decimal places. If (num*100)%100 == 0, it has one.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    38
    i am couting bank account balances.
    but if the account has only $10.00 it couts $10 instead of $10.00

    I am trying to figure out a way to make it print the .00 place holders

  4. #4
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Do as I said and if the first one is true cout .00, if the second one is true cout 0. I'm not sure if this is correct as I can't compile it now, but it should work.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,339
    The original code works for me. Can you show your full program so we can test it?

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Wait... you're using a floating point to represent currency? No bank in the world would do that. Floats can't represent numbers like .5 correctly no matter how precise they are; its a fundemental limitation. It's a damn lot better to use a scaled integer: smallest two decimal places for cents values, higher places for dollar values.


    As for the original problem, you should make sure the manipulators are still in effect when you print the money values. Try inlining the manipulators

    Code:
    std::cout << std::setprecision(2) << "foo";
    and see if it works.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    38
    I am not sure what you are trying to tell me to do with the formula you gave me (where to put it)

    here is the program
    menu selection 5 does not print the .00, it will print 25.43 just fine........
    anyway any insight is greatly appreciated.

    Code:
    /*
      Assignment 5
    
      Jessie Brown
      COP 2334
      11.10.06
    
      Project was created in Borland IDE. This project demonstrates the use of arrays,
      functions, sorting, and searching.
    
    
    */
    
      #include <iostream>
      using namespace std;
    
      //Prototypes -- function declarations.
      //-----------------------------------
    
      void Menu_Selection();
      void enter_accounts (int[],double[],int,int& total);
      void print_all_accounts(int[],double[],int& total);
      void total_bank_funds(double[], int&);
      void DepositFunction(int,int[],double[], int& total);
      void WithdrawFunction(int,int[],double[], int& total);
      void DeleteAccount(int,int[],double [],int& total);
      int find_account(int[],int&,int);
      void sort_accounts (int [], double[], int);
    
    
    
    
    void main()
    {
    
          Menu_Selection();
    
    
          cout.setf(ios::fixed);
          cout.setf(ios::showpoint);
          cout.precision(2);
    
      cout << "\nPress <ENTER> to continue.\n";
      fflush(stdin);
      cin.get();
    }
    /*------------------------------------------------------------------------------
            This section of code is the initial screen after employee signs in
                    to system.  From here said employee can choose what she must
                    do with each customer being assisted.
    ------------------------------------------------------------------------------*/
    
    
      void Menu_Selection()
    {
    
         const int NUMBER = 10;
         int account[NUMBER], total=0;
         double balance[NUMBER];
         int option;
    
    
    
         do
         {
              cout << endl
                   << "1. Add a distinct account number: \n \n"
                   << "2. Remove an existing account number: \n \n"
                   << "3. Deposit Funds into distinct account: \n \n"
                   << "4. Withdraw Funds from distinct account: \n \n"
                   << "5. View all Customer accounts and Balance: \n \n"
                   << "6. Total Funds available from All accounts: \n \n"
                   << "7. Exit program: \n \n";
              cin >> option;
              cout << endl;
              
              switch (option)
               {
                    case 1:
                         enter_accounts (account,balance,NUMBER,total);
                         cout << endl;
                         break;
                     case 2:
                         DeleteAccount(NUMBER,account,balance,total);
                        //code to display deletion of an existing account.
                         break;
                    case 3:
                        DepositFunction(NUMBER,account,balance,total);
                        //code to display deposit entry.
                         break;
                    case 4:
                         WithdrawFunction(NUMBER,account,balance,total);
                         //code to display withdrawel deduction.
                         break;
                    case 5:
                         print_all_accounts(account,balance,total);
                         cout << endl;
                         break;
                    case 6:
                         total_bank_funds(balance, total);
                         //code to display grand total of all available accounts.
                         break;
                    case 7:
                        cout << "End of Program.\n";
                        break;
                    default:
                    cout << "Not a valid option. Please choose a valid option. \n\n";
               }
         }while (option != 7);
    
    
    }
    
    
    
    /*------------------------------------------------------------------------------
     This function allows a user to add an account with its beginning balance.
            accounts are to be sorted and no to account numbers are to be the same
    ------------------------------------------------------------------------------*/
    void enter_accounts (int account[],double balance[],const int NUMBER, int& total)
    {
     if(total >= NUMBER)
           {
            cout <<"Maximum account capacity has been reached.\n"
                 <<"Please delete an account to continue\n";
            }
          else if(total < NUMBER)
            {
             cout <<"Please enter new account number.\n";
             cin >> account[total];
              for (int i =0; i < total; i++)
                    {
                    do
                    {
                     if (account[total]==account[i])
                           {
                            cout << "Account number must be original.\n"
                                 << "Please enter a new account number.\n";
                            cin >> account[total];
                           }
                     }while (account[total] == account[i]);
                     }
             cout << "Please enter opening balance ($10.00 minimum)\n";
             cin >> balance[total];
             do
             {
              if (balance[total] < 10)
                   {
                    cout << "Minimum deposit must be $10.00\n";
                    cin >> balance[total];
                   }
              } while (balance[total] < 10);
             sort_accounts (account,balance,total);
    
             total++;
             }
    
    }
    /*------------------------------------------------------------------------------
    This function prints all acounts listed in the "bank array" as well as thier
    corresponding balances.
    ------------------------------------------------------------------------------*/
    
    
    void print_all_accounts(int account[],double balance[],int& total)
     {
      for( int i = 0; i < total ; i++ )
              {
                    cout << "Account number " << account[i];
                    cout << " Account balance is $" << balance[i];
                    cout << endl;
    
    
    
              }
     }
    
    
    
    /*------------------------------------------------------------------------------
    This function prints the total balance of all funds added together.
    ------------------------------------------------------------------------------*/
    void total_bank_funds(double balance[], int& total)
    {
        double funds = 0;
        for (int index = 0; index < total; index++)
            funds = funds + balance[index];
        if (funds > 0)
        {
            cout << "The total investments funds available for this bank is $" << funds;
            cout << endl;
        }
        else if (funds <= 0)
        {
            cout << "This bank needs a new CEO!\n";
            cout << endl;
        }
    }
    
    
    /*------------------------------------------------------------------------------
    This function searches an account number then adds a deposit to this account
    ------------------------------------------------------------------------------*/
    
     void DepositFunction(int NUMBER, int account[],double balance[], int& total)
     {
       double deposit;
       int target, result;
       cout << "Enter a number to search for: ";
       cin >> target;
       cout << "Please enter the amount you wish to deposit\n";
       cin >> deposit;
       result = find_account(account, total, target);
            if (result == -1)
                {
                cout << target << " is not on the list.\n";
                }
            else
                balance[result] = balance[result]+deposit;
                cout << "Your new balance for       " << account[result]
                     << " is         " << balance[result];
    
     }
    
    
    
    /*------------------------------------------------------------------------------
    This function finds an account number called and returns the target array or
    location of this particular account variable to functions for deposit to account,
    withdraw from account and delete account.
    ------------------------------------------------------------------------------*/
    
     int find_account(int account[],int& total,int target)
     {
    
        int index = 0;
        bool found = false;
        while ((!found) && (index < total))
            if (target == account[index])
                found = true;
            else
                index++;
    
        if (found)
            return index;
        else
            return -1;
    }
    
    /*------------------------------------------------------------------------------
    This function makes a withdrawal from a specific account, if the account will go
    into the red withdrawal will not be made.
    ------------------------------------------------------------------------------*/
    
     void WithdrawFunction(int NUMBER, int account[],double balance[], int& total)
     {
       double withdrawal;
       int target, result;
       cout << "Enter a number to search for: ";
       cin >> target;
       cout << "Please enter the amount you wish to withdraw.\n";
       cin >> withdrawal;
       result = find_account(account, total, target);
            if (result == -1)
                {
                cout << target << " is not on the list.\n";
                }
            else
                if (withdrawal > balance[result])
                   {
                    cout << "You do not have enough funds in this account to make\n"
                         << "a withdrawal in this amount!\n";
                   }
                else
                {
                balance[result] = balance[result]-withdrawal;
                cout << "Your new balance for " << account[result]
                     << " is $" << balance[result];
                }
    
     }
    
     
    /*------------------------------------------------------------------------------
    This Function removes a specific account and then moves all accounts up!
    ------------------------------------------------------------------------------*/
    
     void DeleteAccount(int NUMBER, int account[],double balance[],int& total)
     {
       int target, result;
       cout << "Enter a number to search for: ";
       cin >> target;
    
       result = find_account(account, total, target);
          if (result == -1)
                {
                cout << target << " is not on the list.\n";
                }
         else if(result >-1)
          {
            while(result < total)
            {
            account[result] = account[result+1];
            balance[result] = balance [result+1];
            result++;
            }
          }
            total--;
    
    
     }
    
    /*------------------------------------------------------------------------------
    This function sorts the accounts, it is done each time a new account it added.
    Because when an account is deleted and remaining accounts are only moved up,
    there is no need to sort them at the time of deletion
    ------------------------------------------------------------------------------*/
    
    void sort_accounts (int account[],double balance[],int total )
    {
      int i,hold;
      double hold2;
    
      for (int pass =0; pass <= total; pass++)
          for (i=0; i==total-1;i++)
               if (account[i] > account[i+1])
                 {
                   hold =account[i];
                   hold2 = balance[i];
                   account[i]=account[i+1];
                   balance[i] = balance[i+1];
                   account[i+1] = hold;
                   balance[i+1] = hold2;
                  }
    
    }

  8. #8
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    What I am trying to say:
    Code:
    if ((num*100)%10 == 0)
         cout << num << ".00" << endl;
    else if ((num*100)%100 == 0)
         cout << num << "0" << endl;
    else 
         cout << num << endl
    Tell me if this works.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,339
    Your problem is that your code that changes the output is after all the code that does the work in the program. You need to place that stuff before Menu_Selection() is called.

    There's no need to use manutd's code, just put your own in the right place.

  10. #10
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Yes, your/Dave_Sinkula's solution is better than mine, as mine is rather hackish.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  11. #11
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Code:
    fflush(stdin);
    Read the FAQ for why this is bad.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Dave_Sinkula's solution is better than mine
    My eyes must be going ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. generic printing preferences dialog box
    By stanlvw in forum Windows Programming
    Replies: 8
    Last Post: 06-27-2008, 02:20 AM
  2. I need help with decimal to binary Algorithm
    By webznz in forum C Programming
    Replies: 4
    Last Post: 03-13-2008, 03:52 AM
  3. printing data to a file
    By coralreef in forum C Programming
    Replies: 3
    Last Post: 11-02-2006, 07:10 PM
  4. need help relating printing?
    By omarlodhi in forum Linux Programming
    Replies: 0
    Last Post: 03-03-2006, 03:46 AM
  5. Replies: 29
    Last Post: 10-25-2005, 09:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21