Solved

# Pointer

Posted on 2002-05-29
159 Views
Can anyone made a code that creates array of chars mychars(first char has value '3' ,2 char:'.',3:'1',4:'4')and then converts it into float mynumber.
Tnx.
0
Question by:rv_man
• 2

LVL 30

Expert Comment

ID: 7042607
rv_man,
Is this homework?

It's agaisnt EE policy to do homework for questioners.
0

LVL 30

Expert Comment

ID: 7042610
Please provide some of your code, and we can assist you with specific questions.
0

LVL 86

Accepted Solution

jkr earned 100 total points
ID: 7042650
Use 'strtod()' or 'atof()':

/* STRTOD.C: This program uses strtod to convert a
* string to a double-precision value; strtol to
* convert a string to long integer values; and strtoul
* to convert a string to unsigned long-integer values.
*/

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

void main( void )
{
char   *string, *stopstring;
double x;
long   l;
int    base;
unsigned long ul;
string = "3.1415926This stopped it";
x = strtod( string, &stopstring );
printf( "string = %s\n", string );
printf("   strtod = %f\n", x );
printf("   Stopped scan at: %s\n\n", stopstring );
string = "-10110134932This stopped it";
l = strtol( string, &stopstring, 10 );
printf( "string = %s", string );
printf("   strtol = %ld", l );
printf("   Stopped scan at: %s", stopstring );
string = "10110134932";
printf( "string = %s\n", string );
/* Convert string using base 2, 4, and 8: */
for( base = 2; base <= 8; base *= 2 )
{
/* Convert the string: */
ul = strtoul( string, &stopstring, base );
printf( "   strtol = %ld (base %d)\n", ul, base );
printf( "   Stopped scan at: %s\n", stopstring );
}
}

/* ATOF.C: This program shows how numbers stored
* as strings can be converted to numeric values
* using the atof, atoi, and atol functions.
*/

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

void main( void )
{
char *s; double x; int i; long l;

s = "  -2309.12E-15";    /* Test of atof */
x = atof( s );
printf( "atof test: ASCII string: %s\tfloat:  %e\n", s, x );

s = "7.8912654773d210";  /* Test of atof */
x = atof( s );
printf( "atof test: ASCII string: %s\tfloat:  %e\n", s, x );

s = "  -9885 pigs";      /* Test of atoi */
i = atoi( s );
printf( "atoi test: ASCII string: %s\t\tinteger: %d\n", s, i );

s = "98854 dollars";     /* Test of atol */
l = atol( s );
printf( "atol test: ASCII string: %s\t\tlong: %ld\n", s, l );
}

Axter, having seen the other Qs posted by rv_man, I don't think it's homework...
0

Author Comment

ID: 7042734
First of all thats not my homework. I just "playing" with
direct3d and to "play" with it I must know how pointer and stl works. I "prof." in java but I just began learning c++. I know what pointer is but I don't know how to work with it :)
Now my question:
I have
int iInTextLen = 20;
char *in_text = new char[iInTextLen];
float iMyNumber;

I have in WM_CHAR messege:
in_text[strlen(in_text)]=(char)wParam;
So, every time when I pressing... ok.

I running my programm and typing for example "new vector 60.0f 60.0f 0.5f 1.0f 0xffff0000"
So, it goes to in_text.
Now I want ,for example, place 60.0f from in_text in iMyNumber.

Thats it. I asking it because I don't even know how to do it: with pointer or with help of stl.

Sorry for my English :)

0

## Featured Post

### Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

#### Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!