• Log in or sign up for an account here: https://www.sythe.org/login

how to convert a string to an int?

Joined
Jan 31, 2007
Sythe Gold
0
Messages
447
Location
Argentina
well title says all. i have a string with numbers on it and want to convert it to an int so i can do calculations with the numbers.
 
well I am not a c++ expert by no means, but I heard it is similar to java and in java you do
Code:
int "name your int" = Integer.parseInt("string"[1]);
 
Le Monkey
PHP Shit Show 2k26
Potamus
Prove it!
Sythe Awards 2011 Winner
Tier 1 Prizebox
Tortoise Penis
Trole
UWotM8?
Wait, do you not have an Archer rank?
char *buf = "123";
int n = atoi(buf);
 
im using the atoi function for this, but i have 1 problem. the compiler gives me an error that says:
error C2664: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
and i tryed casting the var like this: atoi((const char *) var);
that way the program compiles, but gets stuck, so that cast might have changed the var's data.
 
Le Monkey
PHP Shit Show 2k26
Potamus
Prove it!
Sythe Awards 2011 Winner
Tier 1 Prizebox
Tortoise Penis
Trole
UWotM8?
Wait, do you not have an Archer rank?
You're declaring it as a char when it should be char*.
 
Ya well you are right. I solved it in another way:
Code:
#include <string>
#include <iostream>

using namespace std;

int main()
{
string a = "";
cout << "Enter string to convert: ";
cin >> a;
int b = atoi(a.c_str());
cout << a << " was converted into int: " << b << endl;
system("PAUSE");
}
now it works xD
thank you all!
 
duelarena.gg
Back
Top