loop supposed to exit on new line but doesn't

I want to store a big integer in an array. As a result i want the program to take each integer as a character store it in the array. If a new line or space is encountered it is meant that the number is finished.
But the problem is that this loop doesn't exit even when i hit space or enter, i don't understand why.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  
    int t;
    char ch;
    ch = getch();
    t = int(ch);
    while ( t > 46 && t < 58 )
    {
        vec[j++]=t;
        ch = getch();
        t = (int)ch;
        
    }

Last edited on
Your loop works fine for me. Are you sure that the problem is there? What compiler do you use?

Several notes:
1) getch() returns int, not char. Actually all C-style char manipulation functions returns int.
2) char is integral value. Only difference between char and int is it range.
3) Because of such thing as widening you can simply assign char to int and it won't even raise implicit conversion warning
4) Digits character code are not always [47, 57].

Simplified loop:
1
2
3
4
5
char ch = getch();
while ( isdigit(ch) ) { //Think about non-ASCII users
    vec[j++]=ch; //Widening
    ch = getch();
}
Topic archived. No new replies allowed.