Using default arguments, write a function that asks the user for a number and re
ID: 3678229 • Letter: U
Question
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 results. 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 results.Explanation / Answer
#include
#include
using namespace std;
/*The function has same name with different type parameters called as function overloading*/
//returns integer taking string as default argument is (Enter a number)
int askNum(const string &prompt = "Enter a number: ");
//returns integer taking integer as argument
int askNum(int guess);
int main() {
/*asks user number using askNum function with string default argument is =Enter a num and store into number variable*/
int number = askNum("Enter a num: ");
cout <<"You entered: " <<number;//using askNum(string)
cout <<"Enter another number: ";
cin >> guess;
//calling askNum function taking Integer type parameter
guess = askNum(guess);
cout <<"You entered: "<<guess;
}
int askNum(int guess) {
int guess2 = guess;
return guess2;
}
int askNum(const string &prompt) {
int guess;
cout<<prompt;
cin> > guess;
return guess;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.