Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String -> int Conversion ? 2

Status
Not open for further replies.

Andycapp

Programmer
Oct 8, 2001
4
0
0
NO
Hello fellow programmers...

I would like to know if there's any good way to convert a string to int ?

An example:
void main{
string Numbers;
string Temp;
int Number;

Numbers = "12345";
Temp.assign(Numbers,0,1); //assigns the first number in the string to Temp
cout << &quot;Temp is : &quot; << Temp; getch(); //Temp here displays &quot;1&quot;
Number = Temp; //this doesn't seem to work

cout << &quot;The number is : &quot; << Number; getch();

}

Can anyone help me with this ??
 
Take your pick. :->

Number = StrToInt(Temp);
Number = StrToInt64(Temp);
Number = StrToFloat(Temp);
etc.
 
Thanks for your effort, Maladon!

But I can't get any of these commands to work...is there some header-file I'm missing ?

I'm using Borland C++ v5.02 ..

Your help is greatly appreciated !


 
For the older version, look at itoa. It is part of the standard library (stdlib.h). For long integers, look at ltoa.
Code:
#include <stdlib.h>
#include <iostream.h>

int main(void)
{
   int number = 12345;
   char string[25];
   int base = 10;

   itoa(number, string, base);
   cout << &quot;integer = &quot; << number << &quot; string = &quot; << string << endl;
   return 0;
}
The above code converts the integer from base 10 to a null-terminated character string. (Base can be any number from 2 to 36). James P. Cottingham

I am the Unknown lead by the Unknowing.
I have done so much with so little
for so long that I am now qualified
to do anything with nothing.
 
Opps, I'm not awake yet. For the reverse, try atoi, atol, scanf, and strtod. James P. Cottingham

I am the Unknown lead by the Unknowing.
I have done so much with so little
for so long that I am now qualified
to do anything with nothing.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top