Hey all, I have this function to make and it has to be like this.
What would the reason of having a prompt parameter?Code:int ReadScore( string prompt )
Thanks in advance!
Hey all, I have this function to make and it has to be like this.
What would the reason of having a prompt parameter?Code:int ReadScore( string prompt )
Thanks in advance!
So that you could err... prompt the user for input, yet be able to reuse the function with different wording for the prompt.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
But can't you already do that without a parameter?
you certainly can, but if you don't want your code littered with stuff like this:
when you could instead have this:Code:std::cout << "Enter a number:"; int score = ReadScore();
it keeps the code a bit more readable (in my opinion), and you know exactly what input the prompt is for.Code:int score = ReadScore("Enter a number:");
keep in mind that the snippet you provided passes the prompt string by value, and this may not be desirable, if the prompt string is very long. it's best to pass by (const) reference so that no copy is made, saving CPU time and memory.
like this:
Code:int ReadScore(const string& prompt)
I would if I could but this is a homework assignment. :b but thanks again!