Click to See Complete Forum and Search --> : Separating numbers...


viduus
August 31st, 2007, 12:18 AM
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

upashu2
August 31st, 2007, 06:00 AM
1. Read data as number from file. as

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.
num[i] = stringname[i] - '0'; // ascii code of zero (48) should be minus to get value of digit.