我正在用 c++ 编写一个函数,它应该在传递的数字中找到最大的单个数字(inputValue)。例如,0.345 的答案是 5。但是,过了一会儿,程序将 inputValue 更改为类似于 0.3449 的值(然后将最大数字设置为 9)。我不知道为什么会这样。任何解决此问题的帮助将不胜感激。
这是我的 .hpp 文件中的函数
void LargeInput(const double inputValue)
//Function to find the largest value of the input
{
int tempMax = 0,//Value that the temporary max number is in loop
digit = 0,//Value of numbers after the decimal place
test = 0,
powerOten = 10;//Number multiplied by so that the next digit can be checked
double number = inputValue;//A variable that can be changed in the function
cout << "The number is still " << number << endl;
for (int k = 1; k <= 6; k++)
{
test = (number*powerOten);
cout << "test: " << test << endl;
digit = test % 10;
cout << (static_cast<int>(number*powerOten)) << endl;
if (tempMax < digit)
tempMax = digit;
powerOten *= 10;
}
return;
}