0

Ok this program I am working on seems to be all ok except there is a problem. Here is the code

#include <iostream>
#include <fstream>

using namespace std;

/*
Function Name: CalculateBinary
CalculateBinary takes a number from the main function and finds its binary form.
*/

void CalculateBinary( long InputNum)
{   
    //Takes InputNum and divides it down to "1" or "0" so that it can be put in binary form.
    if ( InputNum != 1 && InputNum != 0)
        CalculateBinary(InputNum/2);

    // If the number has no remainder it outputs a "0". Otherwise it outputs a "1". 
    if (InputNum % 2 == 0)
        cout << "0";
    else
        cout << "1";
}


void main()
{
    // Where the current number will be stored
      long InputNum;

    //Opens the text file and inputs first number into InputNum. 
    ifstream fin("binin.txt");
    fin >> InputNum;

    // While Input number is not 0 the loop will continue to evaluate, getting a new number each time.
    while (InputNum >= 0)
    {
        if(InputNum > 1000000000)
            cout << "Number too large for this program ....";
        else
            CalculateBinary(InputNum);

        cout << endl;
        fin >> InputNum;        
    }
}

Here is the text file I am reading in

12
8764
 2147483648
2
-1

When I get to 8764, it just keeps reading in this number over and over again. It ignores the 2147483648. I know I can solve this by declaring InputNum as a long long. But I want to know why is it doing this?

5
  • Is the space before 2147483648 supposed to be there? Commented Sep 13, 2011 at 17:01
  • Looks familiar: stackoverflow.com/questions/7397034/infinite-loop-problem Commented Sep 13, 2011 at 17:01
  • depending on the input from binin.txt this program may have UB. Commented Sep 13, 2011 at 17:01
  • @Mystical it is the same thing, but I didnt quite ask what I wanted to in that question Commented Sep 13, 2011 at 17:05
  • @Steffan: I know, hence why I didn't say "duplicate". Commented Sep 13, 2011 at 17:06

4 Answers 4

4

That is the usual problem with such loops which you've written.

The correct and the idiomatic loop is this:

ifstream fin("binin.txt");
long InputNum;
while (fin >> InputNum && InputNum >= 0)
{
   //now construct the logic accordingly!
    if(InputNum > 1000000000)
         cout << "Number too large for this program ....";
    else
         CalculateBinary(InputNum);
    cout << endl;
}
Sign up to request clarification or add additional context in comments.

2 Comments

@pezcode: There was a typo. I wanted to initialize InputNum with 0.
@Nawaz, just reverse the while loop condition.
2

That number is too large for a long to store, so fin >> InputNum; does nothing. You should always read as while(fin >> InputNum) { ... }, as that will terminate the loop immediately on failure, or at least check the stream state.

Comments

0

It would appear that the long type on your platform is 32 bits wide. The number 2147483648 (0x80000000) is simply too large to be represented as a signed 32-bit integer. You either need an unsigned type (which obviously won't work with negative numbers) or a 64-bit integer.

Also, you should check whether the read is successful:

  ...
  cout << endl;
  if (!(fin >> InputNum)) break; // break or otherwise handle the error condition
}

Comments

0

You don't check for EOF, thus being trapped in a loop forever. fin >> InputNum expression returns true if succeeded, false otherwise, so changing you code to something like this will solve the problem:

while ((fin >> InputNum) && InputNum >= 0)
{
  // ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.