Solved

Pointer

Posted on 2002-05-29
4
163 Views
Last Modified: 2010-04-02
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
Comment
Question by:rv_man
  • 2
4 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 7042607
rv_man,
Is this homework?

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

Expert Comment

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

Accepted Solution

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

by:rv_man
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 :)

Waiting for answer.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maze travler 6 66
Building cUrl in Windows v7.43.0 4 39
How to convert MFC::CString to UTF8 wchar_t* 10 383
find Speed using GPS latitude & longitude 4 89
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 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.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

749 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