Is Your C++ Character Range Check Correct?

  • Thread starter Thread starter gringo
  • Start date Start date
  • Tags Tags
    C++ Range
AI Thread Summary
The discussion focuses on a problem with checking if an input character falls within a specified range in C++. The user attempted to compare a character pointer using ASCII values but encountered runtime errors. It was clarified that a char* is a pointer, and the correct approach to access the character value is by dereferencing the pointer with *var or using var[0]. The suggested solution for the character range check is to use the condition if (*var <= 'K' && *var >= 'A'). The conversation emphasizes the importance of correctly dereferencing pointers when performing comparisons.
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.
 
I multiplied the values first without the error limit. Got 19.38. rounded it off to 2 significant figures since the given data has 2 significant figures. So = 19. For error I used the above formula. It comes out about 1.48. Now my question is. Should I write the answer as 19±1.5 (rounding 1.48 to 2 significant figures) OR should I write it as 19±1. So in short, should the error have same number of significant figures as the mean value or should it have the same number of decimal places as...
Thread 'A cylinder connected to a hanging mass'
Let's declare that for the cylinder, mass = M = 10 kg Radius = R = 4 m For the wall and the floor, Friction coeff = ##\mu## = 0.5 For the hanging mass, mass = m = 11 kg First, we divide the force according to their respective plane (x and y thing, correct me if I'm wrong) and according to which, cylinder or the hanging mass, they're working on. Force on the hanging mass $$mg - T = ma$$ Force(Cylinder) on y $$N_f + f_w - Mg = 0$$ Force(Cylinder) on x $$T + f_f - N_w = Ma$$ There's also...
Back
Top