CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2007
    Posts
    4

    Separating numbers...

    For the life of me I can't remember how to separate the individual numbers from a larger number. Essentially what I am trying to do is read a file that has a list of 1000 numbers back to back. I am probably going about it the wrong way, but so far what I have tried to do is input the whole thing into a string and then read the individual numbers by stringName[i].

    The problem from there occurs when I try to translate stringName[i] to an integer. Eg-> atoi(stringName[i])

    The resulting error says its an invalid conversion from char to const char.

    My question then is, is there a simpler way to separate a long list of numbers (eg-> 454545 ) into a linked list or even an array?

    eg:

    array[0] = 4
    array[1] = 5
    array[3] = 4

  2. #2
    Join Date
    Apr 2005
    Location
    Mumbai,India
    Posts
    185

    Re: Separating numbers...

    1. Read data as number from file. as
    Code:
            ifstream file(".......",ios::in);
            int i = 0, array[MAX];
            while(!file.eof())
               file>>array[i++];
    2. if In your case you want to read individual digits of a large number, and you have already read as a string then indiviual character of string will tell the digit of number.
    Code:
          num[i] = stringname[i] - '0'; // ascii code of zero (48) should be minus to get value of digit.
    Life is what u make it and u can make it more simple..........and woderful.
    Rate is what give and it give me pleasure.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured