Infinite loop while attempting to read char by char until end of line

Sep 26, 2014 at 11:42pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//Reads in digits for an array.
int getFactors (int ary[], ifstream &inFile)
{
    char k = '0';
    int count;
    
    for (int i = 0; i < MAX; i++)
    {
        while (k != '\n')
        {
            count++;
            inFile >> k;
            ary[i] = k - '0';   //Convert character to integer and store in array.
            cout << ary[i]; //TEST
        }
    }
    return count;
}


I have this function which is intended to read input from a file one character at a time and convert and store the integers in an array. When I run this with the //TEST part that you see below, I get an infinite loop that looks like this:

6946123853213872663123654321222222222222222222222222222222555555555556666666666666666666999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999387687687687687687687687687687658485949392039999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999


This part of the code was given by my instructor:
1
2
3
4
5
6
7
while (k != '\n')
        {
            ...
            inFile >> k;
            ary[i] = k - '0';
            ...
         }


If anybody can see what is causing it to fail, please help out!
Sep 26, 2014 at 11:43pm
//TEST part that you see above***
Sep 27, 2014 at 3:37am
What is MAX??
Sep 28, 2014 at 2:06am
MAX is the number of elements in the array...
Sep 28, 2014 at 2:35am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const int MAX = 100 ;

int getFactors( int ary[], std::istream& stm )
{
    char k = '0';
    int count = 0 ; // initialise

    while( stm.get(k) && // a character was read ( note: stm >> k skips whitespace; it won't read a new line)
           k != '\n' && // and it was not a newline
           count < MAX ) // and the array bound is not exceeded
    {
        if( k >= '0' && k <= '9' ) // isdigit : if it is a digit
        {
            ary[count] = k - '0';   // Convert character to integer and store in array.
            ++count ;
        }
        else { /* the character is not a digit. throw it away(?) */ }
    }

    return count;
}
Sep 28, 2014 at 3:55am
Thanks @JLBorges, that was helpful and I modified my code using your suggestions. I'm having a weird thing happening now though. As the program steps through the input file, when it encounters a number that takes up the entire array (30 integers) it goes back and gets the number before that one before moving on the next one..?
Sep 28, 2014 at 4:06am
Posting your code would be a good idea.
Sep 28, 2014 at 4:21am
Here is my modified function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int getFactors (int ary[], ifstream &inFile)
{
	char k = '0';
	int count = 0;
    
	while (inFile.get(k) && k != '\n' && count < MAX)
	{
        if (k >= '0' && k <= '9')
        {
            ary[count] = k - '0';
            count++;
        }
	}
    
	return count;
}


And here's what is happening in main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main()
{
    int topFactor[MAX], bottomFactor[MAX], product[MAX];
    int t_NUM, b_NUM, p_NUM;
    
    ifstream inFile;
    inFile.open ("/Users/jordanfleming/Desktop/BigNumbersV2.txt");
    
    while (inFile)
    {
        t_NUM = getFactors (topFactor, inFile);
        b_NUM = getFactors (bottomFactor, inFile);

        printUnEArray (topFactor, t_NUM);               //TEST
        cout << endl;                                            //TEST
        printUnEArray (bottomFactor, b_NUM);        //TEST
        cout << endl;                                           //TEST
    }
    
    inFile.close();
    
    return 0;
}


And here is the output that I am getting:

600000000000000000000000000000
900000000000000000000000000000
461230000000000000000000000000
853210000000000000000000000000
387266312300000000000000000000
654321000000000000000000000000
222222222222222222222222222222
654321000000000000000000000000
555555555556666666666666666666
654321000000000000000000000000
999999999999999999999999999999
654321000000000000000000000000
999999999999999999999999999999
654321000000000000000000000000
999999999999999999999999999999
654321000000000000000000000000
399999999999999999999999999999
876876876876876876876876876876
399999999999999999999999999999
584859493920396876876876876876
399999999999999999999999999999
584859493920396876876876876876


Also, the input file is this:
6
9
46123
85321
3872663123
654321
222222222222222222222222222222
555555555556666666666666666666
999999999999999999999999999999
999999999999999999999999999999
999999999999999999999999999999
3
876876876876876876876876876876
58485949392039
Sep 28, 2014 at 4:28am
And what does your printUnEArray() look like?
Sep 28, 2014 at 4:32am
A simple print function:
1
2
3
4
5
6
7
void printUnEArray (int ary[])
{
    for (int i = 0; i < MAX; i++)
    {
        cout << ary[i];
    }
}


Note: The reason I am printing out all the way to MAX instead of the exact number of elements in the array has to do with the purpose of other parts of the program.
Sep 28, 2014 at 4:45am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int getFactors (int ary[], std::ifstream &inFile)
{
	char k = '0';
	int count = 0;
	
	// initialise the array to all zeroes
	for( int i = 0 ; i < MAX ; ++i ) ary[i] = 0 ; // *** added ****

	while (inFile.get(k) && k != '\n' && count < MAX)
	{
        if (k >= '0' && k <= '9')
        {
            ary[count] = k - '0';
            count++;
        }
	}

        // ******************* added ***********************
        if( k != '\n' ) // if there are more characters left in this particular line
                inFile.ignore( 1000000, '\n' ) ; // throw them away
        // http://www.cplusplus.com/reference/istream/istream/ignore/

	return count;
}
Sep 28, 2014 at 5:00am
Perfect. Thank you and thanks for the reference. Definitely need to do more research about how all this works.
Topic archived. No new replies allowed.