Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,933 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,697 people online right now. Registration is fast and FREE... Join Now!




Converting integer to array

 
Reply to this topicStart new topic

Converting integer to array

janandrada
6 Jan, 2008 - 02:21 AM
Post #1

New D.I.C Head
*

Joined: 27 Sep, 2007
Posts: 37


My Contributions
How can i store integers digit by digit in an array element?

i have my code but i'm not able to find the error;
input is two integers and have to store in separate array
CODE

  int num1, num2, x;
  
    cout << "Type 2 integers seperated by space: ";
    cin>>num1>>num2;
    
    int count1 = 0;
    while (num1 > 0)
    {
        num1 = num1/10;
        count1++;
    }
    int count2 = 0;
    while (num2 > 0)
    {
        num2 = num2/10;
        count2++;
    }
    
    int array1[count1];
    int array2[count2];
    int elem = 0;
    
    for(int i=0;i<count1;i++){
            array1[elem++]=num1%10;
            num1=num1/10;
            }
            
    for(int i=0;i<count2;i++){
            array2[elem++]=num2%10;
            num2=num2/10;
            }

User is offlineProfile CardPM
+Quote Post

Bench
RE: Converting Integer To Array
6 Jan, 2008 - 02:49 AM
Post #2

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 686



Thanked: 24 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
The easiest way would be to read it in as a string in the first place - then you have the advantage of the data already being in an array-like format.

You can then iterate through the string, and extract digits as needed to obtain the digits. converting from character values to integer values need be no more complicated than subtracting character zero '0'

CODE
#include <iostream>
#include <string>

int main()
{
    using namespace std;

    string str = "1234567";
    int num = str[2] - '0';

    cout << "Value of digit at position 2 is " << num;
}


There are also various neat tricks involving stringstream, which allow you to convert between string and numerical types.
User is offlineProfile CardPM
+Quote Post

janandrada
RE: Converting Integer To Array
6 Jan, 2008 - 03:09 AM
Post #3

New D.I.C Head
*

Joined: 27 Sep, 2007
Posts: 37


My Contributions
also, i have to add, multiply or subtract the the two array..

is it possible to use string?
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Converting Integer To Array
6 Jan, 2008 - 04:39 AM
Post #4

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,351



Thanked: 51 times
Dream Kudos: 25
My Contributions
Not really (well, using ASCII values), but you can easily convert the array elements to integers once they've been placed in an array.
User is offlineProfile CardPM
+Quote Post

Bench
RE: Converting Integer To Array
6 Jan, 2008 - 03:25 PM
Post #5

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 686



Thanked: 24 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
Not directly, you must convert them first

CODE
#include <string>
#include <sstream>

bool to_int(const std::string& str, int& num)
{
    return std::stringstream( str ) >> num;
}

int main()
{
    std::string num("1234");
    int i;

    if( to_int( num, i ) )
    {
        //Converted successfully from string to int.
    }
}

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 03:29PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month