2

I made this humble function to transform a string into an integer. I can't figure out how this function returns the number (that it's supposed to return) minus 1.

For example, if it has an argument of "105", it will return the number 104. There may be another way to do this easy task, but I really want to know where does my code fail in order not to make this mistake again. It is C++

int convertnumber(string cad)
{
    int e = 0;
    int number = 0;
    for (int i = cad.length() - 1; i >= 0; i--)
    {
        number += (cad[i] - '0') * (pow(10, e));
        e++;
    }
    return number;
}
  • What language is this code written in? I've tried C++ but I can't replicate your issue. –  Sep 01 '15 at 01:34
  • 1
    It works fine. I'm guessing some sort of 'operator error'. – Steve Wellens Sep 01 '15 at 02:07
  • 2
    I think the problem should be the "pow(10, e)", sometimes the result of pow(10,2) will return 99 instead of 100, you try to write your own pow function, then problem will be solved – Anson Tan Sep 01 '15 at 02:11
  • It would be easier to scan the string from 'left-to-right'. Multiply by 10 and add the digit for each iteration. – Brett Hale Sep 01 '15 at 02:47

3 Answers3

4

I think the problem should be the "pow(10, e)", sometimes the result of pow(10,2) will return 99 instead of 100, you try to write your own pow function, then problem will be solved. please read this, Why does gcc compiler output pow(10,2) as 99 not 100?

#include <iostream>
using namespace std;

int pow10(int n){
  if (n == 0) {return 1;}
  int result = 1;
  for(int i = 0 ; i < n ; i++){
   result = result*10;
  }
 return result;
 }

int convertnumber(string cad)
{
   int e = 0;
   int number = 0;
   for (int i = cad.length() - 1; i >= 0; i--)
   {
     number += (cad[i] - '0') * pow10(e);
     e++;
  }
  return number;
  }

int main() {
cout<<convertnumber("105")<<endl;
return 0;
}
Community
  • 1
  • 1
Anson Tan
  • 1,256
  • 2
  • 13
  • 34
  • 3
    *"pow(10,2) will return 99 "* `pow` returns a `double`, which wouldn't be 99 but might be 99.99999999238 or some such.... but when that's multiplied by `(cad[i] - '0')` and converted to an `int` for addition to `number` - that's when it may be rounded down to one less than hoped. If it was actually 99, it'd be `cad[i] - '0'` less than hoped. More generally, multiplying number by 10 before adding `cad[i] - '0'` is easier and faster than looking up an integral `pow` value. – Tony Delroy Sep 01 '15 at 02:53
1

C++ 11 already has a lot of tools for that king of thing if you include the string library (#include <string>).

A good example is the famousstd::stoi(myString) that you can use as a convert function for string to an int. But you can also look for stol, stoll, stoul, stof, stof.

You can find more information about this topic here.

Now, talking about your problem.

I tested your code and the program worked fine, maybe your error is because you need to clean the buffer or something like that.

Gabriel
  • 763
  • 1
  • 10
  • 28
  • *"I tested your code and the program worked fine"* - the problem depends on the implementation of `pow`, and could only be expected to fail for a subset of exponents, so light testing on one compiler/system is not necessarily going to reproduce the fault. – Tony Delroy Sep 01 '15 at 03:24
0

The problem is that pow returns a floating-point number, and floating-point arithmetic has limited precision.

You could write an integer power function, but you don't actually need to keep computing the same powers of ten; just multiply your ”position factor” by ten on each iteration:

int convertnumber(string cad)
{
    int e = 1;
    int number = 0;
    for (int i = cad.length() - 1; i >= 0; i--)
    {
        number += (cad[i] - '0') * e;
        e *= 10;
    }
    return number;
}
molbdnilo
  • 64,751
  • 3
  • 43
  • 82