emufossum13
emufossum13

Reputation: 407

Currency Conversion Program

I'm working on a currency converter program that converts the old system of British pounds, Shillings, and pence, into their new system, which is a type of Decimal Pound. Where 100 pence equals a pound. Here is the code for the program

#include <iostream>
#include <iomanip>
#include <conio.h>
using namespace std;

int calcNum(int pound, int shilling, int pence)
{
    pence = pound*240 + shilling*12 + pence;
    return pence;
}

int calcNew(int total_pence, double dec_pound)
{
    dec_pound = total_pence / 240;
    return dec_pound;
}

int main()
{

    int pence;
    int shilling;
    int pound;
    const int OLD_POUND = 240;
    const int OLD_SHILLING = 12;
    double total_pence;
    double dec_pound = 0;
    double deci_pound;

    cout << "Please Enter the Amount of old pounds: ";
    cin >> pound;
    cout << endl;
    if(cin.fail())
    {

        cout << "That's not a valid number\n";
        cout << "This program will terminate on any keypress!";
        _getch();
        exit(1);
    }
    cout << "Please Enter the Amount of old shillings: ";
    cin >> shilling;
    cout << endl;
    if(cin.fail())
    {
        cout << "That's not a valid number\n";
        cout << "This program will terminate on any keypress!";
        _getch();
        exit(1);
    }
    cout << "Please Enter the Amount of old pence: ";
    cin >> pence;
    cout << endl;
    if(cin.fail())
    {
        cout << "That's not a valid number\n";
        cout << "This program will terminate on any keypress!";
        _getch();
        exit(1);
    }
    total_pence = calcNum(pence, shilling, pound);
    deci_pound = calcNew(dec_pound, total_pence);
    cout << (5, "\n");
    cout << "The total amount in decimal pounds is: ";
    cout << setprecision(2) << "\x9c" << deci_pound;
    _getch();
    return 0;
}

When I run this program however, I'm having a bit of a problem. No matter what the number input is, it always says 0 pounds. Just to make sure that the setprecision function at the end wasn't interfering with the code, I had originally set a cout statement with a _getch() after the two functions to show how much deci_pound came out to be calculated to, and once again, it came out as zero. So my issue seems to be somewhere in the functions running the calculations. If someone could help me with this, I would really appreciate it.

Upvotes: 2

Views: 3182

Answers (2)

Ionut
Ionut

Reputation: 6866

The order of the parameters when you call both functions is wrong. Your functions are declared and implemented as:

int calcNum(int pound, int shilling, int pence);
int calcNew(int total_pence, double dec_pound);

And then you call them like this:

total_pence = calcNum(pence, shilling, pound);
deci_pound = calcNew(dec_pound, total_pence);

Upvotes: 0

yizzlez
yizzlez

Reputation: 8805

Your calcNew(...) function returns an int, make it return a double. Right now it casts to int which involves stripping the decimals.

In your code, dec_pound is set equal to zero, and you're deci_pound = calcNew(dec_pound, total_pence), which divides 0 by 240 = 0.

Upvotes: 1

Related Questions