Reading One digit at a time

Ok, So I want to do the following, Please help me out.
I want to input 150 1 digit integers, from 0-9 and I don't want to put spaces between them while inputing, like if I input a 150 digit integer, the compiler should take it as 150 1 digit integers. I'm not sure how to accomplish that.


1
2
3
int a[150];
for(int i=0;i<150;i++)
cin>>a[i];


If I try to do it with above code, the compiler needs a space to know when the value of i is incremented. But I want to avoid spaces for my task. Please help!!
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

int main()
{
    const int NDIGITS = 30 ;
    int number[NDIGITS] = {0} ;

    for( int i = 0 ; i<NDIGITS ; ++i )
    {
        char digit ;
        const char zero = '0' ;
        const char nine = '9' ;
        std::cin >> digit ;
        if( digit >= zero && digit <= nine ) number[i] = digit - zero ;
        else std::cerr << digit << " is not a decimal digit (set to zero)\n" ;
    }

    for( int v : number ) std::cout << v ;
    std::cout << '\n' ;
}

http://coliru.stacked-crooked.com/a/4664f3f222b106e4
Thank you!
Topic archived. No new replies allowed.