Archived

This topic is now archived and is closed to further replies.

Question relating to C++

This topic is 5586 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Just wondering, why I have to postfix the " -''0'' " at the back of the expression?
   
//my constructor implementation 

HughInt::HughInt(const char *str) 
{ 
      for(int x=0;x<30;x++) 
            integer[x] = 0; 

for(int count = 30 - strlen(str), int i=0;count <30;count++,i++) 
{ 
      if(isdigit(str[i])) 
            integer[count] = str[i]-''0''; // <- why needed? 

} 
} 
  
data members in class definition: short integer[30]; The road may be long, wind may be rough. But with a will at heart, all shall begone. ~savage chant

Share this post


Link to post
Share on other sites
Because you want to translate the digit (That is in ascii) to an integer.
So if the character is ''0'', ''0'' - ''0'' will give you 0
And ''2'' - ''0'' will give you 2, Regardless of the ASCII value of ''0''


Goblineye Entertainment
The road to success is always under construction

Share this post


Link to post
Share on other sites
it is a way of getting the numeric value from the ascii value:

char ASCII
''0'' 48
''1'' 49
''2'' 50
''3'' 51
''4'' 52
''5'' 53
''6'' 54
''7'' 55
''8'' 56
''9'' 57

that code seems to translate a string representation of a number into the actual number (i.e. "12345" -> 12345).

Share this post


Link to post
Share on other sites