B
Beginner
can you convert a string to an int in c++?
string temp;
string temp;
Use the atoi function:
int atoi(const char *string);
Example:
int n=atoi(temp.c_str());
For example:can you convert a string to an int in c++?
string temp;
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
Salil said:Thanks for pointing that out,
I have corrected the code , it should be :
str+=48;
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.