Is Your C++ Character Range Check Correct?

  • Thread starter Thread starter gringo
  • Start date Start date
  • Tags Tags
    C++ Range
Click For Summary
SUMMARY

The discussion focuses on the correct method to check if a character input falls within a specified range in C++. The user initially attempted to use ASCII values with a pointer variable, which led to runtime errors. The correct approach involves dereferencing the pointer using either *var or var[0] to access the character value. The recommended comparison syntax is if (*var <= 'K' && *var >= 'A', ensuring proper character range checks.

PREREQUISITES
  • Understanding of C++ pointer syntax
  • Knowledge of character data types in C++
  • Familiarity with ASCII values and character comparison
  • Basic debugging skills in C++
NEXT STEPS
  • Learn about C++ pointer dereferencing techniques
  • Research character data types and their operations in C++
  • Study ASCII value representations and their usage in comparisons
  • Explore common runtime errors in C++ and their debugging methods
USEFUL FOR

C++ developers, programming students, and anyone troubleshooting character range checks in C++ applications.

gringo
Messages
1
Reaction score
0
hi,
i have a little problem i m not able to check if an input character is in a specified range of characters. i.e is this char between A and Z let s say.
I tried to use the ascII but it didn t work out; no syntax errors at compile time but the error occurs at run time.
this is the code i used:
if(var<=75 && var >=65) //75 is letter K and 65 letter A
and var is a char *.

where is my problem?
THX
 
Physics news on Phys.org
var is a char*? The value of a char* is a pointer, so you're checking if a pointer is less than 75, which doesn't make sense. I can't remember, but you might want to check something like &var or var& or var* or something like that. Again, my memory might be off, but should you have two ampersands? What is the actual error you're getting?
 
First of all, a char* is a 32-bit hex value that points to some region of memory. To get the first value pointed to by var, you can either use *var or var[0].

If you want to compare single characters, you can use

Code:
if (*var <= 'K' || *var >= 'A')
without problems.
 

Similar threads

  • · Replies 16 ·
Replies
16
Views
11K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 18 ·
Replies
18
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 20 ·
Replies
20
Views
2K
  • · Replies 16 ·
Replies
16
Views
2K
Replies
5
Views
2K