And now to pick a bone.
@duoas
Instead of going into stuff that's probably going to pass over their head, wouldn't it be better to solve the problem with stuff they already know? Even if it means the code may be a but complicated in terms of understanding, bit at least it can be followed right? I mean the purpose here is not only to help people solve the problem, but to present the solution in a way that would help them understand? |
So.... you've got the gall to
use my own argument against me.
Let's see which one of us is using it fallaciously.
OP's code
1 2 3 4 5 6 7
|
int myFace;
cout << "Please rate my face from 1 to 10"
cin>> myFace;
if (myFace >7)
{
cout << "I am handsome";
}
|
I want an integer
in the range 1..10
Get the integer*
Compare the integer to 7 |
*OP specifically states that he wants to:
- Constrain the input to the range 1..10
- Constrain the input to valid numbers (because things go wonky when the user inputs anything but numbers)
Proof is what lune wrote: |
---|
How do I force users to enter integers within 1 to 10 only? It seems like with programs like this if I enter an alphabet instead of number it screws up the whole output... |
Duoas's code
1 2 3
|
std::istream& getline( std::istream& ins, int& n )
{
}
|
Small block of code containing a magic function to
read an integer from input. It is heavily commented,
but completely understanding it exactly is optional. |
1 2 3 4 5 6 7 8 9 10
|
int myFace;
cout << "Please rate my face from 1 to 10: ";
while (!getline( cin, myFace ) or (myFace < 1) or (myFace > 10))
{
cin.clear();
cout << "Try again: ";
}
if (myFace > 7)
|
I want an integer
in the range 1..10
Get the integer*
(until correct)
Compare the integer to 7 |
*Code to get the integer specifically address OP's problems:
- input must be in range [1,10]
- input must be an integer (and nothing else)
I don't permit the code to continue past this point until user has satisfied those two conditions. (Hmm, this might be important!)
Pratik's code
1 2 3 4
|
char myFace;
cout << "\n Please rate my face from 1 to 10";
cin>> myFace;
if ((myFace >'7')&&(myFace<='10'))
|
I don't want an integer -- I'd rather a character
in the range 1 to 10 (how is '10' a single character?)
Get the character*
Compare the character to '7' and '10' |
*Great, we're getting a single character, so:
-
cin won't go wonky on us if the user tries to input a non-number
- We have constrained the input to single-digit characters, instead of integers in the range 1..10
- Valid inputs now include
'a' and
'$'.
So... not only does it fail to constrain input to integers in the range 1..10, it now accepts
any character input, which means that the code that follows has to deal with it:
1 2 3 4 5 6
|
if ((myFace >'7')&&(myFace<='10'))
cout << "\n I am handsome";
else if ((myFace>='1')&&(myFace<='7'))
cout<<"\n Whatever, dude. ";
else
cout<<"\n Invalid Choice. ";
|
compare to '7' AND to check that input was received in range
still checking that input is in range
deal with input that was not in range |
The verdict?
1st wrote: |
---|
Instead of going into stuff that's probably going to pass over their head, wouldn't it be better to solve the problem with stuff they already know? |
It depends on how you apply it. Here you are applying it with a switch and bait logic.
- All programming involves using black boxes of code. How does
cin >> myFace
work? Who cares? The beginner can still use it just fine. Input is a difficult topic, and a lot of it will necessarily be opaque to a beginner.
- But a beginner
can easily understand something simple like:
attempt to get input
make sure it is in range
if any of those fail, complain to the user and try again
Hmm. Maybe
teaching a beginner how to think about getting input is part of the solution?
Duoas: Here's a common idiom that you should learn to get input
Pratik: Let's inexplicably change things to use characters, add in a whole bunch of conditions, and let bad inputs in. (OP's code didn't even do that!)
2nd wrote: |
---|
Even if it means the code may be a but complicated in terms of understanding, bit at least it can be followed right? |
Something like that nicely-commented function I posted? It doesn't do anything above a beginner's head (plays with strings, uses a stringstream, that failbit thing might be something to figure out some day).
Or, if you treat the function as incomprehensible, at least the part in
main() where the function is used is, while a little more complicated than a single
cin>>myFace
, it can be followed with little effort.
Duoas: Code is commented and follows a clean, easy to read pattern.
Pratik: Code is dense and does not explain why using chars is better than the ints, or why we are now having to deal with 'Invalid Choice's.
3rd wrote: |
---|
I mean the purpose here is not only to help people solve the problem, but to present the solution in a way that would help them understand? |
Duoas: Code solves both of OP's problems AND does it with an easy to read example of good input handling principles (use a loop, try to get input, match to range, repeat until good)
Pratik: Code fails to solve one of OP's problems AND introduces another problem AND spreads logic out AND changes behavior of the program AND doesn't work as advertised...
Hmm. QED.
I am sorry, but you asked for it. Welcome to being a jerk on the internet.
@
lune
I hope you get a good laugh factor out of this, and maybe learn something interesting and useful. In any case, sorry to make you put up with it.
@
ne555
I think that assigning a value on input failure is stupid. (But that's my opinion.)
¿couldn't ss>>n>>std::ws; be used? |
I hadn't thought of that.
Future incantations will be updated. (But I'm not going to press Edit for anything in this thread.)
Thanks!