0
101 Mar 05, 2012 at 17:05

Hello everyone

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 same results.

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:

#include <iostream>
#include <string>

using namespace std;

string userInput(string prompt = “Please enter a number: “);

int main()
{

string input1 = userInput();

cout << input1 << “ - This is what you’ve entered 1st.\n\n”;

return 0;

}

string userInput(string prompt)
{

cout << prompt;

string number;

cin >> number;

return number;

}

#### 2 Replies

0
101 Mar 05, 2012 at 19:27

They mean that you can replace

string userInput(string prompt = "Please enter a number: ");


with something like

string userInput(string prompt);

string userInput()
{