Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

math calculations

Posted on 1999-01-28
4
Medium Priority
?
231 Views
Last Modified: 2010-04-15
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).
so the answer = "05.07"
now, i tried the following and it keeps giving me a 0 for an answer.  please help.
/*-------------------------*/
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
Comment
Question by:zandy1
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
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 85

Expert Comment

by:ozo
ID: 1258477
value += m/60.0;
0
 
LVL 10

Expert Comment

by:viktornet
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 85

Expert Comment

by:ozo
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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 how to create, access, and change arrays in the C programming language.

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question