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