创建货币数据类型 C++

发布于 2024-11-04 08:42:48 字数 1536 浏览 4 评论 0原文

我的家庭作业是创建一个名为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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

忆悲凉 2024-11-11 08:42:49

好吧,我们这里有一个范围界定问题。

由于您的参数和成员都命名为 Dollars,因此您需要使用 this-Pointer 来指定要添加到哪个变量。

this->Dollars += Dollars;

如果不这样做,参数将隐藏该成员,因此您实际上只是将新金额添加到自身,然后将其丢弃。

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.

this->Dollars += Dollars;

If you don't, the parameter hides the member, so you are effectively just adding the new amount to itself and then discarding it.

被你宠の有点坏 2024-11-11 08:42:49

以下是一些可能有所帮助的提示:

在您的货币类中,我建议使用命名约定,以便您可以将成员变量与构造函数和函数中的参数区分开来。似乎在区分 Dollars 成员变量和也名为 Dollars 的参数时可能会出现一些混淆。一种可能的命名约定是将成员变量命名为“my”,因为它们属于该类。还有其他方法可以使用 this 指针来处理这种情况,但您可能还没有了解指针。

class Currency
{
private:

    int myDollars;

public:
    Currency();
    Currency(int dollars);
    void Add(int dollars); // changed this to return void - see below
    int GetDollars();
};

我看到的另一个问题是,采用 int Dollars 参数的函数似乎没有对参数值执行任何操作。我知道您正在尝试弄清楚如何执行此操作,因此这里有一个 Add 函数的示例。对于 Add,我现在只是让它返回 void,这样您就不必处理从函数返回 this 对象的问题。

void Currency::Add(int dollars)
{ 
    myDollars += dollars;
}

int Currency::GetDollars()
{
    return myDollars;
}

我希望这会有所帮助。

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 named Dollars. 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 the this pointer, but you may not have learned about pointers yet.

class Currency
{
private:

    int myDollars;

public:
    Currency();
    Currency(int dollars);
    void Add(int dollars); // changed this to return void - see below
    int GetDollars();
};

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 the this object from the function.

void Currency::Add(int dollars)
{ 
    myDollars += dollars;
}

int Currency::GetDollars()
{
    return myDollars;
}

I hope that helps a little.

衣神在巴黎 2024-11-11 08:42:49

每当您使用 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.

诺曦 2024-11-11 08:42:49

我认为您不需要在 Add 函数中返回 Currency

您的 Add 函数应如下所示:


void 货币::Add(int DollarsToAdd) {
美元 += 美元添加;
}

I don't think you need to return Currency in your Add function.

your Add function should look like this:


void Currency::Add(int dollarsToAdd) {
Dollars += dollarsToAdd;
}

蘑菇王子 2024-11-11 08:42:49

您当前的代码存在很多问题。

  • 当然,您需要两个整数来存储数据。
  • 然后为每个值提供一个获取函数。
  • 你我认为你通过尝试返回货币而使你的 Add 函数过于复杂。不需要,只需将其设为无效并将输入的金额添加到您的整数值中即可。
  • 您需要处理超过 100 美分的情况。例如 56 + 56 = 112 美分...
  • 您没有提到数据验证,这可能也需要执行。例如,不允许 alpha 和其他垃圾,并且不允许美分值 > 99 待输入。等等。

尝试一下:

class Currency
{
private:
    int m_dollars;
    int m_cents;

public:
    Currency(int dollars, int cents);
    Currency Add(int dollars, int cents);
    int GetDollars();
    int GetCents();
};

Currency::Currency(int dollars, int cents) 
{ 
    m_dollars = dollars; 
    m_cents = cents;
}

Currency Currency::Add(int dollars, int cents)
{ 
    if (m_cents + cents >= 100) 
        return Currency(m_dollars + dollars + 1, m_cents + cents - 100);

    return Currency(m_dollars + dollars, m_cents + cents);
}

int Currency::GetDollars() { return m_dollars; }
int Currency::GetCents() { return m_cents; }

void main()
{
    int currDollars;
    int currCents;

    cout << "Please enter a whole dollar amount:" << endl;
    cin >> currDollars;

    cout << "Please enter a cents amount:" << endl;
    cin >> currCents;

    Currency payroll(currDollars, currCents);

    cout << "Current Amount is: " 
         << payroll.GetDollars() 
         << "."
         << payroll.GetCents()
         << endl;

    cout << "Please enter a whole dollar amount to add:" << endl;
    cin >> currDollars;

    cout << "Please enter cents to add:" << endl;
    cin >> currCents;

    payroll = payroll.Add(currDollars, currCents);

    cout << "Updated Amount is: " 
         << payroll.GetDollars() 
         << "."
         << payroll.GetCents()
         << endl;
}

There are a bunch of issues with your current code.

  • You need two integers, of course, to store the data.
  • Then a get function for each value.
  • You I think you overcomplicated your Add function by trying to return a Currency. No need, just make it void and have it add the entered amounts to your integer values.
  • You need to deal with cents overflowing past 100. E.g. 56 + 56 = 112 cents...
  • You didn't mention data validation, that might need to be performed too. E.g. Disallow alpha and other garbage and do not allow cents values > 99 to be entered. etc.

Give this a try:

class Currency
{
private:
    int m_dollars;
    int m_cents;

public:
    Currency(int dollars, int cents);
    Currency Add(int dollars, int cents);
    int GetDollars();
    int GetCents();
};

Currency::Currency(int dollars, int cents) 
{ 
    m_dollars = dollars; 
    m_cents = cents;
}

Currency Currency::Add(int dollars, int cents)
{ 
    if (m_cents + cents >= 100) 
        return Currency(m_dollars + dollars + 1, m_cents + cents - 100);

    return Currency(m_dollars + dollars, m_cents + cents);
}

int Currency::GetDollars() { return m_dollars; }
int Currency::GetCents() { return m_cents; }

void main()
{
    int currDollars;
    int currCents;

    cout << "Please enter a whole dollar amount:" << endl;
    cin >> currDollars;

    cout << "Please enter a cents amount:" << endl;
    cin >> currCents;

    Currency payroll(currDollars, currCents);

    cout << "Current Amount is: " 
         << payroll.GetDollars() 
         << "."
         << payroll.GetCents()
         << endl;

    cout << "Please enter a whole dollar amount to add:" << endl;
    cin >> currDollars;

    cout << "Please enter cents to add:" << endl;
    cin >> currCents;

    payroll = payroll.Add(currDollars, currCents);

    cout << "Updated Amount is: " 
         << payroll.GetDollars() 
         << "."
         << payroll.GetCents()
         << endl;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文