| | |
stuck on c++ assignment
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2008
Posts: 7
Reputation:
Solved Threads: 0
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/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)
#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; }
•
•
Join Date: Aug 2008
Posts: 77
Reputation:
Solved Threads: 16
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:
The outputdata() method should be written similarily.
Also, when calling outputdata you are missing the parentheses.
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)
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.
Last edited by mahlerfive; Dec 8th, 2008 at 3:05 pm.
•
•
Join Date: Dec 2008
Posts: 7
Reputation:
Solved Threads: 0
•
•
•
•
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)
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.
C++ Syntax (Toggle Plain Text)
#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; }
![]() |
Similar Threads
- Java Assignment Help Needed!!!!!!!!!! (Java)
- OO assignment help (Java)
- Help with programming assignment (Python)
- Simple array/class problem (dot operator)??? (C++)
- i need help getting started with my programming assignment (C++)
- new programmer stuck with 'if statements.. (C++)
- Many Errors while doing this assignment (C)
- help neede about converting characters (C++)
- stuck with definition (C++)
Other Threads in the C++ Forum
- Previous Thread: USB Dismount not turning off my USB Drive?
- Next Thread: Populating a 3d vector array
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






