how to take numbers from int and put them on table 1 by one

hi.... im trying to figure an easy way, how to take numbers from int and put them on table 1 by one. for example 12345 would go to table as {5,4,3,2,1}.
hopefully someone could help me on this :)


Answer this question

how to take numbers from int and put them on table 1 by one

  • Carlos Figuera.

    What have you tried so far

    Thanks,
    Ayman Shoukry
    VC++ Team


  • Rodolfo Montero

    peteyy wrote:
    i wasnt able to make that work :(

    You couldn't make my code work Really Because I tested it before I posted it here. What compiler are you using Did you include these headers

    #include <iostream>

    #include <string>

    #include <algorithm>



  • zille

    I think this is what you want:

    int toint(int val)

    {

    return val - '0';

    }

    int _tmain(int argc, _TCHAR* argv[])

    {

    std::string val;

    std::cin >> val;

    std::reverse(val.begin(), val.end());

    std::vector<int> array(val.length(), 0);

    std::copy(val.begin(), val.end(), array.begin());

    std::transform(array.begin(), array.end(), array.begin(), toint);

    return 0;

    }



  • spattewar

    i wasnt able to make that work :(

    is there a simple way to move numbers from Char to  an  Integer table.... like 12345 would put 5 to int[0]spot, 4 would go to int[1] spot... and so on.

    help would be appriciated, since im really stuck on this :(

  • tulip-pcsc

    So you can learn by example.  Not as elegant as Marius' answer ... I don't use the Standard C++ lib that deeply.

    int main()
    {
        string s;
        cout << "Enter a string of digits: " << endl;
        getline( cin, s );
        vector<int> int_table;
        for( size_t i = 0; i < s.length(); i++ )
        { 
            if( s[ i ] >= '0' && s[ i ] <= '9' )
            {
               int_table.push_back( s[ i ] - 48 ); // subtract ascii of '0' to 0-base values
            }
            else
            {
                cerr << "Ignoring invalid character: " << s[ i ] << endl;
            }
        }

        for( size_t i = 0; i < int_table.size(); i++ )
        {
            cout << "int_table[" << i << "]=" << int_table[ i ] << endl;
        }
    }


  • Anthony von LA

    Hi.. i tried this with string... so i would read the given number into string.. then move then re arrange it bacwards... and then putting it into int. here's the code:

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

    using namespace std;

    int main() {

    string numbers;
    string temp;
    int arranged[20];

    cout<<"give numbers: ";
    cin>>numbers;

    for(int i=numbers.length(), j=0; i>0; i--, j++) {
    std::istringstream help(numbers.substr(i-1,1));
    help >> arranged[j];
    cout<<arranged[j];
    }
    }

    but the problem with this code is that i cant multiple those re arranged numbers one by one.... for that i tried this code :

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

    using namespace std;

    int main() {

    string numbers;
    string temp;
    int total;
    int arranged[20];
    int multiple []={7,3,1,7,3,1,7,3,1,7,3,1,7,3,1,7,3,1,7,3};

    cout<<"give a number: ";
    cin>>numbers;

    for(int i=numbers.length(), int j=0; i>0; i--, j++) {
    std::istringstream help(numbers.substr(i-1,1));
    help >> arranged[j];
    total+= help*multiple[j];
    cout<<arranged[j];
    cout<<total;
    }

    return 0;
    }

    it gives error : C2676: binary '*' : 'std::istringstream' does not define this operator or a conversion to a type acceptable to the predefined operator




  • how to take numbers from int and put them on table 1 by one