Jump to content


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


2 replies to this topic

#1 JPaulez

    New Member

  • Members
  • Pip
  • 2 posts

Posted 05 March 2012 - 05:05 PM

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 }:+()___ (Smile)

    Member

  • Members
  • PipPipPip
  • 169 posts

Posted 05 March 2012 - 07:27 PM

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: ");
}

Sorry my broken english!

#3 JPaulez

    New Member

  • Members
  • Pip
  • 2 posts

Posted 06 March 2012 - 08:05 AM

Ok.
Thanks for the help! :D





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users