When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
48 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
What is "a Wallet 3"?
A Wallet 3 by adding wallet 1 and wallet 2
You already did that.
Wallet w3(w1.getQuarters() + w2.getQuarters(), w1.getDimes() + w2.getDimes(), w1.getNickles() + w2.getNickles(), w1.getCents() + w2.getCents());
cout << endl
<< "W3:";
cout << "\t" << w3.getQuarters() << " quarters" << endl;
cout << "\t" << w3.getDimes() << " dimes" << endl;
cout << "\t" << w3.getNickles() << " nickels" << endl;
cout << "\t" << w3.getCents() << " cents" << endl;
double total3 = ((w3.getQuarters() * 25 + w3.getDimes() * 10 + w3.getNickles() * 5 + w3.getCents()) / 100.0);
cout << "\t--------> amount = $" << total3 << endl;
Instead of these lines in the main function to have the Wallet 3
we will create it by using a regular function outside the main function
Make a function, copy/paste the code into it 🤷
void f(Wallet w1, Wallet w2) {
Wallet w3(w1.getQuarters() + w2.getQuarters(), w1.getDimes() + w2.getDimes(), w1.getNickles() + w2.getNickles(), w1.getCents() + w2.getCents());
cout << endl
<< "W3:";
cout << "\t" << w3.getQuarters() << " quarters" << endl;
cout << "\t" << w3.getDimes() << " dimes" << endl;
cout << "\t" << w3.getNickles() << " nickels" << endl;
cout << "\t" << w3.getCents() << " cents" << endl;
double total3 = ((w3.getQuarters() * 25 + w3.getDimes() * 10 + w3.getNickles() * 5 + w3.getCents()) / 100.0);
cout << "\t--------> amount = $" << total3 << endl;
}
It's just your code with void f(Wallet w1, Wallet w2) {} around it.
So just use addTwoWallets instead of f.
And return w3; at the end.
And you probably don't want to print it.
Can I just have these line in the Function void f(Wallet w1, Wallet w2) {
Wallet w3(w1.getQuarters() + w2.getQuarters(), w1.getDimes() + w2.getDimes(), w1.getNickles() + w2.getNickles(), w1.getCents() + w2.getCents());
And then I will print the wallet in the main ?
Almost. w3 only lives inside f and you cannot access it from main. That's why addTwoWallets returns a Wallet.
That way you can access the result later.
I got errors
What is your code if you do that way
like Function to add 2 wallets
and then print out the result at the main function
?
Like I said, you take the hint:
Wallet addTwoWallets(Wallet w1, Wallet w2)
The code:
Wallet w3(w1.getQuarters() + w2.getQuarters(), w1.getDimes() + w2.getDimes(), w1.getNickles() + w2.getNickles(), w1.getCents() + w2.getCents());
and the return:
return w3;
and that should be it.
Well you also need the { and }.
Let's say If we just have these line in the main function
out << endl
<< "W3:";
cout << "\t" << w3.getQuarters() << " quarters" << endl;
cout << "\t" << w3.getDimes() << " dimes" << endl;
cout << "\t" << w3.getNickles() << " nickels" << endl;
cout << "\t" << w3.getCents() << " cents" << endl;
double total3 = ((w3.getQuarters() * 25 + w3.getDimes() * 10 + w3.getNickles() * 5 + w3.getCents()) / 100.0);
cout << "\t--------> amount = $" << total3 << endl;
What's the function look like out side the main ?
If you do it properly it should be std::ostream &operator<<(std::ostream &os, const Wallet &w).
But void print(Wallet w) will do too.
Could you show the code you do by the void print(Wallet w)
I'm confused why you need that.
void print(Wallet w) {
cout << endl
<< "W3:";
cout << "\t" << w.getQuarters() << " quarters" << endl;
cout << "\t" << w.getDimes() << " dimes" << endl;
cout << "\t" << w.getNickles() << " nickels" << endl;
cout << "\t" << w.getCents() << " cents" << endl;
double total3 = ((w.getQuarters() * 25 + w.getDimes() * 10 + w.getNickles() * 5 + w.getCents()) / 100.0);
cout << "\t--------> amount = $" << total3 << endl;
}
There is no intelligence, it's just copy/paste.
Maybe I misunderstood the requires of the homework
I am also confused the questions on the homework from my professor too
I know how to do it niw
thank you for your helping
Putting the printing into a function makes sense, because print(w2); print(w3); is a lot shorter than your previous code.
And you'll also need print(w3);.
@uncut harbor Has your question been resolved? If so, run !solved :)
@uncut harbor
Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.
!solved