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: Lv1 Programmer 2009-04-19 9:59

The major errors I'm getting:



1. I get these errors simply initializing the array
--error C2059: syntax error : '{'
--error C2334: unexpected token(s) preceding '{'; skipping apparent function body


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

        double accountArray[accounts][field] = {};





2. Syntax errors surrounding the lines found in Credit::mainCredit()


        accountArray[i][0] = temp.getAccountNumber(void);
        accountArray[i][1] = temp.getCreditLimit(void);
        accountArray[i][2] = temp.calcNewBalance(void);




3. 'accountArray' : undeclared identifier
I get this error the most

Name: Anonymous 2009-04-19 9:59

Thanks for letting us know. Debugging can always be a bit tricky, but I'm sure you'll be able to find the problem.

Name: Anonymous 2009-04-19 10:09

How did you come up with this? Your syntax errors are trivial and easily solvable, but the actual problem here is that the program you've written (or copied and modified from somewhere?) is terrible!

1. Don't place your code in header files. Ever.
2. You're using a class to do something that has nothing to do with object-oriented programming. If you want to use OOP, use OOP. If not, don't abuse classes and methods for procedural programming.

The bottom line is: the person teaching you doesn't know anything about teaching programming, and shouldn't be doing it. If this is a class, change schools.

Name: Anonymous 2009-04-19 10:25

What's the point of using an OOP language if you don't even use objects?

Name: Anonymous 2009-04-19 10:27

>>5
The nice feeling you can do this:
http://dis.4chan.org/read/prog/1239952953/5

Name: Anonymous 2009-04-19 10:34

double accountArray[accounts][field] = {};

Don't do this. You're not using C, so don't use C arrays. Use a std::vector (or better yet, a boost::multi_array) instead.

Name: Anonymous 2009-04-19 10:34

Use a constructor to initialize the values of the array

Name: Anonymous 2009-04-19 10:45

>>4
>>5
You know, programming in an OOP language doesn't mean EVERYTHING has to have objects. He's obviously learning, and by the QUALITY of the code, I can tell he just recently started. Learning the basics first is fine.

>>7
You're not using C, so don't use C arrays.
0/10

Name: Anonymous 2009-04-19 10:46

A 2-dim array in C++?
You best be joking nigger.

Name: Anonymous 2009-04-19 10:50

Here's some help, should get the ball rolling:


class Credit
{
    public:
        Credit( int x = 4, int y = 3 );    // Constructor; x = accounts; y = fields

       
    private:
        int accounts;
        int field;
        double accountArray[][];


// Constructor
Credit::Credit(int x, int y)
{
    int accounts = x;
    int field = y;
    double accountArray[accounts][field] = {}{};
}
// end Constructor

Name: Anonymous 2009-04-19 10:53

>>11
Forgot to initialize the array properly
It would go something like:


for (int row=0; row<accounts; row++) {
    for (int col=0; col<field; col++) {
        accountArray[row][col] = ' ';
    }
}

Name: Lv1 Programmer 2009-04-19 11:00

>>9
What do you mean by quality?
My friend read the code and also said the quality hurt his eyes lol.
Is quality only improved with time (more programming experience)?

Also the code is inside the header because that's the way the professor wanted it. Also I can't use vectors because the assignment requires the use of arrays.


>>11
>>12
Thanks for the help! The only error I get now is:
1. error C2087: 'accountArray' : missing subscript
2. warning C4200: nonstandard extension used : zero-sized array in struct/union

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

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