0

We got this exercise in class:

Using default arguments, write a function that ask the user for a number and returns that number. The function should accept a string prompt from the calling code. If the caller does not 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.

The wording is confusing, but I get that I need two methods, one int and one string, to overload.

But after talking with my teacher, he explained that with the program a user should be able to enter and int 5 or a string "five", and the program should be able to determine if it is an int or string and output it.

I've looked up this exercise and there were several "solutions", none of which worked within my teacher's parameters.

Is it possible to create a program like what my teacher explained?

If not, does anyone have any other interpretations of the instructions?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • 1
    It;s not clear from this description what the second function should do. –  Feb 01 '17 at 18:31
  • 3
    your teacher's wording for this problem is horrible. – Erix Feb 01 '17 at 18:31
  • Related/dupe: http://stackoverflow.com/questions/24504582/how-to-test-whether-stringstream-operator-has-parsed-a-bad-type-and-skip-it – πάντα ῥεῖ Feb 01 '17 at 18:32
  • So does your teacher expect you to parse a more complicated input, for example, "twenty three thousand, eight hundred and fifty seven"? – barak manos Feb 01 '17 at 18:34
  • 2
    I half jokingly feel this would be better suited to English Language as the the biggest issue seems to be not the program itself but that the teacher has written the spec absolutely incomprehensibly. – Vality Feb 01 '17 at 18:35
  • _"I get that I need two methods, one int and one string, to overload."_ That's not how I see it. – Lightness Races in Orbit Feb 01 '17 at 18:36
  • 1
    So it sounds like the function just asks the user to input a number and then reads and returns that number. The difference is that one version of the function takes a string for a custom prompt, the other one uses a default prompt. – Greg Kikola Feb 01 '17 at 18:36
  • Also, what does "generic prompt" even mean??? – barak manos Feb 01 '17 at 18:36
  • @barak "Input Something: " –  Feb 01 '17 at 18:37
  • @NeilButterworth: So what does "If the caller does not supply a string for the prompt, the function should use a generic prompt" mean then? – barak manos Feb 01 '17 at 18:38
  • @barak What I said. Something that asks the user for some input. –  Feb 01 '17 at 18:39
  • @barakmanos Just that the function should use some default string as the prompt. Like "Enter a number". – Greg Kikola Feb 01 '17 at 18:39

1 Answers1

2

The teacher's wording translates into this:

#include <string>
#include <iostream>
#include <stdexcept>

/**
 * Prompts the user for an integer input on standard input, and
 * returns it. The prompt text is #prompt_text, or "Enter input please"
 * if not given.
 * 
 * @throws std::runtime_error if input is not an integer
 */
int promptForInput(const std::string& prompt_text = "Enter input please")
{
   std::cout << prompt_text << ": " << std::flush;

   int result;
   if (std::cin >> result)
      return result;
   else
      throw std::runtime_error("Could not understand your input");
}

int main()
{
   const int x = promptForInput();
   std::cout << "You entered: " << x << std::endl;

   const int y = promptForInput("Gimme intz lol");
   std::cout << "You entered: " << y << std::endl;
}

With overloading rather than default arguments, the function is like this:

/**
 * Prompts the user for an integer input on standard input, and
 * returns it. The prompt text is #prompt_text.
 * 
 * @throws std::runtime_error if input is not an integer
 */
int promptForInput(const std::string& prompt_text)
{
   std::cout << prompt_text << ": " << std::flush;

   int result;
   if (std::cin >> result)
      return result;
   else
      throw std::runtime_error("Could not understand your input");
}

/**
 * Prompts the user for an integer input on standard input, and
 * returns it. The prompt text is "Enter input please".
 * 
 * @throws std::runtime_error if input is not an integer
 */
int promptForInput()
{
   return promptForInput("Enter input please");
}

In both cases, the output of this program is like this:

[tomalak@andromeda ~]$ ./test2
Enter input please: 4
You entered: 4
Gimme intz lol: 62
You entered: 62

If you enter a non-integer, you'll get a non-descript process error instead:

[tomalak@andromeda ~]$ ./test2
Enter input please: lol
terminate called after throwing an instance of 'std::runtime_error'
  what():  Could not understand your input
Aborted (core dumped)

(N.B. I recommend a try/catch situation in main, but I couldn't be bothered.)


But after talking with my teacher, he explained that with the program a user should be able to enter and int 5 or a string "five", and the program should be able to determine if it is an int or string and output it.

I can't imagine that this is right; it seems pointless, and it has nothing to do with the quoted assignment. I believe you misunderstood this conversation.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • std::flush is probably redundant, as cin and cout are tied by default. –  Feb 01 '17 at 18:51
  • @NeilButterworth: Yeah probably – Lightness Races in Orbit Feb 01 '17 at 18:52
  • I wish I had misunderstood what he was trying to explain, but his exact words were "you should be able to enter 5 or five". – Proxy_Prone Feb 01 '17 at 19:01
  • 1
    @Proxy_Prone: That is not what the assignment text says, so either there is more that you did not quote, or the teacher is on some kind of drugs. Either way, I have demonstrated where the default arguments vs overloading comes into it. You may need to change what the function does with its input in order to satisfy the requirements, but that doesn't have anything to do with overloading. – Lightness Races in Orbit Feb 01 '17 at 19:10
  • No need for drugs. I've found a baseball bat can induce many of the same symptoms. – user4581301 Feb 01 '17 at 19:13