c++ optimization

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

c++ optimization

Post by Charles256 »

yes. i do know this code is retarded.i mean, it's a pointless assignment but wanetd to se eif anyone had any optimization tips.if not,no big deal.it works:-D

Code: Select all

// Program4.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
	int bankcharge=10;
	double balance;
	int checks;
	double checkfee;
	int bankfee=0;
	
	// above declares all the variables that will be used. bankfee is set to 0 so that
	// later when we do a check we don't get a compiler warning for using an
	// undefined variable.

	cout << "What is the beginning balance of your bank account?";
	cin >> balance;

	//accepts input from the user.

	if (balance < 0)
	{
		cout << "\nYour account is overdrawn and your bank\n";
		cout << "account has been closed. We apologize for any\n";
		cout << "inconvience this might have caused. Put more money\n";
		cout << "in your account.\n\n";
		return 0;
	}

	// essentially tells the customer that their account is overdrawn and the
	// bank closed their account because of this. It also ends the program
	// there is no point in continuing since they don't have money
	// to be writing checks anyways.

	cout << "How many checks did you write?";
	cin >> checks;
	
	//accepts user input, begin error checking below.
	
	while (checks < 0)
	{
		cout << "\nError: Please enter a positive number:";
		cin >> checks;
	}
	
	cout << "\n"; // simply for formatting reasons.

	// the above loop ensures that the user enters a positive value
	// below begins some checks to determine user check fee.
	
	if (checks < 20)
	{
		checkfee=0.10;
	}
	else if( checks < 40 )
	{
		checkfee=0.08;
	}
	else if ( checks < 60 )
	{
		checkfee=0.06;
	}
	else
	{
		checkfee=0.04;
	}

	balance= balance-bankcharge;
	
	// determines the users current balance. we don't want to charge them
	// the bankfee for account being below 400 after we add in check fees
	// however, it did not say anything about bank charges being applied.

	if (balance < 400 )
	{
		bankfee=15;
	}

	double totalcheckfee=checks*checkfee;

	balance=balance-bankfee-totalcheckfee;

	// calculates the final balance.

	if (bankfee==15)
	{
		cout << "You were charged a $15.00 fee for falling below $400.00 .\n";
	}

	//above code just displays that fee if it was applied, else it's not displayed.

	cout << setprecision(2) << fixed;
	cout << "You were charged $"<< totalcheckfee << " in check fees.\n";
	cout << "You were also charged the standard $10.00 fee for having an account.\n";
	cout << "Your ending balance at the end of"; // new line left off on purpose
	cout << "the month is $"<<balance << ".\n";


	return 0;
}
Post Reply