Problems while printing List elements

Hello!

I got that "you got to be kidding me" type of problem :)

I'm trying to put in a list digits of a number which has more than 50 digits. I input the number like a string than convert its elements to int and put them in a list. I got this idiotic error when I wanted to printout the elements.

So, no matter what number I input, all elements of the list from the 49th to the 59th get an extra digit, and for that 10 elements the digits go from 0 to 9 respectively. This is an example of the printout:

1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 90 1
12 23 34 45 56 67 78 89 9 0
1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 9 0

(note: the first row represents the first 10 elements of the list-
or the first 10 digits of the number)

This is my code:
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
#include <iostream>
#include <string>
#include <list>

using namespace std;

int main(){
    
    string s="12345678901234567890123456789012345678901234567890123456789012345678901234567890";
    list<int> myList;
    for(int i=0;i<s.length();i++){
    char a=s[i];        
    myList.push_back(atoi(&a));
    }
    
    int counter(0);      
    while(!myList.empty())
    {
    cout<<myList.front()<<" ";
    myList.pop_front();
    counter++;
    if(counter==10){ cout<<endl; counter=0; }
    
    }

    system("PAUSE");
}
Last edited on
atoi expects a zero terminated string. Instead, push
(int)a-48

that solved the problem :) thanx ;)
Topic archived. No new replies allowed.