Any Better ideas???

This is a discussion on Any Better ideas??? within the C++ Programming forums, part of the General Programming Boards category; This is my third program was just wondering if anybody had any better ideas???? seems to work for now...... was ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    29

    Any Better ideas???

    This is my third program was just wondering if anybody had any better ideas???? seems to work for now...... was just wondering hmm

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main() /*function main*/
    {
    
    float bal = 0;
    int month = 0;
    float dep = 0;
    float wit = 0;
    float interest = 0;  
    float rate = 0;
    float totalinterest =0;
    float mi = 0;
    float twit =0;
    float tdep = 0;
    
    
    	cout <<"Please enter the your balance   $: ";
    	cin >> bal;
    
    	cout <<"Please enter the your interest rate   $: ";
    	cin >> rate;
    
    	cout <<"How many months has the account been opened?  :";
    	cin >> month;
    	
    
    			rate = rate/12; 
    			
    			
    
    
    	while (month >= 1)////start loop
    	{
    		cout <<"Please enter total deposits per month  $: ";
    		cin >> dep;
    		
    		if (dep < 0)
    		{	cout << "Error start over";
    			break;
    		}
    		
    		cout <<"Please enter total withdrawals per month  $: ";
    		cin >> wit;
    		if (wit < 0)
    		{	cout << "Error start over";
    			break;
    		}
    			twit += wit;
    			tdep += dep;
    			bal += dep;
    			bal -= wit;
    			mi = (rate/100) * bal;
    			totalinterest += mi;
    			bal += mi;
    			month--;
    			
    		
    			if (bal < 0)
    			{	cout <<" Account Closed see the Bank Manager";
    				break;
    			}
    	}
    
    		
    	
    	cout.setf(ios::showpoint);
    	cout.setf(ios::fixed);
    	cout.precision(2);	
    	
    	cout <<'\n';
    	cout << "The balance is " << bal << endl; //print output
        cout << "The total deposits are: " << tdep << endl;
        cout << "The total withdrawals are: " << twit << endl;
        cout << "The total interest earned is : " << totalinterest << endl;
    	return 0;
    }

    thanks for any input

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    rate = rate/12;
    Consider using the following.

    Code:
    rate /= 12;
    That's the only thing I see, though I did do just a quick read-through. Also check out your colon on the 3rd outpot statement.

  3. #3
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    Even though this is a very trivial program, and the code simple enough to follow with ease, you should get into the habit of commenting your code. If you don't do now, it will hunt you later...

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Ditto. Up until very recently, I never commented, because I was sure I did need it, and no one else would read my code. Well... you run into problems with both of those ideas.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,500
    > and the code simple enough to follow with ease
    Not that easy - the indentation is horrible.

    Comments:
    float mi = 0;
    float twit =0;
    float tdep = 0;
    How about using actual words rather than random abbreviations, because twit means something completely different.

    Say
    float monthly_interest;
    float total_withdrawn;
    float total_deposits;

    > int main() /*function main*/
    You have only 3 comments, and all state the obvious to anyone who is at all familiar with the language. As such, they're just useless clutter.
    Sometimes tutors insist on them in the belief that it tells them if you understand the language.

    For me, I'd put all the totals etc into a class called account, with a couple of functions to say
    - update with dep and wit
    - print the stats
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ideas, how do you get them? xD
    By Akkernight in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 01-22-2009, 02:53 AM
  2. cool ideas for a game
    By Shadow12345 in forum Game Programming
    Replies: 7
    Last Post: 05-18-2004, 08:37 PM
  3. idea's idea's idea's
    By mithrandir in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 04-29-2002, 12:30 AM
  4. Small app ideas
    By dirkduck in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 02-15-2002, 07:57 PM

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