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: 4 2009-04-19 11:03

>>9
You know, programming in an OOP language doesn't mean EVERYTHING has to have objects.
Huh? Did you even read what I wrote? The problem was exactly that is using `objects' for clearly non-OOP code.

Learning the basics first is fine.
Learning the basics in an ass-backwards way like OP is doing is the worst way you can start your programming career with. Stop giving such disastrous advice.

Name: Anonymous 2009-04-19 11:04

>>13
Also the code is inside the header because that's the way the professor wanted it.
Your professor is an idiot. Quit the class now.

Name: Anonymous 2009-04-19 11:10

>>15
THIS!!!!!!!!!!!1

Name: Anonymous 2009-04-19 11:23

YOU DONT NEED A HEADER FILE FOR A PROGRAM AS SIMPLE AS THIS! FUCK!

Name: Anonymous 2009-04-19 11:24

>>13
Also the code is inside the header because that's the way the professor wanted it.
Please be kidding.

Name: Anonymous 2009-04-19 11:46

>>14-
NEWSFLASH:
Intro programming courses¹ are shit. All of them.  You don't get a choice.  Just learn to mimic whatever retarded standards your professor uses and drink them all away at the end of the semester.

_____
1. advanced ones too

Name: Anonymous 2009-04-19 13:02

>>19
>>14-
Considered harmful. What if posts after this one don't refer to intro programming course?

Name: Anonymous 2009-04-19 13:17

>>19
SICP!?!??!

Name: Anonymous 2009-04-19 13:17

I mean

600.1!?!??!

Name: Anonymous 2009-04-19 13:19

>>22
I think you mean 6.001

Name: Anonymous 2009-04-19 13:24

>>4
1. Don't place your code in header files. Ever.
Templates?

Name: Lv1 Programmer 2009-04-19 13:38

Okay program runs! :)
I changed the accounts to just 1 for testing purposes.
Everything is inputted just fine.
However, all the outputs return 0.

Could it be the sortArray?

Is accountArray the same array in every function?
When initialized in the class, is it a global within the class?

Name: Anonymous 2009-04-19 13:42

HOW ABOUT qsort []     = []
qsort (x:xs) = qsort (filter (< x) xs) ++ [x] ++ qsort (filter (>= x) xs)

Name: Anonymous 2009-04-19 14:03

>>25
Come back when you're at least Lv30
(Reading SICP gives you 5 levels automatically)

Name: Anonymous 2009-04-19 15:05

>>27
There are only 8 levels. Go read your Jeff Atwood again.

Name: Anonymous 2009-04-19 15:35

>>28
Jeff Atwood
IHBT

Name: Anonymous 2009-04-19 16:05

=  !=  ==

Name: Anonymous 2009-04-19 16:41

>>26
That's not quicksort.

Name: Anonymous 2009-04-19 16:46

>>31
This may surprise you, but that indeed is a quicksort. An absolutely useless, inefficient Haskell masturbatory quicksort, but a quicksort nonetheless.

Name: Anonymous 2009-04-19 17:13

>>32
a slow quick sort?

Name: Anonymous 2009-04-19 17:15

This quicksort method leverages collective synergy to drive "outside of the box" thinking and formulate key
    objectives into a win-win game plan with a quality-driven approach that focuses on empowering key players
    to drive-up their core competencies and increase expectations with an all-around initiative to drive down  the bottom-line.

Name: Lv1 Programmer 2009-04-19 17:38

OP here.
Keeps returning 0's
I even commented out sortArray to check if that was the problem, but it isn't.
Which means there's something going wrong before that...
Help please /prog/

Name: Anonymous 2009-04-19 17:40

Use Haskell.

Name: Anonymous 2009-04-19 17:42

I mean don't.

Name: Anonymous 2009-04-19 18:14

>>32
Except for the fact that no, it isn't.

Name: Anonymous 2009-04-19 18:16

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;
}

Name: Anonymous 2009-04-19 18:31

>>39
I see someone hasn't read that paper by that girl computer scientist that was on the Reddit a while ago.

Name: Anonymous 2009-04-19 18:58

>>41
what!

Name: Anonymous 2009-04-19 21:01

>>40
Amazing. How long have you been programming?

Name: Anonymous 2009-04-19 21:15

>>40
I want to shake your hand for that but you said EMACS so it's probably not a good idea.

Name: Anonymous 2009-04-19 21:38

>>44
Why? What's wrong with EMACS?

Name: Anonymous 2009-04-19 21:54

I created a sorting algorithm that sorts in O(log* N) time.

Name: Anonymous 2009-04-20 6:32

>>43
Approximately ten minutes.

Name: Anonymous 2009-04-20 8:22

>>47
back to /pr/ please

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