Help! - Exercise 3 - Ch. 5 - Beginning C++ Through Game Programming

7dde0ae58e7c544a8247d0768e4d1c18
0
jpaulez 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

Please log in or register to post a reply.

6eaf0e08fe36b2c23ca096562dd7a8b7
0
__________Smile_ 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()
{
    userInput("Please enter a number: ");
}
7dde0ae58e7c544a8247d0768e4d1c18
0
jpaulez 101 Mar 06, 2012 at 08:05

Ok.
Thanks for the help! :D