string to int

P

Paul

Use the atoi function:
int atoi(const char *string);

Example:
int n=atoi(temp.c_str());
 
J

Jack Klein

Use the atoi function:
int atoi(const char *string);

Example:
int n=atoi(temp.c_str());

Please don't ever recommend the ato... functions to anyone in C or
C++. They produce undefined behavior if the conversion results in a
value outside of the range of the destination type. Also, they do not
provide a way to tell if a returned value of 0 is due to a string
representing the value 0 or an invalid string that could not be
converted at all.

The standard library strto... functions were added a long time ago to
overcome these problems. They have defined behavior with any input,
other than a null pointer.
 
D

darek

Beginner napisa³(a):
can you convert a string to an int in c++?

string temp;
For example:

#include <iostream>
#include <sstream>

int main( )
{
std::string a("321");
int b;
std::stringstream ss(a);
ss >> b;
std::cout << b;
return 0;
}
 
S

Salil

You can declare a string say 6 bytes in length and insert all the
digits of the number one at a time into the string.
For example :
#include<iostream.h>
int main()
{
char str[6];
int a=12345;
for(int i=0;a!=0;i++)
{
str=a%10;
a=a/10;
}
//this will make str : 54321
//after that u can reverse the string using strrev()
//and that's it!
return 0;
}
 
P

Peter_Julian

| You can declare a string say 6 bytes in length and insert all the
| digits of the number one at a time into the string.
| For example :
| #include<iostream.h>

#include <iostream>

| int main()
| {
| char str[6];

is not a string, its a char array[]

| int a=12345;
| for(int i=0;a!=0;i++)
| {
| str=a%10;
| a=a/10;
| }
| //this will make str : 54321
| //after that u can reverse the string using strrev()
| //and that's it!
| return 0;
| }

You should study the std::string type and the std::stringstream classes.
These dynamic containers include powerful algorithms, iterators and
overloaded operators.

You'll never, ever use a char array again except when absolutely
neccessarry.
 
K

Karl Heinz Buchegger

Salil said:
You can declare a string say 6 bytes in length and insert all the
digits of the number one at a time into the string.
For example :
#include<iostream.h>
int main()
{
char str[6];
int a=12345;
for(int i=0;a!=0;i++)
{
str=a%10;
a=a/10;
}
//this will make str : 54321


Aehm. No. This will not make str "54321" no most systems.
 
S

Salil

Thanks for pointing that out,
I have corrected the code , it should be :
#include<iostream.h>
#include<string.h>
int main()
{
char str[6]=" ";
int a=12345;
for(int i=0;a!=0;i++)
{
str=a%10;
a=a/10;
str+=48;
}
cout<<"\nNew string :"<<str;
//str will be 54321
char *stn=&str[0];
strrev(stn);
cout<<"\nReversed string :"<<str;
//here str is reversed and it becomes : 12345
return 0;
}

please note that this program was written in ANSI C++ , that's why i
used the char array
~Salil
 
K

Karl Heinz Buchegger

Salil said:
Thanks for pointing that out,
I have corrected the code , it should be :

You have corrected it for the case that the OP's system
uses ASCII (most, but not all, do).

Now I show you how you can achieve the same thing which
works in any case, no matter if the OP'S system uses
ASCII or not (Just one line needs to be changed)

Instead of


you write

str += '0';

This also has the benefit, that it gets clearer
what is going on.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top