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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C problem (with decimal point) 2

Status
Not open for further replies.

sundust1

Technical User
Jul 29, 2005
67
US
I have a spippet here that i'm trying to get working. My problem is that when i go to enter a value. Such as, 19.53 the decimal point does not show up. So my value is only multiplied by 19. I have tried the float method and get weird results. Is there a proper way to enter floating point numbers with code. I would like to keep this program and have it multiply decimal point value and give a answer in decimal point notation. Please help.
Sundust1
 
Here is the Snippet,


#include <stdio.h>
#include <math.h>
#include <float.h>

int main()
{
int Range;
int Step;
int Answer;
printf("Enter Step");
scanf("%d", &Step);

printf("%4s%21s%25s\n","Answer","Step","Range");
for(Range=0;Range<=255;Range++) {
Answer=Range * Step;

printf("%4d%21d%25d\n",Answer,Step,Range);
}

return 0;
}
 
Am I missing something here?

All your variables appear to be integers...

(You don't get decimal points with integers).
 
No, They are integers. I had to change them to floating point variables in order to get the source to work. This way when i multiplied by 0-255 i would get the answer in decimal point notation. Now i'm working on how i can get hex values to appear for my output. Any help on this would be appreciative.
Thanks,
Sundust1
 
printf("0x%08x", intVariable); will print like hex


scanf("%f", &floatVariable); if you want to capture floating point

hex can only represents unsigned 4 byte integers. If you're trying to represent your floating point in hex, use a union

union {
float in;
unsigned long out;
} data;

scanf("%f", &data.in);
printf("0x%08x", data.out);

NullT
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top