Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using a single byte character in calculation.. 2

Status
Not open for further replies.

johnfdutcher

Programmer
Dec 16, 2003
12
0
0
US
I have an input file to process written in another language.

One or more fields of (1) byte in length contain digits to be treated as integers.

Simply multiplying the variable name for the field by a constant gives erroneous mathmatical results:

char day_of_week = 7;
int meal_index = 0;

meal_index = 3 * day_of_week;
OR
meal_index = 3 * (int)day_of_week;

Both of the above give 156, where what is desired is 21 ???
 
It sounds like the variable
Code:
day_of_week
contains the ASCII value for the character '7', which is 52, instead of the number 7. Try doing this:

Code:
meal_index = 3 * (day_of_week - '0');

This will convert the ASCII character '7' to the number 7.

Dennis
 
Sounds like you want to take a character constant
and do some calculation.

Try this example setup ...

char cc = '7';
int iv;

sscanf(&cc,"%i",&iv);
printf("Result of mult. a char const: %i\n", iv*iv);

This will produce value 49

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top