Hey guys, I am tying to get my program to work, I don't have any errors but I cant get an output from it. I am supposed to output the second set of three from the nine inputs. Thanks to those who reply.

#include <iostream>
using namespace std;

class TMoney
{
private:
     int tdollars;
     int twons;
     int tpennies;
     int MONEY;
     int TDollars,TWons,TPennies;
public:
     TMoney():tdollars(0),twons(0),tpennies(0){}
     void inputData();
     void outputData();
     int getTDollars(){return tdollars;}
     void setTDollars(int td){tdollars = td;}
     int getTWons(){return twons;}
     void setTWons(int tw){twons = tw;}
     int getTPennies() {return tpennies;}
     void setTPennies(int tp){tpennies = tp;}
     
};

int main()
{
	
     TMoney money[3];
     money[0].inputData();
     money[0].outputData();
     
     

     return 0;
}

void TMoney::inputData()
{
    int j;
     for (j=0;j<3;j++)
     {
     int td;
     cout <<"Enter how many tdoallars you own.";
     cin >> td;
     setTDollars(td);
     int tw;
     cout <<"Enter how many twons you own.";
     cin >> tw;
     setTWons(tw);
     int tp;
     cout << "Enter how many tpennies you own.";
     cin >> tp;
     setTPennies(tp);
     }
}

void TMoney::outputData()
{
    int m;
    for(m=0;m<MONEY;m++)
    {
       
     tdollars = getTDollars();
     twons = getTWons();
     tpennies = getTPennies();
     cout << "You have ," << TDollars << " tdollars," << TWons <<" twons." <<"and"<< TPennies << "tpennise"<<endl;
    }

}

Recommended Answers

All 7 Replies

Hi,

You have made several errors (a) mis-understanding of C++ [these are critical since they affect all further programming in the language]
(b) logic errors. They only effect this program.


I think that you have mis-understood the class encapsulation rules.
Private data is available to all members of the class as both the current and other versions of the class.

Let me give an example:

class A
{
    private:
        int x;
    public:
      void func();
      void f2(const A&);
}
       
void 
A::func()
{
    x++;   // acceptable
}

void 
A::f2(const A& Other)
{
    x+=Other.x;      // Also acceptable
}

Notice that func is able to just change x. Also f2 is able to access Other's version of x as well as its own. Note also by making Other const I have forbidden that Other's x is changed. If I make a mistake in the code logic and do change Other's x (or anything else in x) then the compiler will tell me. Important. compiler errors are many times easier to find than runtime errors. Almost anything that moves errors from runtime to compile time is worth doing.

That means that you don't need all the getTdollar etc with outputData() since it is a class member method.

If your class has a constructor / copy-constructor / assignment operators, you should nearly alway initialize everything. [Unless an expert (and even then most of the time) you should always define these three methods + the destructor explicitly
if you define any one of them]
.

Constant things should be defined const. Consider outputData should it change no. If you use good C++ practice and had defined this method const ( void outputData() const ), you would have immediately been told about your error by the compiler.
you have made the same mistake on all the get methods.

(B) Logic Errors

What is the for(int j=0;j<3;j++) line for in inputData().
It just asks the same three questions 3 times.
See above just use tpennies = tp; etc

In output data you use getTDollars to set a variable tdollars (which is in the class -- THEN you use TDollars which is also in the class.
This variable has never been set. (Why you have tdollars and TDollars I have no idea.) You need to remove the getTDollars() and just print the results from tdollars etc. (and make the method const please!!)

(B) Logic Errors

What is the for(int j=0;j<3;j++) line for in inputData().
It just asks the same three questions 3 times.
See above just use tpennies = tp; etc

The assignment asks to have it like that, but it needs to output just the second set of inputs.

TMoney money[3];
money[0].inputData();
money[0].outputData();

If you have an array but only use the first element of the array, why bother with the array. I'd use a loop in main() to access all three elements of the array, calling input() on each of them in turn and calling output() only if it is the second set of three (I'll let you figure out what index (loop counter value) that needs to be).

input() and output() are public member functions so they don't need to use the setter and getter (mutator and accessor) functions to alter/access the member variable values, though they can do so if you want them to.

This is going to sound stupid, but what kind of loop a for loop? I am not sure about how I am supposed to be doing this, and I know I need to do my own homework, and I am definitely trying to do it.

From what I'm reading of the posts and assignment, you should remove the for loop from inputData() and add a for loop to main to call inputData for all three TMoney members of the array money.

TMoney money[3];
for (int jj = 0; jj < 3; jj++)
    money[jj].inputData();

The first data entered will be in money[0], the second in money[1] and the third in money[2].

You're supposed to print the second set?

money[1].outputData();

Yes I am supposed to print the second set, so should I remove the for loop from outputData() as well?

yes, and be careful of using the get* methods in outputdata, its not necessary, just reference the members.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.