Riotson
Riotson

Reputation: 81

Create Currency Data Type C++

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!!!!

Upvotes: 1

Views: 3350

Answers (5)

Paul Sasik
Paul Sasik

Reputation: 81429

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;
}

Upvotes: 1

Zan Lynx
Zan Lynx

Reputation: 54325

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.

Upvotes: 2

Andy White
Andy White

Reputation: 88345

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.

Upvotes: 2

xDD
xDD

Reputation: 3453

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.

Upvotes: 2

Quincy
Quincy

Reputation: 1939

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; }

Upvotes: 1

Related Questions