Digits To A Number

Hey!
I'm trying to convert digits separated from each other in a vector into a number, for example if I have 1, 3, and 9 in a vector I want to turn that into the number 139. But my code always seems to return the desired number minus 1, so it returns 138 instead of 139, and 222 instead of 223 and so on. Can you help me? Here is my code:

(I'm not using it for the purpose of just printing it out, because then I could've used the vector.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

int main(){
    vector<int> number = {1,3,0};
    int n = number.size()-1, sum = 0;
    for(int x=0;x<number.size();x++){
        sum += number[x]*pow(10,n);
        n--;
    }
    cout << sum << endl;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <vector>
#include <cmath>
#include <sstream>

using namespace std;

int main(){
    vector<int> number;

	number.push_back(1);
	number.push_back(3);
	number.push_back(0);

    stringstream num;    
   
    for(int x=0;x<number.size();x++){
        num << number[x];
    }

   int sum;
   num >> sum;
   cout << sum << endl;

   int z;
   cin >> z;

}
Last edited on
Thanks for your answer! Do you know why my version gave the desired number minus 1?
> sum += number[x]*pow(10,n);

Avoid floating point operations when integer operations are adequate.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <vector>
#include <limits>

int main()
{
    const std::vector<int> digits = { 1, 3, 0, 9, 8, 7, 2, 5 } ;
    if( digits.size() > std::numeric_limits<int>::digits10 )
    {
        std::cerr << "possible overflow\n" ;
        return 1 ;
    }

    int number = 0 ;
    for( int v : digits )
    {
        if( v<0 || v>9 )
        {
            std::cerr << "invalid decimal digit\n" ;
            return 1 ;
        }
        number *= 10 ;
        number += v ;
    }
    std::cout << number << '\n' ;
}

http://ideone.com/U3OvAr
If you could give me a brief explanation of your code that would be terrific :)
replace 10 with 10.0 and your code should work.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <vector>
#include <limits>

int main()
{
    const std::vector<int> digits = { 1, 3, 0, 9, 8, 7, 2, 5 } ;

    // if the vector conatains more digits than the guaranteed number of decimal digits
    // that can be represented by the type int, give up and report an error
    // see: http://en.cppreference.com/w/cpp/types/numeric_limits/digits10
    if( digits.size() > std::numeric_limits<int>::digits10 )
    {
        std::cerr << "possible overflow\n" ;
        return 1 ;
    }

    int number = 0 ;
    for( int v : digits ) // for each value in the vector
    {
        if( v<0 || v>9 ) // if the value is not that of a valid decimal digit
        {
            std::cerr << "invalid decimal digit\n" ;
            return 1 ;
        }

        number *= 10 ; // add a zero at the end of the current value
        number += v ; // and add the new digit

        // for example: if the current value of number is 1309
        // and the next digit is 8
        // 1309*10 + 8 == 13090 + 8 == 13098
    }

    std::cout << number << '\n' ;
}
@OP: basically you've got the coefficients of a polynomial
For {1,3,9} it means 1*x^2 + 3*x + 9
Evaluating for x=10 would give you the number


JLBorges's code is simply using Horner's rule in order to evaluate the polynomial
http://en.wikipedia.org/wiki/Horner_rule
Thanks for the answers everyone! I think I got it working now, however when inputting it into my program it doesn't work :( Must have done something else wrong
Topic archived. No new replies allowed.