创建货币数据类型 C++
我的家庭作业是创建一个名为Currency 的类,使用整数作为美元的基本类型。我必须使用我拥有的这些公共方法...... 现在我删除了“美分”部分和“减法”部分,因为我假设一旦我弄清楚如何对“美元”货币类型进行“加法”,我就可以做到这些。
无论我在“Currency Add(int Dollars);”中输入什么内容我无法让我的代码工作=。 我做过类似totalDollars += Dollars、返回totalDollars 或只是Dollars += Dollars 等操作。
输出总是垃圾:“-8353513636”。 或者有时我在构造函数中输入的任何数字。
我似乎无法让它传递数字并将其保留在那里(最终想让它保存运行总数),并在完成后输出它。我缩短了我的代码,因为我的代码的唯一问题是处理“类”/货币数据类型的部分。它就是行不通。
有一次它告诉我它无法从 int 转换为货币数据类型
这是我到目前为止的代码:
/* Program By:
Date: 4/29/2011
Class:
*/
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <ostream>
using namespace std;
// Currency Class
class Currency
{
private:
int Dollars;
public:
Currency();
Currency(int Dollars);
Currency Add(int Dollars);
int GetDollars();
};
Currency::Currency()
{
Dollars = 0;
}
Currency::Currency(int Dollars)
{
}
// Add
Currency Currency::Add(int Dollars)
{
// Have put totalDollars += Dollars, etc.
// Have also put a "return" value here.
}
int Currency::GetDollars()
{
return Dollars;
}
int main()
{
Currency payroll;
int currDollars;
cout << "Please enter a dollar mount" << endl;
cin >> currDollars;
payroll.Add(currDollars);
cout << "Current Amount is: " << payroll.GetDollars() << endl;
return 0;
}
我做错了什么?我已经为此花费了几个小时,我需要在明天之前完成=。 帮助非常感谢!!!!
my homework assignment is to create a class named Currency, using integers as the primitive type for dollars. And I must use these public methods I have.....
Now I removed the "cents" part, and the "subtration" part, because I'm assuming I can do those once I figure out how to do "Addition" for "Dollars" currency type.
No matter what I put into the "Currency Add(int Dollars);" I cannot get my code to work =.
I've done things like totalDollars += Dollars, return totalDollars, or just Dollars += Dollars, etc.
The output is always garbage : "-8353513636".
Or sometimes whatever number I put in the constructor.
I just can not seem to get it to PASS the number in and keep it there (Eventually want to make it hold a running total), and output it when I'm done. I've shortened up my code because the only problem with my code is the parts dealing with the "class"/currency data type. It just won't work.
At one point it told me it cannot convert from int to Currency data type
Here's the code I have so far:
/* Program By:
Date: 4/29/2011
Class:
*/
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <ostream>
using namespace std;
// Currency Class
class Currency
{
private:
int Dollars;
public:
Currency();
Currency(int Dollars);
Currency Add(int Dollars);
int GetDollars();
};
Currency::Currency()
{
Dollars = 0;
}
Currency::Currency(int Dollars)
{
}
// Add
Currency Currency::Add(int Dollars)
{
// Have put totalDollars += Dollars, etc.
// Have also put a "return" value here.
}
int Currency::GetDollars()
{
return Dollars;
}
int main()
{
Currency payroll;
int currDollars;
cout << "Please enter a dollar mount" << endl;
cin >> currDollars;
payroll.Add(currDollars);
cout << "Current Amount is: " << payroll.GetDollars() << endl;
return 0;
}
What am I doing wrong? I've been at this for several hours and I need to have it in by tomorrow =.
Help MUCH APPRECIATED!!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好吧,我们这里有一个范围界定问题。
由于您的参数和成员都命名为 Dollars,因此您需要使用 this-Pointer 来指定要添加到哪个变量。
如果不这样做,参数将隐藏该成员,因此您实际上只是将新金额添加到自身,然后将其丢弃。
Alright, we have a scoping issue here.
Since your parameter and member are both named Dollars, you need to use the this-Pointer to specify to which variable you would like to add.
If you don't, the parameter hides the member, so you are effectively just adding the new amount to itself and then discarding it.
以下是一些可能有所帮助的提示:
在您的货币类中,我建议使用命名约定,以便您可以将成员变量与构造函数和函数中的参数区分开来。似乎在区分
Dollars
成员变量和也名为Dollars
的参数时可能会出现一些混淆。一种可能的命名约定是将成员变量命名为“my”,因为它们属于该类。还有其他方法可以使用 this 指针来处理这种情况,但您可能还没有了解指针。我看到的另一个问题是,采用 int Dollars 参数的函数似乎没有对参数值执行任何操作。我知道您正在尝试弄清楚如何执行此操作,因此这里有一个 Add 函数的示例。对于 Add,我现在只是让它返回 void,这样您就不必处理从函数返回
this
对象的问题。我希望这会有所帮助。
Here are some tips that might help:
In your Currency class, I would recommend using a naming convention so you can tell your member variables apart from arguments in the constructor and functions. It seems like there might be some confusion in differentiating your
Dollars
member variable and the arguments that are also namedDollars
. One possible naming convention is to name you member variables "my" because they belong to the class. There are other ways to handle this situation using thethis
pointer, but you may not have learned about pointers yet.The other problem I see is that your functions that take the
int Dollars
argument don't seem to do anything with the argument value. I know you're trying to figure out how to do this, so here's an example for the Add function. For Add, I would just make it return void for now, so you don't have to deal with returning thethis
object from the function.I hope that helps a little.
每当您使用 C 或 C++、Java 或任何语言编写代码时,您确实需要注意变量名称。
这里发生的情况是,您在类中使用 Dollars 作为变量名称,在函数参数中使用 和 。该参数会覆盖该类,无论您对 Add 内的 Dollars 执行什么操作,都不会对该类内的 Dollars 变量产生任何影响。
尝试将 Dollars 参数重命名为诸如
x
之类的描述性名称。Whenever you write code in C or C++, Java or any language, really, you need to pay attention to your variable names.
What's happened to you here is that you've used Dollars as the variable name inside the class, and in the function parameter. The parameter overrides the class and whatever you do to Dollars inside Add never has any affect on the Dollars variable inside the class.
Try renaming the Dollars parameter to something descriptive like
x
.我认为您不需要在
Add
函数中返回Currency
。您的
Add
函数应如下所示:void 货币::Add(int DollarsToAdd) {
美元 += 美元添加;
}
I don't think you need to return
Currency
in yourAdd
function.your
Add
function should look like this:void Currency::Add(int dollarsToAdd) {
Dollars += dollarsToAdd;
}
您当前的代码存在很多问题。
尝试一下:
There are a bunch of issues with your current code.
Give this a try: