MSK
MSK

Reputation: 25

Conversions in C++ program

Please see the image below for this complex problem: enter image description here After reading that: So far I have a lot of the code written up. I just dont know quite how to add together the pounds, shellings, and pence because of the conversions. I think I may have been able to add up the pounds and shellings properly, but I'm going crazy over trying to find a way to add up the shellings, while keeping conversions and format proper! Please help!

/*Write a program that asks the user to enter two money amounts expressed in old-pounds and will then add the two amounts and display the answer both in old-pounds and in decimal-pounds.
OLD CURRENCY
1 pound= 20 shillings
1 pound=240 pence
1 shilling= 12 pence
conversion rate from old pence to new pence *.416
*/

#include <iostream>
using namespace std;

int main()
{
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);

    int pounds=0;
    int shillings=0;
    int pence=0;
    char dot1;
    char dot2;
    int pounds2=0;
    int shillings2=0;
    int pence2=0;
    char dot12=0;
    char dot22=0;
    double Poundtotal=0;
    int shillingstotal=0;
    int pencetotal=0;
    double x;
    double y;

    cout << "Enter first old-pound amount:";
    cin >> pounds >> dot1 >> shillings >> dot2 >> pence;
    cout << "Enter second old-pound amount:";
    cin >> pounds2 >> dot12 >> shillings2 >> dot22 >> pence2;
    Poundtotal= (pounds+pounds2);
    shillingstotal=(shillings+shillings2);
    pencetotal=pence+pence2;
    x=(shillingstotal*12)+pencetotal;
    while (x>240)
    {
        Poundtotal=Poundtotal+1;
        x=x-240;
    }


    cout<<x<<endl;
    cout<<Poundtotal<<endl;

    /*need to find a way to add the rest!*/

    return 0;
}

Upvotes: 1

Views: 1375

Answers (3)

raman
raman

Reputation: 93

Posting an answer to help others who might be stumped with the same or similar question. You can chain the inputs collected through cin. You can modify this code to add the old pound.

#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
    cout << "Enter the amount in pound-shillings-pence format (e.g. 5.2.8): ";
    int pounds, shillings, pence;
    char separator;
    cin >> pounds >> separator >> shillings >> separator >> pence;

    //Imperial pound to imperial pence (1 pound = 240 pence)
    pence += 240 * pounds;
    //Imperial shilling to pence (1 Shilling = 12 pence)
    pence += 12 * shillings;
    //Decimal pounds
    //Unicode for the pound sign = \u00A3
    cout << "Decimal pounds: \u00A3" << fixed << setprecision(2) << (float) pence/240 << endl;
}

Upvotes: 0

neshkeev
neshkeev

Reputation: 6476

You are confused because there are no types with two decimal points in the number, right? Then you have to use a string value for old amount and a float one for new amount like this:

#include <iostream>
#include <string>
#include <iomanip>

class Money
{
public:
    Money(int pound, int shilling, int pence):
        pence(pence),
        shilling(shilling),
        pound(pound)
    {
    }

    Money& operator+=(const Money& money)
    {
        auto newPence = this->pence + money.pence;
        auto newShilling = this->shilling + money.shilling + newPence / 12;
        this->pence = newPence % 12;
        this->shilling = newShilling % 20;
        this->pound += money.pound;
        this->pound += newShilling / 20;
    }
    std::string GetOldAmount()
    {
        return std::to_string(pound) + "." + std::to_string(shilling) + "." + std::to_string(pence);
    }
    float GetNewAmount()
    {
        auto totalPences = 1.0f * (pound * 240 + shilling * 12 + pence) / 240;
        return totalPences;
    }
private:
    int pence = 0;
    int shilling = 0;
    int pound = 0;
};

int main()
{
    int pound, shill, pence;
    char dot1, dot2;
    std::cout << "Enter first old-pound amount:";
    std::cin >> pound >> dot1 >> shill >> dot2 >> pence;
    Money money1(pound, shill, pence);
    std::cout << "Enter second old-pound amount:";
    std::cin >> pound >> dot1 >> shill >> dot2 >> pence;
    Money money2(pound, shill, pence);
    money1 += money2;
    std::cout << "Old-pound total = ";
    std::cout << money1.GetOldAmount() << std::endl;
    std::cout << "Decimal-pound total = ";
    std::cout << std::setprecision(3) << money1.GetNewAmount() << std::endl;
}

Result

Enter first old-pound amount:5.10.11
Enter second old-pound amount:3.9.5
Old-pound total = 9.0.4
Decimal-pound total = 9.02

Upvotes: 0

Thomas Matthews
Thomas Matthews

Reputation: 57678

  1. Convert everything to the smallest unit.
  2. Add them together.
  3. Divide up into larger units if desired.

In the U.S.A., our smallest denomination is the penny. A quarter is equal to 25 pennies, a dime is equal to 10 pennies.

To add a dime and a quarter, one would convert them to pennies and add, resulting in 35 pennies (equivalence).

Lastly, use a debugger to execute each statement separately. Also, print out, display or watch the contents of the variables.

Upvotes: 3

Related Questions