Checking Digit Range in C++ - Any Suggestions?

  • Context: C/C++ 
  • Thread starter Thread starter madmike159
  • Start date Start date
  • Tags Tags
    C++
Click For Summary

Discussion Overview

The thread discusses a C++ programming issue related to validating user input for a number between 1 and 9. Participants explore various aspects of the code, including logic errors, debugging techniques, and the proper use of functions.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant shares their initial code and states it is not functioning as expected.
  • Another suggests using print statements to debug the logic and points out the incorrect use of the expression (1 <= NumPlayers <= 9), recommending it be split into two conditions.
  • A different participant emphasizes the importance of displaying input and results to facilitate debugging.
  • Another suggests using a debugger to step through the code and monitor variable values.
  • One participant requests clarification on what "not working" means, asking for specific error messages or unexpected results.
  • A later reply indicates that the initial problem has been resolved, thanking others for their help.
  • Some participants assert that the expression (1 <= NumPlayers <= 9) is correct, while others argue it is semantically incorrect in programming contexts.
  • One participant explains that isdigit() is misused, as it should be called with a character, not an integer.

Areas of Agreement / Disagreement

Participants express differing views on the correctness of the expression (1 <= NumPlayers <= 9) and the use of isdigit(). There is no consensus on these points, and the discussion remains unresolved regarding the best practices for input validation in C++.

Contextual Notes

Participants highlight limitations in the original code, including potential misunderstandings of function usage and logical expressions. There are also unresolved issues regarding the specific nature of the initial problem described as "not working."

madmike159
Gold Member
Messages
369
Reaction score
0
I want the user to enter a number from 1 to 9, then check its in that range and a number.
This is the code I have, but its not working.
Code:
	int NumPlayers = 0;
	do
	{
		cout << "Please enter a interger from 1 to 9." << endl;
		cin >> NumPlayers;
	}
	while(isdigit(NumPlayers)  && (1 <= NumPlayers <= 9));

Any Suggestions?
 
Technology news on Phys.org
madmike159 said:
Any Suggestions?
Print statements!
print out NumPlayers and your logic and see that everything is evaluating as it should be.

Also, it's been a while, but I'm pretty sure you can't do:
(1 <= NumPlayers <= 9)

You have to break it up:
((1<=NumPlayers) && (NumPlayers<=9))
 
In situations like this, it's instructive to insert code that displays your input and the results of your tests explicitly:

Code:
int NumPlayers = 0;
do
{
    cout << "Please enter a interger from 1 to 9." << endl;
    cin >> NumPlayers;
    cout << "You entered " << NumPlayers << "." << endl;
    if (isdigit(NumPlayers))
        cout << "NumPlayers is a digit." << endl;
    if (NumPlayers >= 1)
        cout << "NumPlayers is >= 1." << endl;
    if (NumPlayers <= 9)
        cout << "NumPlayers is <= 9." << endl;
}
while(isdigit(NumPlayers)  && (1 <= NumPlayers) && (NumPlayers <= 9));

This technique often enables you to find the solution faster than by posting to the Internet and waiting for responses. :smile:
 
or better yet, use a debugger.
Set a breakpoint at the top, set watches on the variables you are interested in and then step through each line.
 
madmike159 said:
but its not working.

Just another tip: In general, it really really helps people if you say exactly what you mean by "not working."

Does the program fail to compile? If so, what error messages did you get?

Does the program seem to compile OK, but doesn't give correct results when it runs? If so, what results did you expect and what did you actually get?

Or did your monitor screen melt down, or did you get electric shocks to your fingers through the keyboard, or...? :rolleyes:
 
Ok thanks, I'll remember that next time. I fixed the problem now. Thanks for the help.
 
madmike159 said:
Code:
(1 <= NumPlayers <= 9)

Actually this is a correct expression :wink:
 
Borek said:
Actually this is a correct expression :wink:

I wonder if the compiler optimizes it to if(true) { ... }
 
The user interface of your program --
Code:
cout << "Please enter a interger from 1 to 9." << endl;
-- could use some editing. Note spelling of integer.

"Please enter an integer from 1 to 9."

To expand on what Borek and DavidSnyder said, the expression 1 <= NumPlayers <= 9 always evaluates to true, regardless of the value of NumPlayers.

This expression is evaluated as if it were written as ((1 <= NumPlayers) <= 9).

The logical expression 1 <= NumPlayers will evaluate to true (1) or false (0), depending on the value of NumPlayers.

The expressions (0 <= 9) and (1 <= 9) both evaluate to true (1).

The moral of the story is that the continued inequality that is used in mathematics is syntactically correct but semantically incorrect in all programming languages based on C.
 
  • #10
In addition to all problems already mentioned, the call to isdigit() does not do what you think it does.

isdigit() is normally called with a 'char' parameter and it returns nonzero if the parameter is between '0' and '9' (or, in ASCII, between 0x30 and 0x39). Your NumPlayers is an 'int'. Therefore it will already have been converted from char to integer. There's no need to call isdigit.
 

Similar threads

Replies
52
Views
5K
Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K