问题与Infite循环 [英] Issue with Infite Loop

查看:147
本文介绍了问题与Infite循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题。这段代码没有给出它应该的结果。它应该输出数字到大到控制台,并且由于某种原因它忽略了if语句时这样做。此外,在此程式中 InputNum 应保留长数据类型。

I have a problem. This code is not giving the results it should. It should out number to large to the console and for some reason it is ignoring the if the statement when doing this. Also, in this program InputNum should remain of the long data type.

#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 = 3000000000;

    //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;      
    }
}


推荐答案

CalculateBinary(InputNum)不会修改 InputNum 的值,因此其值将始终相同(300000000)而循环永远不会结束。

CalculateBinary(InputNum) does NOT modify the value of InputNum, so its value would be always the same (300000000) and the while loop never end.

这篇关于问题与Infite循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