Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# math calculations

Posted on 1999-01-28
Medium Priority
230 Views
i am having trouble getting an int divide with another value and answer in 2 decimal places. here's the problem:
the user answered "05:04" as time so:
char ans[5]="05:04";
i need to convert the min=.04 to .07 of 60 mins (the fraction within the 60 min whole).
/*-------------------------*/
char ans[5]="05:04";
float value=0.0;
int h,m;

sscanf(ans,"%2d:%2d",&h,&m);
value= h;
if(m >=0 && m < 60)
value= (value+(m/60));
/*----------------------------------------*/
the real problem is when i calculate (m/60), i get an answer of .0000000 instead of .066,  HOW DO I GET AN ANSWER WITH 2 DECIMAL PLACES?

zandy1
0
Question by:zandy1
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2

LVL 1

Accepted Solution

sergelebel earned 160 total points
ID: 1258476
zandy1

M and H must also be a type float, well actually ony m really needs to be.

change sscanf(ans,"%2f:%2f",&h,&m);

Serge..
0

LVL 84

Expert Comment

ID: 1258477
value += m/60.0;
0

LVL 10

Expert Comment

ID: 1258478
sergelebel, that's not the way you do it... here is how,,,

sscanf(ans,"%.2f:%.2f",&h,&m);

Hope this helps...

-Viktor
--Ivanov
0

LVL 84

Expert Comment

ID: 1258479
to get an ANSWER with 2 decimal places, you could say
printf("value=%.2f\n",value);

sscanf(ans,"%.2f:%.2f",&h,&m);
is meaningless. (i.e.: "undefined behavior")

int h,m; sscanf(ans,"%2f:%2f",&h,&m);
is also undefined.
To use sscanf(ans,"%2f:%2f",&h,&m) you would have to declare
float h,m;

but
int h,m; sscanf(ans,"%2d:%2d",&h,&m);
works fine, as long as you use (m/60.0) instead of (m/60)
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
###### Suggested Courses
Course of the Month4 days, 23 hours left to enroll