Default arguments and function overloading.

Hey guys, I need a little advice on this exercise I have to do, it says that

"I must create a function that asks the user for a number, and then 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. Next, using function overloading, write a function that achieves the same result."

Now I think I've done it BUT I'm not sure that the function overloading is correct, it compiles and works but are there any logical problems?

Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  // Chapter 5 - Exercise 3
// 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.
// Next, using function overloading, write a function that achieves the same result.

#include <string>
#include <iostream>

using namespace std;

int askNumber(string prompt = "Enter a number: ");
int askNumber(int guess);

int main() {
	int number = askNumber("Enter a num: ");
	cout << "You entered: " << number << endl;

	int guess;
	cout << "Enter another number: ";
	cin >> guess;
	guess = askNumber(guess);
	cout << "You entered: " << guess << endl;
}

int askNumber(string prompt) {
	int guess;
	cout << prompt;
	cin >> guess;
	return guess;
}

int askNumber(int guess) {
	int guess2 = guess;
	return guess2;

}
Last edited on
Looks and ran ok. Also the overloading looks ok to me.
This code int askNumber(string prompt = "Enter a number: "); could just be int askNumber(string prompt);. The string that was originally there is unnecessary since you put line 16 in.
I think you are supposed to create two versions of the program with main being the same in both. The first version should have a function with a default argument to handle both if one argument is passed or if no argument is passed. The second version should have two functions with the same name, one taking one argument and the other taking no argument so that the result is the same as in the first program.
You're possibly right Peter but I'm not sure if it's supposed to be in a single version. I'm a little confused with the instructions because every way I try, it won't work because both the functions must have at least a different type. If I pass the number that the user inputs, it becomes a redundant parameter.

If I pass the string prompt then I'd have to concatenate the string and number using streams but I don't think I have to go that far.

This way, looks a lot better and it examples overloading better I think but the number parameter passed becomes unused. Any ideas? Thanks guys.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <string>
#include <iostream>

using namespace std;

int askNumber(const string &prompt = "Enter a number: ");
int askNumber(int number);

int main() {
	int number = askNumber("Enter a num: ");
	cout << "You entered: " << number << endl;

	askNumber(number);
	cout << "You entered: " << number << endl;
}

int askNumber(const string &prompt) {
	int guess;
	cout << prompt;
	cin >> guess;
	return guess;
}

int askNumber(int number) {
	int guess;
	cout << "Enter a number: ";
	cin >> guess;
	return guess;
}
Last edited on
... every way I try, it won't work because both the functions must have at least a different type. If I pass the number that the user inputs, it becomes a redundant parameter.

That's why I strongly suspect you should not use both function overloading and default arguments at the same time. Your teacher probably want you too see that there are two ways to do the same thing. The caller of the function(s) doesn't need to care because the function(s) are called the same way no matter which way you choose to implement the function(s).
I can confirm that both should be within the same program. How cryptic..:( I'm going to go with this. It's still function overloading but it just doesn't return a value.

Hopefully this is cool, thanks Peter for your help. :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <string>
#include <iostream>

using namespace std;

int askNumber(const string &prompt = "Enter a number: ");
void askNumber(int number);

int main() {
	int number = askNumber("Enter a num: ");
	cout << "You entered: " << number << endl;

	int number2 = askNumber();
	askNumber(number2);
	
}

int askNumber(const string &prompt) {
	int guess;
	cout << prompt;
	cin >> guess;
	return guess;
}

void askNumber(int number) {
	cout << "You entered: " << number << endl;
}
Topic archived. No new replies allowed.