String to int

S

- Steve -

I have an array of char values that I need to assign to a integer variable.
I tried casting it: integer = (int)charArray; but when I run it I get
incorrect results in integer. What is the correct way to cast a string to
an integer?
 
A

Artie Gold

- Steve - said:
I have an array of char values that I need to assign to a integer variable.
I tried casting it: integer = (int)charArray; but when I run it I get
incorrect results in integer. What is the correct way to cast a string to
an integer?

As it is somewhat unclear just what you're trying to do, why don't
you post your code?

[hint: casting is likely not what you want]

HTH,
--ag
 
K

Kevin Goodsell

- Steve - said:
I have an array of char values that I need to assign to a integer variable.
I tried casting it: integer = (int)charArray; but when I run it I get
incorrect results in integer. What is the correct way to cast a string to
an integer?

Avoid casts whenever possible. Avoid C-style casts always. This is a
good example of why: the code you tried to use does nothing even
remotely similar to what you want, but the cast prevents the compiler
from warning you of that fact. Had you used a C++ cast operator instead,
you would have found that you needed reinterpret_cast, and that would
have (or should have) served as a warning that it wasn't doing what you
wanted.

Next piece of advice: Don't use char pointers and arrays for strings.
Use std::string. It's much easier to deal with and much less
error-prone. Here's an example of what (I think) you want to do:

#include <iostream>
#include <string>
#include <sstream>

int main()
{
std::string str("4922");
std::istringstream strin(str);
int i;

strin >> i;

std::cout << str << " = " << i << std::endl;

return 0;
}

Think of 'strin' as 'cin' but using the string supplied to it as its
data source, rather than the program's standard input.

-Kevin
 
C

Chris Theis

- Steve - said:
I have an array of char values that I need to assign to a integer variable.
I tried casting it: integer = (int)charArray; but when I run it I get
incorrect results in integer. What is the correct way to cast a string to
an integer?

I can only agree with all the things Kevin already said so I won't waste my
breath. I'd just want to point out that a template function will give you
more freedom regarding the conversion you want to achieve:

////////////////////////////////////////////////////////////////////////////
//
template<typename T>
T FromStringEx( const std::string& s, bool& bSuccess)
// string conversion from string to typename T with a flag
// the represents success or failure.
//
// e.g: int d = FromString<int>( s );
//
// if you want to check whether the conversion was successful then you can
// use the bSuccess flag!
////////////////////////////////////////////////////////////////////////////
//
{
bSuccess = true;
std::istringstream is(s);
T t;
is >> t;
if( !is )
bSuccess = false;
return t;
}

HTH
Chris
 
T

Tom

- Steve - said:
I have an array of char values that I need to assign to a integer variable.
I tried casting it: integer = (int)charArray; but when I run it I get
incorrect results in integer. What is the correct way to cast a string to
an integer?

Other folks have answered your question. Let me just add that your
question is answered by Sections 38.2 and 38.3 of the FAQ:

http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-38.2

You may find the rest of the FAQ helpful too.

Also, let me reiterate that it's a bad idea to use an array of chars
instead of a std::string.

Best regards,

Tom
 

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

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top