I’ve started to learn C++ and I’m reading this book (in the title).
I got stuck in this exercise.
I’ve done half of it but I can’t clearly understand what it’s asking me
to do next
This is what the exercise says:
Using default arguments, write a function that asks the user for a
number and returns that number.The function should accept a string prompt from the calling code.If the caller doesn’t supply a string for the prompt, the function
should use a generic prompt.(until here I understand)Next, using function overloading, write a function that achieves the
How could I make an overloaded function to make the same thing?
I thought (perhaps wrong) that overloaded functions were only created to
handle different types of data (int, string, bool…)
This is what I’ve done:
using namespace std;
string userInput(string prompt = “Please enter a number: “);
string input1 = userInput();
cout << input1 << “ - This is what you’ve entered 1st.\n\n”;
string userInput(string prompt)
cout << prompt;
cin >> number;
Please log in or register to post a reply.
They mean that you can replace
string userInput(string prompt = "Please enter a number: ");
with something like
string userInput(string prompt);
userInput("Please enter a number: ");
Thanks for the help! :D