Thread: Simple money question

  1. #1
    Banned
    Join Date
    Oct 2004
    Posts
    15

    Simple money question

    right now, I am working on a problem that requires us to compare 2 different account types at a fictional bank. We have to input a value, and make calculations to show the interest rates and how much you gain/lose at the end of the year by using that type of account, etc.

    Right now, I don't have very much of this problem done. There is one simple problem that I need to address. How can I make C++ show the correct number of decimals? For instance...if I enter "250" as a value, it prints "250". I want it to show all values, even if they are zero, as in "250.00".

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
    	/*variables: avg daily balance; yearly account cost,
    	income year (avg.daily * interest%) profit/loss (cost/year - income)*/
    	float avg;
    	int prft;
    	int nvcost = 0;
    	int svcost = 12;
    	double nvincomeyr = 0;
    	double nvinterest = 0;
    	double svinterest = 0;
    	nvinterest = 0.01;
    	svinterest = 0.015;
    	prft = nvcost - nvincomeyr;
    	std::cout << "Enter Average Daily Balance \n";
    	std::cin >> avg;
    	nvincomeyr = avg * nvinterest;
    	std::cout << "account \t avg daily \t cost/year \t income/year \t profit/loss\n type\t\t  balance\n";
    	std::cout << "Netvalue \t    " << avg << "        \t     " << nvcost << "        \t      " << nvincomeyr << "        \t " << prft << "\n"; 
    	return (0);
    }
    (Ignore all of my stupid and confusing comments, and my ill-chosen variable names. )

    Also, is there a better way to make a "table" in code? If you see what I've done, I have a delicate balnce of \t's and spaces betweem my categories. Is there a better way to categorize data? In other words, i want it to look something like...

    Code:
    account type    |  avg daily balance  | cost/year  | income/year | profit/loss
    --------------------------------------------------------------------------------
    NetValue               250.00               0.00         2.50         2.50
    SuperValue             250.00             108.00         3.75      (104.25)
    ^This.


    Thank you very much in advance.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >How can I make C++ show the correct number of decimals?
    Use setprecision().
    Code:
    #include <iomanip>
    .
    .
    std::cout << "Netvalue \t    " << fixed << setprecision(2) << avg << "        \t     " << nvcost << "        \t      " << nvincomeyr << "        \t " << prft << "\n";
    >Also, is there a better way to make a "table" in code?
    You can use setw() to line up your fields.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  2. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  3. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM