943,616 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 450
  • C++ RSS
Dec 8th, 2008
0

stuck on c++ assignment

Expand Post »
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.
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class TMoney
  5. {
  6. private:
  7. int tdollars ;
  8. int twons;
  9. int tpennies;
  10.  
  11.  
  12. public:
  13. TMoney():tdollars(0),twons(0),tpennies(0){}
  14. void getinputdata();
  15.  
  16. cout << "Enter the number of tdollars you own:";
  17. cin >> tdollars;
  18. cout << "Enter the number of twons you own:";
  19. cin >> twons;
  20. cout << "Enter the number of tpennies you own:";
  21. cin >> tpennies;
  22.  
  23.  
  24. void outputdata();
  25.  
  26.  
  27. };
  28.  
  29.  
  30. int main()
  31. {
  32. const int size = 3; // Added this line so you can easily change the input for everything just changeing this
  33. TMoney money[size];
  34. int tdollars,twons,tpennies;
  35.  
  36. // Changed your declaration of the money variable
  37.  
  38. for (int j = 0; j < size; j++) // Changed the 3 to size
  39. {
  40. money[j].getinputdata(); // Added this
  41. // Put all of this inside your getinputdata() function within the class.
  42. // cout << "Enter the number of tdollars you own:";
  43. // cin >> tdollars;
  44. //cout << "Enter the number of twons you own:";
  45. //cin >> twons;
  46. //cout << "Enter the number of tpennies you own:";
  47. //cin >> tpennies;
  48. }
  49.  
  50. for (int j=0; j<1; j++)
  51. {
  52. cout << "You have tdollars," << " twons," << " tpennies." << endl;
  53. cin >> money[size].outputdata;
  54. }
  55.  
  56. return 0;
  57. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gamer12223 is offline Offline
7 posts
since Dec 2008
Dec 8th, 2008
0

Re: stuck on c++ assignment

for (int j=0; j<1; j++)
{
cout << "You have tdollars," << " twons," << " tpennies." << endl;
cin >> money[size].outputdata;
}
Why j<1?
Why cin when there is output? size?
Reputation Points: 2023
Solved Threads: 644
Senior Poster
ddanbe is online now Online
3,735 posts
since Oct 2008
Dec 8th, 2008
0

Re: stuck on c++ assignment

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:
C++ Syntax (Toggle Plain Text)
  1. void getinputdata() {
  2.  
  3. cout << "Enter the number of tdollars you own:";
  4. cin >> tdollars;
  5. cout << "Enter the number of twons you own:";
  6. cin >> twons;
  7. cout << "Enter the number of tpennies you own:";
  8. cin >> tpennies;
  9. }

The outputdata() method should be written similarily.

Also, when calling outputdata you are missing the parentheses.
Last edited by mahlerfive; Dec 8th, 2008 at 3:05 pm.
Reputation Points: 33
Solved Threads: 18
Junior Poster in Training
mahlerfive is offline Offline
77 posts
since Aug 2008
Dec 8th, 2008
0

Re: stuck on c++ assignment

Click to Expand / Collapse  Quote originally posted by mahlerfive ...
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:
C++ Syntax (Toggle Plain Text)
  1. void getinputdata() {
  2.  
  3. cout << "Enter the number of tdollars you own:";
  4. cin >> tdollars;
  5. cout << "Enter the number of twons you own:";
  6. cin >> twons;
  7. cout << "Enter the number of tpennies you own:";
  8. cin >> tpennies;
  9. }

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.
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class TMoney
  5. {
  6. private:
  7. int tdollars ;
  8. int twons;
  9. int tpennies;
  10.  
  11.  
  12. public:
  13. TMoney(): tdollars(0), twons(0), tpennies(0){}
  14.  
  15. void getinputdata(){
  16.  
  17. cout << "Enter the number of tdollars you own:";
  18. cin >> tdollars;
  19. cout << "Enter the number of twons you own:";
  20. cin >> twons;
  21. cout << "Enter the number of tpennies you own:";
  22. cin >> tpennies;
  23. }
  24.  
  25. void outputdata(){
  26.  
  27. cout << "Enter the number of tdollars you own:";
  28. cin >> tdollars;
  29. cout << "Enter the number of twons you own:";
  30. cin >> twons;
  31. cout << "Enter the number of tpennies you own:";
  32. cin >> tpennies;
  33. }
  34.  
  35.  
  36.  
  37. };
  38.  
  39.  
  40. int main()
  41. {
  42. const int size = 3;
  43. TMoney money[size];
  44. int tdollars,twons,tpennies;
  45.  
  46.  
  47.  
  48. for (int j = 0; j < size; j++)
  49. {
  50. money[j].getinputdata();
  51.  
  52. }
  53.  
  54. for (int j=0; j<1; j++)
  55. {
  56. cout << "You have tdollars," << " twons," << " tpennies." << endl;
  57.  
  58. }
  59.  
  60. return 0;
  61. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gamer12223 is offline Offline
7 posts
since Dec 2008
Dec 8th, 2008
0

Re: stuck on c++ assignment

Strange... getinputdata is the same as outputdata??
Reputation Points: 2023
Solved Threads: 644
Senior Poster
ddanbe is online now Online
3,735 posts
since Oct 2008
Dec 8th, 2008
0

Re: stuck on c++ assignment

I thought it was wrong but I didn't know, not sure what to put. I'm sure ill figure it out eventually.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gamer12223 is offline Offline
7 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: USB Dismount not turning off my USB Drive?
Next Thread in C++ Forum Timeline: Populating a 3d vector array





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC