I am working on a program and this what I must do, Create a class based on TMoney. TMoney only contains three denominations, tdollars, twons (worth
1/5 or .2 of a dollar) and tpennies.
Create an array of three Tmoney objects. Use a loop to input data into each of the elements. Print out
the data for the second Tmoney entered.
● inputdata prompts the user for input and then stores the values they enter in the object
● outputdata prints the information stored in the object
I am stuck because I keep getting errors on my cin and couts, and the last cin is also giving me an error. Could someone please help me? Thanks to those who reply.

#include <iostream>
using namespace std;

class TMoney
{
private:
    int tdollars ;
    int twons;
    int tpennies;

   
public:
    TMoney():tdollars(0),twons(0),tpennies(0){}
    void getinputdata();
   
    cout << "Enter the number of tdollars you own:";
    cin >> tdollars;
    cout << "Enter the number of twons you own:";
    cin >> twons;
    cout << "Enter the number of tpennies you own:";
    cin >> tpennies;
   
     
    void outputdata();


};


int main()
{   
    const int size = 3;   //  Added this line so you can easily change the input for everything just changeing this
  TMoney money[size];
  int tdollars,twons,tpennies;
 
  //  Changed your declaration of the money variable
 
  for (int j = 0; j < size; j++)  //  Changed the 3 to size
  {
   money[j].getinputdata();  //  Added this
//  Put all of this inside your getinputdata() function within the class.
  //  cout << "Enter the number of tdollars you own:";
   // cin >> tdollars;
    //cout << "Enter the number of twons you own:";
    //cin >> twons;
    //cout << "Enter the number of tpennies you own:";
    //cin >> tpennies;
  }
 
  for (int j=0; j<1; j++)
  {
    cout << "You have  tdollars," << "  twons," << "  tpennies."  << endl;
    cin >> money[size].outputdata;
  }

    return 0;
}

Recommended Answers

All 5 Replies

for (int j=0; j<1; j++)
{
cout << "You have tdollars," << " twons," << " tpennies." << endl;
cin >> money.outputdata;
}
Why j<1?
Why cin when there is output? size?

You are getting mixed up about what should be in your main() and what should be in your class methods.

As the problem specifies, input for a TMoney should be done in the inputdata() method, and output should be done in the outputdata() method. The main should only declare an array of 3 TMoney, loop to call inputdata() three times, then call outputdata() on the 2nd TMoney.

Your inputdata method needs braces to surround the code that belongs in the method like this:

void getinputdata() {
   
    cout << "Enter the number of tdollars you own:";
    cin >> tdollars;
    cout << "Enter the number of twons you own:";
    cin >> twons;
    cout << "Enter the number of tpennies you own:";
    cin >> tpennies;
}

The outputdata() method should be written similarily.

Also, when calling outputdata you are missing the parentheses.

You are getting mixed up about what should be in your main() and what should be in your class methods.

As the problem specifies, input for a TMoney should be done in the inputdata() method, and output should be done in the outputdata() method. The main should only declare an array of 3 TMoney, loop to call inputdata() three times, then call outputdata() on the 2nd TMoney.

Your inputdata method needs braces to surround the code that belongs in the method like this:

void getinputdata() {
   
    cout << "Enter the number of tdollars you own:";
    cin >> tdollars;
    cout << "Enter the number of twons you own:";
    cin >> twons;
    cout << "Enter the number of tpennies you own:";
    cin >> tpennies;
}

The outputdata() method should be written similarily.

Also, when calling outputdata you are missing the parentheses.

Ok, now I have this am I any closer? Sorry if I misunderstood you and thanks for the reply.

#include <iostream>
using namespace std;

class TMoney
{
private:
    int tdollars ;
    int twons;
    int tpennies;

   
public:
     TMoney(): tdollars(0), twons(0), tpennies(0){}
     
    void getinputdata(){
   
     cout << "Enter the number of tdollars you own:";
        cin >> tdollars;
    cout << "Enter the number of twons you own:";
        cin >> twons;
    cout << "Enter the number of tpennies you own:";
        cin >> tpennies;
    }
     
    void outputdata(){

        cout << "Enter the number of tdollars you own:";
        cin >> tdollars;
    cout << "Enter the number of twons you own:";
        cin >> twons;
    cout << "Enter the number of tpennies you own:";
        cin >> tpennies;
    }



};


int main()
{   
    const int size = 3; 
  TMoney money[size];
  int tdollars,twons,tpennies;
 
 
 
  for (int j = 0; j < size; j++) 
  {
   money[j].getinputdata();

  }
 
  for (int j=0; j<1; j++)
  {
    cout << "You have  tdollars," << "  twons," << "  tpennies."  << endl;
    
  }

    return 0;
}

Strange... getinputdata is the same as outputdata??

I thought it was wrong but I didn't know, not sure what to put. I'm sure ill figure it out eventually.

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.