[Practice 41.2] Why does my code output ‘1’? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

[Practice 41.2] Why does my code output ‘1’?

Hello, I’m currently stuck on Practice 41.2 but I’m not sure what’s wrong with my code. It keeps outputting 1 for an odd reason, despite all of the data types being ‘string’. Here’s my code: #include <iostream> #include <string> using namespace std; //complete the function with one default argument void printOrder (string a, string b="Black_tea") { } int main() { //getting coffee type string friendOrder; cin >> friendOrder; printOrder(friendOrder); cout << printOrder; return 0; I appreciate any help :)

18th Mar 2021, 9:23 AM
Plaush
Plaush - avatar
6 Answers
+ 1
Plaush You are outputting function name which doesn't make sense. The output may be any undefined value .
18th Mar 2021, 9:29 AM
Hima
Hima - avatar
+ 1
Nvm, I solved it. Turns out, I was being an idiot. Solved it by placing cout ‘inside’ the function’s curly brackets. Thanks for the help! :) p
18th Mar 2021, 10:01 AM
Plaush
Plaush - avatar
0
Hima Ahh, I see. I’ll assign it to something :)
18th Mar 2021, 9:40 AM
Plaush
Plaush - avatar
0
Plaush The function is of type void so you can not assign it to anything .
18th Mar 2021, 9:46 AM
Hima
Hima - avatar
0
Hima Hmm, you’re right. It gives me an error
18th Mar 2021, 9:54 AM
Plaush
Plaush - avatar
0
void printOrder (string a, string b="Black_tea") { cout << "Black tea"; }
6th Oct 2021, 12:34 AM
Khomi TAKAYANAGI
Khomi TAKAYANAGI - avatar