Exiting while loop

I don't know why, but my program keeps exiting while loop ( the one inside "if" statement)

the input file contains "abc abb ca" and this should put "abc" and "abb" into one (stack_arr[0]) stack and "ca" into different one ( stack_arr[2])
1
2
3
4
5
6
7
8
9
10
11
fin.get(ch);
while (fin){
        if((int)ch>=97 && (int)ch<=122){
            x=(int)ch-97;
            while(fin && (int)ch>=97 && (int)ch<=122){
                stack_arr[x].push(ch);
                fin.get(ch);
            }
        }
    fin.get(ch);
    }


but it only puts all the 'a' in one stack, all the 'b' in another and so on... i found out that in inner while loop it enters, does one cycle, but does not stay. I cannot understand why is it so.
Last edited on
How are you entering these characters? How are you handling the end of line characters, if present?

Also I recommend that you use character constants instead of their numeric value, ie 'A' instead of 65.

Line 4: One of two things is going to cause the while loop to exit. 1) fin's bad bit being set, or 2) ch is not a lower case character.

Since we got to line 4, we know fin is good at line 1 and therefore good at the first iteration of the inner while loop. Presumably, you've already read the first character into ch, although you haven't shown that.

So at line 6, either the get is failing, or it succeeded, but did not get a lower case character.

There is a text file that contains thoese characters (as I said "input file").

1
2
3
    fin.open ("paldies.txt", ios::in);
    fout.open("paldiess.txt", ios::out);
    if (!fin){fout<<"nothing"; return 13;}


The thing is I need only words that starts only with small-case letters from 'a' to 'z' thats why I use ascii form 97 to 122.
Topic archived. No new replies allowed.