[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SImple explain of the atof function ???

Posted on 2007-07-23
7
Medium Priority
?
935 Views
Last Modified: 2008-02-01
Just a simple questions about atof function?
Why and when would you need a function that convert char to double value???
What is the purpose??
Can you explain to me the following 2 programs
give me some situations when such a function need to be use.
thanks
 
 str = "  3336402735171707160320 ";  
 
Is "333640273......"  being treated as a string and not a value?
 
* atof example: sine calculator */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main ()
{
double n,m;
double pi=3.1415926535;
char sizeInput [256];
printf ( "Enter degrees: " );
gets_s ( sizeInput );       // WHY does this read in a degree value but declare as char to read up to 256 ???
n = atof ( sizeInput );
m = sin (n*pi/180);
printf ( "The sine of %f degrees is %f\n" , n, m );
return 0;
}
 
 
-----------------------------------------------------------------------------------------
 
 
// crt_atof.c
//
// This program shows how numbers stored as
// strings can be converted to numeric
// values using the atof function.

#include <stdlib.h>
#include <stdio.h>

int main( void )
{
    char    *str = NULL;
    double  value = 0;

    // An example of the atof function
    // using leading and training spaces.
    str = "  3336402735171707160320 ";
    value = atof( str );
    printf( "Function: atof( \"%s\" ) = %e\n", str, value );

    // Another example of the atof function
    // using the 'd' exponential formatting keyword.
    str = "3.1412764583d210";
    value = atof( str );
    printf( "Function: atof( \"%s\" ) = %e\n", str, value );

    // An example of the atof function
    // using the 'e' exponential formatting keyword.
    str = "  -2309.12E-15";
    value = atof( str );
    printf( "Function: atof( \"%s\" ) = %e\n", str, value );
0
Comment
Question by:mikedinh1234
7 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 19550114
gets_s ( sizeInput );       // WHY does this read in a degree value but declare as char to read up to 256 ???
It reads in a degree value because that's what you told the user to type in, and because you are multiplying by pi/180 before passing to sin which takes a radian value
char sizeInput [256]; is allocated so that we don't write past the end of the buffer  if the user types 250 characters
0
 
LVL 85

Expert Comment

by:ozo
ID: 19550130
str = "  3336402735171707160320 ";  
 
Is "333640273......"  being treated as a string and not a value?


The value of str is the address of the first character in "  3336402735171707160320 "
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 200 total points
ID: 19550232
>>>>> Why and when would you need a function that convert char to double value???
If a program reads text input from a file or from user input and tries to extract a number from that text (or parts of the text) in order to make calculations. Calculations usually were made with doubles. In C/C++ there is no standard functionality to make calculations with numbers stored in strings.

Note, if some input may or may not be a valid number it is always better to read it into a string and not let the input formatting functions fail to read. In the latter case you might loose some information which already was extracted from an input stream but failed to being converted to a double. Also, the status of the input stream goes to failed, hence it is invalid as long as the status was not resetted.

Regards, Alex
0
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.

 
LVL 22

Expert Comment

by:NovaDenizen
ID: 19550761
All your input is in the form of text until you convert it into something else.

gets(sizeInput) reads a series of characters from standard input until it sees a newline character, then it stores that sequence of characters into the sizeInput buffer.  After the last character, it puts a special zero character ('\0') to signal the end of the string.

At this point, you don't have a number loaded in the sizeInput buffer.  You just have a sequence of characters like '2', '.', '7', '1', '8', '2', '8', then a '\0' at the end.

atof() looks at a character string, calculates the floating point number that the character string represents, then returns that number, in this case 2.71828.
0
 

Author Comment

by:mikedinh1234
ID: 19550781
ozo  -  your statement make sense but it didn't exactly leaning toward a clear explain I was looking for
           what you said is all correct

itsmeandnobodyelse: yes you point out the idea where it would be use, so when it read in a file, then the value in the file is treated as string, i see. thanks

but can you guys explain two lines:

printf ( "Enter degrees: " );
gets_s ( sizeInput );      

// so when I enter 45, the program does not view the value as an int, but as a string???
0
 
LVL 85

Expert Comment

by:ozo
ID: 19550892
// so when I enter 45, the program does not view the value as an int, but as a string???
yes
0
 

Expert Comment

by:krusho
ID: 19577084
http://www.cplusplus.com/reference/clibrary/cstdlib/atof.html

I would say a good place you would use something like atof() is if you were parsing a file and you needed to use parts of that file as a number and not as a string.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 how to use strings and some functions related to them in the C programming language.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

831 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