Public Group

# Little string2int conversion problem

This topic is 4292 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

int convStringToInt( std::string input ) // Only works for positive numbers
{
int outcome = 0;
int ciffer;

for( int i = 0; i < input.length(); i++ ) {
switch( input[input.length()-(i+1)] ) {
case '0' :
ciffer = 0;
break;
case '1' :
ciffer = 1;
break;
case '2' :
ciffer = 2;
break;
case '3' :
ciffer = 3;
break;
case '4' :
ciffer = 4;
break;
case '5' :
ciffer = 5;
break;
case '6' :
ciffer = 6;
break;
case '7' :
ciffer = 7;
break;
case '8' :
ciffer = 8;
break;
case '9' :
ciffer = 9;
break;
default :
return -1; // Error
}
outcome = outcome + ciffer*(10^i);
}

return outcome;
}


This is a little conversion function that is supposed to take a std::string and convert it to an int. Somehow, it doesn't work, it seems to give me wrong numbers back. A string "2" returns 20, a string "3" returns 30, but a string "32" returns 53, there just doesn't seem to be any logic behind it. I debugged it (on a piece of paper), and I can't seem to figure out what makes the function fail. Could someone please help me? Thanks in advance --Rob Spoel

##### Share on other sites
I think you should look closer at what the operator ^ does in c/c++ [smile]

You will be unpleasantly surprised..

##### Share on other sites
I haven't looked at the rest of it much, but:

outcome = outcome + ciffer*(10^i);
^ is a bitshift operator, it doesn't do exponents. #include <cmath> and use pow(10, i) instead.

Also, I'm assuming you're writing this function for an exercise/homework? There are better ways to do this (just to be sure :P )

EDIT: I'm slow :(

##### Share on other sites
Why would you make that? There are much better ways to do conversions.

#include <sstream>template<class ReturnType, class Type> ReturnType LexicalCast( Type Value ){	ReturnType ReturnValue;	std::stringstream ss;	ss << Value;	ss >> ReturnValue;	return ReturnValue}

##### Share on other sites
It's not homework, but you could see it as exercise :) I'm dusting off my programming skills a little, by writing small litle solutions for something like converting a std::string to an int. I followed your instruction, except using math.h, I remember using that a long time ago ;)

@ Scet:
I'm afraid that your code is far over my level of comprehension :(

##### Share on other sites
math.h is deprecated and supplied purely for backwards compatibility. You want to use cmath as originally stated.

##### Share on other sites
Quote:
 Original post by c4c0d3m0n@ Scet:I'm afraid that your code is far over my level of comprehension :(

It's really quite simple. Let's look at the string to int case first:

int LexicalCast( std::string str ) {  int returnValue; // The resulting int  std::stringstream ss; // A stringstream. If you have ever used any of the C++ streams (such as fstream or cin/cout), then this is a stream that reads/writes from/to a string.  ss << str; // Put in our string  ss >> returnValue; // Get the resulting int  return returnValue; // return the int.}

Now, with the use of templates, we can generalize this:

template< class ReturnType, class Type > ReturnType LexicalCast( Type value ) {  ReturnType returnValue;  std::stringstream ss;  ss << value;  ss >> returnValue;  return returnValue;}// Usage:std::string str = "394025";int value = LexicalCast< int >( str ); // convert string to int

Boost has an implementation of this.

Hope that helps.

jfl.

1. 1
2. 2
3. 3
Rutin
25
4. 4
5. 5
khawk
14

• 11
• 11
• 23
• 10
• 9
• ### Forum Statistics

• Total Topics
633650
• Total Posts
3013119
×