Default Parameters in C++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Default Parameters in C++

Default arguments Your usual order at your favorite cafe is black tea, which the waiter brings you by default. Today, however, you are with your friend and it is his first time there. Obtain your friend's order in one word as input, and place the order for both of you. Complete the function so that it will output "Black tea" by default (without an argument) and then your friend's order as an argument. Sample Input Americano Sample Output Black tea Americano Function should have one default argument set to "Black tea". Don't forget about the line break after the output. my code: #include <iostream> #include <string> using namespace std; //complete the function with one default argument void printOrder(string myOrder = "Black tea", string friendOrder = "") { cout << myOrder; } int main() { //getting coffee type string friendOrder; cin >> friendOrder; printOrder(friendOrder); return 0; }

16th Feb 2022, 4:29 AM
Brian
2 Answers
+ 2
Brian Function should have only one parameter with default value and call that function two times 1 with input value and 1 without input value. void printOrder (string order = "Black tea") { cout << order << endl; } Now call this two times: printOrder (); printOrder (friendOrder);
16th Feb 2022, 5:40 AM
A͢J
A͢J - avatar
0
This lesson doesn't really test default argument very well. You can pass it without using default argument. #include <iostream> #include <string> using namespace std; //complete the function with one default argument //I did not use any...🙄 void printOrder(string order){ cout << "Black tea"<< "\n" << order; } int main() { //getting coffee type string friendOrder; cin >> friendOrder; printOrder(friendOrder); return 0; }
16th Feb 2022, 5:57 AM
Bob_Li
Bob_Li - avatar