stuck on c++ assignment

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2008
Posts: 7
Reputation: gamer12223 is an unknown quantity at this point 
Solved Threads: 0
gamer12223 gamer12223 is offline Offline
Newbie Poster

stuck on c++ assignment

 
0
  #1
Dec 8th, 2008
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.
  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. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,917
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 274
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: stuck on c++ assignment

 
0
  #2
Dec 8th, 2008
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?
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 77
Reputation: mahlerfive is an unknown quantity at this point 
Solved Threads: 16
mahlerfive mahlerfive is offline Offline
Junior Poster in Training

Re: stuck on c++ assignment

 
0
  #3
Dec 8th, 2008
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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 7
Reputation: gamer12223 is an unknown quantity at this point 
Solved Threads: 0
gamer12223 gamer12223 is offline Offline
Newbie Poster

Re: stuck on c++ assignment

 
0
  #4
Dec 8th, 2008
Originally Posted by mahlerfive View Post
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:
  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.
  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. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,917
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 274
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: stuck on c++ assignment

 
0
  #5
Dec 8th, 2008
Strange... getinputdata is the same as outputdata??
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 7
Reputation: gamer12223 is an unknown quantity at this point 
Solved Threads: 0
gamer12223 gamer12223 is offline Offline
Newbie Poster

Re: stuck on c++ assignment

 
0
  #6
Dec 8th, 2008
I thought it was wrong but I didn't know, not sure what to put. I'm sure ill figure it out eventually.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC