Non-member class functions not working

I have a question that says I have a class called Money and the file money.txt has 2 values in each line, one for dollars and one for cents, separated by a space and the number of lines is given in the first line. I need to read and store the values in a dynamic array of Money objects using non-member functions:

void printMoneyTable ( Money table [ ], int n ); : takes a table of money objects and the number of elements within that table and prints out the objects.

Money sumMoneyTable (Money* table [ ], int n ); : takes a table of pointers to money objects and the number of elements in that table and returns a money object that contains the total sum.

Here's what I've done so far but it's not working and I don't know how to fix it.

#include<iostream>
#include<fstream>
using namespace std;
class Money {

public:
Money(int d = 0, int c = 0) { dollar = d; cent = c; };

Money(const Money & m) {
dollar = m.dollar;
cent = m.cent;
}
Money getdollar() {
return dollar;
}
Money getcent() {
return cent;
}
void setdollar(int d) {
dollar = d;
}
void setcent(int c) {
cent = c;
}
private:
int dollar;
int cent;
};
void printMoneyTable (Money table[], int n) {
for (int i = 0; i < n; i++)
{
Money x = table[i].getdollar();
Money y = table[i].getcent();
cout << "$" << x << "." << y << endl;
}
}
Money sumMoneyTable(Money*table[], int n) {
Money temp = 0;
for (int i = 0; i < n; i++)
{
temp.getdollar += table[i];
temp.getcent += table[i];
}
if (&temp.getcent >= 100)
{
for (int i = 100; i < temp.getcent; (i + 100))
{
temp.getdollar++;
temp.getcent -= 100;
}
}

return temp;
}

int main() {
ifstream in("money.txt");
if (in.fail())
{
cout << "error/n";
exit(1);
}
int n;
in >> n;
Money *table = new Money[n];
Money **tableptr = new Money*[n];
int x, y;
for (int i = 0; i < n; i++)
{
in >> x >> y;
table[i].getdollar() = x;
table[i].getcent() = y;
*tableptr[i] = table[i];
}
cout << "The money objects are: /n";
printMoneyTable(table, n);
cout << "The sum of all money objects in the file is: ";
sumMoneyTable(tableptr, n);
in.close();
system("pause");
return 0;
}
it looks like getdollar and getcent should return integers. why do they return money objects?
I think that is the root of multiple problems.

also, wrap in code tags <> on the side editor.
What is this:
1
2
table[i].getdollar() = x;
table[i].getcent() = y;

meant to do?
It is supposed to input the data read from the text file
you typically have a get and a set.
cout >> thing.get();
cin >> tmp;
thing.set(tmp); //like this

you *can* have your 'getter' return a reference to the actual item and you *can* use that to update the value but this is generally frowned upon and its takes a bit of hand-waving syntax to do it. You can also make the fields public and directly access them, which I have no problem with but it is not considered 'pure'. To me, if your getters and setters are not validating or doing anything at all, they don't need to exist, but I very, very anti-bloat.

I recommend you go back to the simple get and set design and do it like this:
table[i].setdollar(x); //it may also be a bit more readable if you use _ or caps to break up words... get_dolllar or GetDollar or something. allonewordwithoutbreakingitupishardtoread. (I am trying to help you, so yes, do as I say not as I do lol).


Last edited on
Topic archived. No new replies allowed.