我正在学习C++,并且正在尝试使用具有静态变量的函数将美元转换为美分,该变量会在每次调用时累计总数。不幸的是,好像我的函数创建了上溢或下溢的情况。此函数的任何指针都将提供很大的帮助。这是代码。
#include <iostream>
#include <iomanip>
using namespace std;
void normalizeMoney(float& dollars, int cents = 150);
// This function takes cents as an integer and converts it to dollars
// and cents. The default value for cents is 150 which is converted
// to 1.50 and stored in dollars
int main()
{
int cents;
float dollars;
cout << setprecision(2) << fixed << showpoint;
cents = 95;
cout << "\n We will now add 95 cents to our dollar total\n";
normalizeMoney(dollars, cents);// Fill in the code to call normalizeMoney to add 95 cents
cout << "Converting cents to dollars resulted in " << dollars << " dollars\n";
cout << "\n We will now add 193 cents to our dollar total\n";
normalizeMoney(dollars, 193);// Fill in the code to call normalizeMoney to add 193 cents
cout << "Converting cents to dollars resulted in " << dollars << " dollars\n";
cout << "\n We will now add the default value to our dollar total\n";
normalizeMoney(dollars);// Fill in the code to call normalizeMoney to add the default value of cents
cout << "Converting cents to dollars resulted in " << dollars << " dollars\n";
return 0;
}
//*******************************************************************************
// normalizeMoney
//
// task: This function is given a value in cents. It will convert cents
// to dollars and cents which is stored in a local variable called
// total which is sent back to the calling function through the
// parameter dollars. It will keep a running total of all the money
// processed in a local static variable called sum.
//
// data in: cents which is an integer
// data out: dollars (which alters the corresponding actual parameter)
//
//*********************************************************************************
void normalizeMoney(float& dollars, int cents)
{
float total = 0;
// Fill in the definition of sum as a static local variable
static float sum = 0.0;
// Fill in the code to convert cents to dollars
if (cents >= 100) {
cents -= 100;
dollars += 1;
total = total + dollars;
sum = static_cast <float> (sum + dollars + (cents / 100));
}
else {
total += (cents / 100);
static_cast <float> (sum += (cents / 100));
}
cout << "We have added another $" << dollars << " to our total" << endl;
cout << "Our total so far is $" << sum << endl;
cout << "The value of our local variable total is $" << total << endl;
}
我得到的输出是:
We will now add 95 cents to our dollar total
We have added another $-107374176.00 to our total
Our total so far is $0.00
The value of our local variable total is $0.00
Converting cents to dollars resulted in -107374176.00 dollars
We will now add 193 cents to our dollar total
We have added another $-107374176.00 to our total
Our total so far is $-107374176.00
The value of our local variable total is $-107374176.00
Converting cents to dollars resulted in -107374176.00 dollars
We will now add the default value to our dollar total
We have added another $-107374176.00 to our total
Our total so far is $-214748352.00
The value of our local variable total is $-107374176.00
Converting cents to dollars resulted in -107374176.00 dollars
Press any key to continue . . .
如果有人可以告诉我我搞砸了,我将不胜感激。
最佳答案
就您最初的问题而言,dollars
从未初始化。因此,当时内存中的任何值将是您的函数所添加的起始美元值。但是,此问题未解决主问题,原因是您没有将新计算的总和分配给函数中的dollars
,这是根据函数描述的预期行为。
要在评论中回答您的其他问题,将您的美分转换为美元,您要做的就是计算cents / 100.0f
,注意您要除以浮点数100.0f
而不是整数100
,这样您的结果本身就变成了float
的int
。
笔记:
从外观上看,这是学校的各种作业,但仍然值得一提:
当前,您要实现的目标更多是“标准化这些美分并将其添加到该值”。如果您想编写一个将美分转换为美元的函数,则将其写为
float normalizeMoney(const int cents = 150);
然后用作dollars += normalizeMoney(95);
忘记完全不需要的静态变量。关于c++ - 在C++中将美元转换为美分,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50195741/