Live Chat & Podcast at 1:00PM Eastern on Sunday!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
Software Development
Tag Cloud
access acer asus bios bsod computer crash desktop driver drivers error ethernet excel freeze gaming graphics hard drive hardware hdmi internet laptop lcd malware memory monitor motherboard network operating system printer problem ram registry router slow software sound trojan ubuntu 11.10 uninstall usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Software & Hardware > Software Development >
Help me simplify this noob c++ code :)

Reply  
Thread Tools
matrim's Avatar
Senior Member with 195 posts.
 
Join Date: Dec 2004
Experience: Little above average
25-Sep-2006, 02:29 AM #1
Help me simplify this noob c++ code :)
Hi,
I'm trying to write a program that computes interest and check fees and then prints a new calculated balance. So far it works okay, but seems like I have a lot of unnecessary work. Can someone help me simplify it down plz?

ps: The premium acct has no check fees, but the standard one does. premium=p, standard=s;
Premium acct gives .05 interest if the account is above 5k or .03 if it is under.
Code:
#include <iostream>
#include <conio.h>

using namespace std;


int main()
	{
		double FIVE = 5000.00;
		const double LOW_INTEREST = 0.03;
		const double HIGH_INTEREST = 0.05;
		const double CHECK_FEE = 0.10;
		char acct_type;
		bool okay = true, notOkay = false;
		
		
		do 
		{
		cout << "What type of account is this: " << endl;
		cout << "Type 's' for a standard account: " << endl;
		cout << "Type 'p' for a premium account: " << endl;
		cin >> acct_type;
		
		cin.ignore(80, '\n');
		okay = (acct_type == 's' || acct_type == 'p');
		if (!okay)
			cout << "\nYou entered an invalid account type!" << endl;
		}while(!okay);
		
		double earned_interest;
		double fees;
		double newbalance;
		double balance;
		int checks;
		
		if (acct_type == 'p') {
			
		cout << "Enter in the ending balance for this month: " << endl;
		cin >> balance;
		if (balance >= FIVE)
		earned_interest = balance * HIGH_INTEREST;
		else earned_interest = balance * LOW_INTEREST;
		
		
		if (balance < FIVE) {
		cout << "Enter in the number of checks written this month: " << endl;
		cin >> checks;
		fees = checks * CHECK_FEE;
		
		
		newbalance = balance - fees + earned_interest;
		cout.precision(2);
			cout << "\nThe new ending balance is: " << "$" << fixed << newbalance << endl;
		}else{
		newbalance = balance + earned_interest;
		cout << "\nThe new ending balance is: " << "$" << newbalance << endl;
		}
		}		 
		if (acct_type == 's')
		cout << "Enter in the ending balance for this month: " << endl;
		cin >> balance;
		cout << "Enter in the number of checks written this month: " << endl;
		cin >> checks;
			earned_interest = balance * LOW_INTEREST;
			fees = checks * CHECK_FEE;
			newbalance = balance - fees + earned_interest;
			cout.precision(2);
			cout << "\nThe new ending balance is: " << "$" << fixed << newbalance << endl;
		
		//getch();
		return 0;
__________________
Matrim
Windows Vista HP
256mb nvidia 7600 GT
3.0 Gig DDR2 RAM
Gigabyte Mobo
260 Gb Western Digital, 300 Gb Maxtor
AMD 4000+ Dual Core
GAMES: Battlefield 2 & BF 2142, Half LIfe 2, F.E.A.R, Anarchy Online, CS:S
www.nutrafitness.com/phpBB2
MUDThunderdome.nutrafitness.com
dquigley's Avatar
Computer Specs
Senior Member with 112 posts.
 
Join Date: Apr 2006
Location: Woodinville, WA
Experience: Advanced
25-Sep-2006, 03:38 AM #2
Here's one way to do it that takes advantage of the OOP nature of C++... note that the Account class contains the logic, not the main program loop.

Code:
#include <iostream>
#include <conio.h>

using namespace std;

#define LOW_INTEREST 0.03
#define HIGH_INTEREST 0.05
#define CHECK_FEE 0.10
#define FIVE 5000.00

class Account
{
public:

    char m_acct_type;
    double m_balance;
    long m_checks_written;

    Account(char type)
    {
        m_acct_type = type;
        m_balance = 0.0;
        m_checks_written = 0;
    }
    
    bool needChecks()
    {
      return (m_acct_type == 's');
    }

    double Interest()
    {
        if (m_acct_type == 's')
            return 0.0;

        if (m_balance > FIVE)
            return m_balance * (HIGH_INTEREST / 12);
        else
            return m_balance * (LOW_INTEREST  / 12);
    }

    double CheckFees()
    {
        if (m_acct_type == 'p')
            return 0.0;
        else
            return m_checks_written * CHECK_FEE;
    }

    double NewBalance()
    {
        return m_balance - CheckFees() + Interest();
    }

};


int main()
{
    char acct_type;
    bool okay;
    do 
    {
        cout << "What type of account is this: " << endl;
        cout << "Type 's' for a standard account: " << endl;
        cout << "Type 'p' for a premium account: " << endl;
        cin >> acct_type;
        cin.ignore(80, '\n');
        okay = (acct_type == 's' || acct_type == 'p');
        if (!okay)
            cout << "\nYou entered an invalid account type!" << endl;
    } while(!okay);
	
    Account *acct = new Account(acct_type);
	
    cout << "Enter in the ending balance for this month: " << endl;
    cin >> acct->m_balance;

    if (acct->needChecks())
    {
        cout << "Enter in the number of checks written this month: " << endl;
        cin >> acct->m_checks_written;
    }
    cout.precision(2);
    cout << "\nBeginning balance : " << "$" << fixed << acct->m_balance << endl;
    cout << "\n         Interest : " << "$" << fixed << acct->Interest() << endl;
    cout << "\n       Check fees : " << "$" << fixed << acct->CheckFees() << endl;
    cout << "\n      New balance : " << "$" << fixed << acct->NewBalance() << endl;

    delete acct;
    return 0;
}
Best,
Dan

Last edited by dquigley; 25-Sep-2006 at 11:54 AM..
matrim's Avatar
Senior Member with 195 posts.
 
Join Date: Dec 2004
Experience: Little above average
02-Oct-2006, 12:17 AM #3
Thanks for that. If you could for a minute, assume I've not had information about classes yet, but have had only the beginnings of code( we haven't covered classes yet I mean), what do you think then? Does my code work okay?
dquigley's Avatar
Computer Specs
Senior Member with 112 posts.
 
