我正在学习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 . . .
如果有人能告诉我我搞砸了哪里,我会非常感激。
答案 0 :(得分:2)
就您的原始问题而言,dollars
永远不会被初始化。因此,当时在内存中发生的任何值都将是您的函数添加的起始美元值。但是这个问题不是修复你的主要问题,而是因为你没有在你的函数中将新计算的和分配给dollars
,这将是根据函数描述的预期行为。
要在评论中回答您的其他问题,要将您的美分兑换成美元,您只需计算cents / 100.0f
,并注意您除以浮点数100.0f
而不是整数100
,以便您的结果本身变为float
而不是int
。
虽然从它的外观来看,这是各种各样的学校作业,但仍值得一提:
目前你想要实现的目标更多的是“将这些美分标准化并将它们添加到此值”。如果你想编写一个将你的美分兑换成美元的函数,那么把它写成
会更有意义float normalizeMoney(const int cents = 150);
然后用作
dollars += normalizeMoney(95);
忘记了完全没有根据的静态变量。
答案 1 :(得分:0)
我相信其他评论已回答了这个问题。以下是对其他错误的一些建议: 而不是if(美分> = 100)等,只做其他部分:193美分/ 100 == 1.93 此外,这个填充修复了你的问题,当增加超过199美分(它只会处理第一美元)。