gpt4 book ai didi

c++ - 无限循环问题

转载 作者:搜寻专家 更新时间:2023-10-31 00:45:09 24 4
gpt4 key购买 nike

我有一个问题。此代码未给出应有的结果。它应该超出控制台的数量,并且出于某种原因,它在执行此操作时忽略了 if 语句。另外,在这个程序中,InputNum 应该保持 long 数据类型。

#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) 并且 while 循环永远不会结束。

关于c++ - 无限循环问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7397034/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com