Join Date: Apr 2006
Location: Woodinville, WA
Experience: Advanced
02-Oct-2006, 03:41 AM #4
Well no... there are several issues.

1) You are charging interest on a monthly basis. Interest is usually charged yearly. action:divide the interest charges by 12

2) The logic requires that the user answers the question "Enter in the number of checks written this month: " twice if the account type is P and the balance is under $5,000


Best,
Dan
Shadow2531's Avatar
Senior Member with 2,640 posts.
 
Join Date: Apr 2001
02-Oct-2006, 07:00 AM #5
An example using functions and validating input:

Code:
#include <cstdio>
using namespace std;

char getType() {
    while ( 1 ) {
        printf("\n(s)tandard or (p)remium account? ");
        const char type( getchar() );
        fflush( stdin );    
        if ( type == 's' || type == 'p' ) {
            return type;
        }
        printf("\nInvalid account type!\n");
    }
}

double getBalance() {
    while ( 1 ) {
        printf("\nEnding balance for this month? $");
        double balance;
        const int good( scanf("%lf", &balance ) );
        fflush(stdin);
        if ( good ) {
            return balance;
        }
        printf("\nInvalid balance!\n");
    }
}

int getChecks() {
    while ( 1 ) {
        printf("\nNumber of checks written this month? ");
        int checks;
        const int good( scanf( "%d", &checks ) );
        fflush(stdin);
        if ( good && checks > -1 ) {
            return checks;
        }
        printf("\nInvalid number of checks\n");
    }
}

int main() {
    const char type( getType() );
    double balance( getBalance() );
    if ( type == 'p' && balance >= 5000.00 ) {
        balance += balance * 0.05 / 12;
    } else {
        balance += balance * 0.03 / 12;
        if ( type == 's' ) {
            balance -= getChecks() * 0.10;
        }
    }
    printf("\nNew ending balance is $%.2f\n", balance );
}
Basically, if you're going to check your account type input to make sure it's right, you should check the others inputs also. I did it with the C functions getchar(), scanf() and fflush() etc. to handle bad input instead of cin.good(), cin.clear() and cin.ignore() etc. It's just easier with the former, but use whatever you're supposed to. ( You should really use std::string and getline(cin,string) to grab input, validate it and convert when necessary, but that's overdoing it for this.)

You want to only ask for each input once instead of multiple times and as already said, if that's the rate for the whole year, you need to divide.

If that doesn't produce your desired output, change it to do so of course.

Last edited by Shadow2531; 02-Oct-2006 at 10:51 AM..
Reply

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
Thread Tools



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 12:47 AM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.