Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++ Homework Help

Name: Lv1 Programmer 2009-04-19 9:52

Help /prog/!
I've been debugging this thing for hours and it still doesn't work.



////////////////////////////////////////////////////////////////
//    Lab 4 - Definitions.h
///////////////////////////////////////////////////////////////

//user supplies the input of X number of accounts (determined by constant accounts)
//input includes: account number, credit limit, charges, credited, starting balance, and new (final) balance
//accounts are sorted by account # and printed with headers

#include <iostream>
using namespace std;

class Credit
{
    public:
        void mainCredit(void);    // main function
        double getAccountNumber(void);    //gets input and returns account number
        double getCreditLimit(void);    //gets input and returns credit limit
        double calcNewBalance(void);    //gets input, performs calculations, and returns new balance
        void sortArray(void);    //sorts the array after main loop
        void displayAll(void);    //prints headers and final array

       
    private:
        static const int accounts = 4;
        static const int field = 3;

        double accountArray[accounts][field] = {};
        // account[0][0]
        // First number represents the number of accounts
        // Second number represents the elements of the account
        // [0][0] = Account number
        // [0][1] = Credit limit
        // [0][2] = New balance
        // [0][3] = Null
};


void Credit::mainCredit()
{
    int i = 0;
    Credit temp;

    cout << "Credit System 3000\n------------------" << endl;

    for(i=0; i <= accounts; i++)
    {
        cout << "\n -- " << i << " -- ";
        accountArray[i][0] = temp.getAccountNumber(void);
        accountArray[i][1] = temp.getCreditLimit(void);
        accountArray[i][2] = temp.calcNewBalance(void);
    }

    void temp.sortArray(void);
    void temp.displayAll(void);
}
//end mainCredit


double Credit::getAccountNumber()
{
    double accountNumber = 0;

    cout << "\nAccount Number: ";
    cin >> accountNumber;

    return accountNumber;
}
//end getAccountNumber


double Credit::getCreditLimit()
{
    double creditLimit = 0;

    cout << "\nCredit Limit: ";
    cin >> creditLimit;

    return creditLimit;
}
//end getCreditLimit


double Credit::calcNewBalance()
{
    // First get starting balance
    // Then get total charged (spent)
    // Then get total credited (payed)
    // Finally calculate new balance

    double startingBalance = 0;
    double totalCharged = 0;
    double totalCredited = 0;
    double newBalance = 0;

    cout << "\nStarting balance: ";
        cin >> startingBalance;
   
    cout << "\nTotal charged: ";
        cin >> totalCharged;

    cout << "\nTotal credited: ";
        cin >> totalCredited;

    newBalance = (startingBalance + totalCharged) - totalCredited;

    return newBalance;
}
//end calcNewBalance


void Credit::sortArray()    //sorts the accounts from lowest to highest (by account #)
{
    int i = 0;
    double temp;

    for(i=0; i <= (accounts + 1); i++)
    {
        int x = 0;
        while( x <= (accounts - 1) )
        {
            if(accountArray[x][0] > accountArray[x+1][0])
            {
                temp = accountArray[x][0];
                accountArray[x][0] = accountArray[x+1][0];
                accountArray[x+1][0] = temp;

                temp = accountArray[x][1];
                accountArray[x][1] = accountArray[x+1][1];
                accountArray[x+1][1] = temp;

                temp = accountArray[x][2];
                accountArray[x][2] = accountArray[x+1][2];
                accountArray[x+1][2] = temp;

                x++;
            }
            else
            x++;
        }
    }
}
//end sortArray


void Credit::displayAll()
{
    int i = 0;
   
    for(i=0; i <= accounts; i++)
    {
        cout << accountArray[i][0] << accountArray[i][1] << accountArray[i][2] << endl;
    }

}
//end displayAll




//////////////////////////////////////////////////////////////
//    Lab 4 - Test.cpp
//////////////////////////////////////////////////////////////

#include "Definitions.h"


int main()
{
    Credit temp;
    temp.mainCredit();

    return 0;
}

Name: Anonymous 2009-04-19 18:19

Here, five minutes in EMACS. I hope this is satisfactorily awful for your class. Tell your professor he shouldn't be teaching something he doesn't understand himself.

////////////////////////////////////////////////////////////////
//    Lab 4 - Definitions.h
///////////////////////////////////////////////////////////////

/* user supplies the input of X number of accounts (determined by
   constant accounts) input includes: account number, credit limit,
   charges, credited, starting balance, and new (final) balance accounts
   are sorted by account # and printed with headers */

#include <iostream>
#include <cstdlib>

using namespace std;

static const int accounts = 4;
static const int field = 3;
double accountArray[accounts][field];

double getNamedDouble(char *);
double calcNewBalance();
void sortArray();
void displayAll();

double getNamedDouble(char *label)
{
    double result;
    cout << endl << label << ": ";
    cin >> result;
    return result;
}

double calcNewBalance()
{
    double startingBalance, totalCharged, totalCredited;

    startingBalance = getNamedDouble("Starting balance");
    totalCharged = getNamedDouble("Total charged");
    totalCredited = getNamedDouble("Total credited");

    return startingBalance + totalCredited - totalCharged;
}

int compare(const void *a, const void *b)
{
    if (((double *) a)[0] < ((double *) b)[0])
        return -1;
    if (((double *) a)[0] > ((double *) b)[0])
        return 1;
    return 0;
}

void sortArray()
{
    qsort(accountArray, accounts, sizeof(accountArray[0]), &compare);
}

void displayAll()
{
    for (int i = 0; i < accounts; i++)
        cout <<
            accountArray[i][0] << " " <<
            accountArray[i][1] << " " <<
            accountArray[i][2] << endl;
}

//////////////////////////////////////////////////////////////
//    Lab 4 - Test.cpp
//////////////////////////////////////////////////////////////

#include "Definitions.h"

/* g o a t s e x * g o a t s e x * g o a t s e x *
 g                                               g
 o /     \             \            /    \       o
 a|       |             \          |      |      a
 t|       `.             |         |       :     t
 s`        |             |        \|       |     s
 e \       | /       /  \\\   --__ \\       :    e
 x  \      \/   _--~~          ~--__| \     |    x
 *   \      \_-~                    ~-_\    |    *
 g    \_     \        _.--------.______\|   |    g
 o      \     \______// _ ___ _ (_(__>  \   |    o
 a       \   .  C ___)  ______ (_(____>  |  /    a
 t       /\ |   C ____)/      \ (_____>  |_/     t
 s      / /\|   C_____)       |  (___>   /  \    s
 e     |   (   _C_____)\______/  // _/ /     \   e
 x     |    \  |__   \\_________// (__/       |  x
 *    | \    \____)   `----   --'             |  *
 g    |  \_          ___\       /_          _/ | g
 o   |              /    |     |  \            | o
 a   |             |    /       \  \           | a
 t   |          / /    |         |  \           |t
 s   |         / /      \__/\___/    |          |s
 e  |           /        |    |       |         |e
 x  |          |         |    |       |         |x
 * g o a t s e x * g o a t s e x * g o a t s e x */

int main()
{
    cout << "Credit System 3000" << endl << "------------------" << endl;

    for (int i = 0; i < accounts; i++) {
        cout << endl << " -- " << i << " -- ";
        accountArray[i][0] = getNamedDouble("Account Number");
        accountArray[i][1] = getNamedDouble("Credit Limit");
        accountArray[i][2] = calcNewBalance();
    }

    sortArray();
    displayAll();

    return 0;
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List