Functions and Arguments

hi everyone,

i am having trouble figuring out how to modify this function in such a way that it will return a default prompt (such as "enter string:") if called without an argument.

1
2
3
4
5
6
7
8
9
static string promptForString(string prompt)
{
	string ret_val;
	
	cout << prompt;
	getline(cin, ret_val);
	
	return ret_val;
}


my understanding is that the parameters must be supplied if declared within the prototype... however, my compiler (xcode) is throwing error messages if I have not supplied necessary params (i.e., calling 'promptForString();' will generate an error). can someone please enlighten me on how to do this? I would appreciate any help you can provide.
Last edited on
1
2
3
4
5
6
7
8
static string promptForString(string prompt = "enter it: ");

...

static string promptForString(string prompt)
{
	...
}

Hope this helps.
closed account (1yR4jE8b)
You need default arguments:

1
2
3
4
5
6
7
8
9
static string promptForString(string prompt="Enter String: ")
{
	string ret_val;
	
	cout << prompt;
	getline(cin, ret_val);
	
	return ret_val;
}
Great! That is exactly what I was looking for. Thanks Duoas and darkestfright!
Topic archived. No new replies allowed.