I was in the library's multimedia room to do my assignment, but all PCs don't have Visual Studio and other smiliar programs. So I wrtie the code on Notepad for the question:

Design a solution that will determine and display the fewest number of ringgit and coins in change of a purchase value. The customer may pay any amount for the purchase. The changes are in the form of 1 cent, 5 cents, 10 cents, 50 cents, RM1, RM5, RM10 and RM50.

The following is an example of the solution result.

Purchase : RM 23.63
Payment : RM 100.00
Change : RM 76.37

RM50 = 1
RM10 = 2
RM5 = 1
RM1 = 1
50 cents = 0
10 cents = 3
5 cents = 1
1 cents = 2

/*
C++ code for Part B Q1
*/

#include "stdafx.h"
#include <iostream>
#include <cmath>

using namespace std;

int main()
{


    //variable declaration
    int FiftyRinggit, TenRinggit, FiveRinggit, Ringgit, FiftyCents, TenCents, FiveCents, Cent;
    float   PurchasePrice, Payment, Change;

    //input or prompt
    cout<<"-----------------PURCHASE AND CHANGE-----------------\n";
    cout<<"Purchase   :   RM";
    cin>>PurchasePrice;

    cout>>"Payment  :   RM";
    cin>>Payment;

    //process
    Change = Payment - PurchasePrice;

    //convert
    Cent = Change * 100;

    //figure out how much change is owed

    //Number of RM50 notes
    FiftyRinggit = Cent / 5000;
    Cent = Cent - (FiftyRinggit * 5000);

    //Number of RM10 notes
    TenRinggit = Cent / 1000;
    Cent = Cent - (TenRinggit * 1000);

    //Number of RM5 notes
    FiveRinggit = Cent / 500;
    Cent = Cent - (FiveRinggit * 500);

    //Number of RM1 notes
    Ringgit = Cent / 100;
    Cent = Cent - (Ringgit * 100);

    //Number of 50 cents coins
    FiftyCents = Cent / 50;
    Cent = Cent - (FiftyCents * 50);

    //Number of 10 cents coins
    TenCents = Cent / 10;
    Cent = Cent - (TenCents * 10);

    //Number of 5 cents coins
    FiveCents = Cent / 50;
    Cent = Cent - (FiveCents * 5);

    //output
    cout<<"Change :   RM"<<Change<<endl

    cout<<"RM50="<<FiftyRinggit
    cout<<"RM10="<<TenRinggit
    cout<<"RM5="<<FiveRinggit
    cout<<"RM1="<<Ringgit
    cout<<"50 cents="<<FiftyCents
    cout<<"10 cents="<<TenCents
    cout<<"5 cents="<<FiveCents
    cout<<"1 cent="<<Cent<<endl
    cout<<"--------------------------END------------------------"<<endl;


    system("pause");
    return 0;
}

Does it look correct or not?

Recommended Answers

All 2 Replies

Looks right. Other than needing some << endl and semicolons at the end our your output statements.

A check that payment is atleast purchase price would help. Also, it's usually considered good practice to intiialize your variables in the delcaration. did find one bug FiveCents = Cent / 50;, shpuld be FiveCents = Cent / 5;.

Here's your code with a couple of changes:

// Ringgit_Make_Change.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    //variable declaration
    int FiftyRinggit = 0;
    int TenRinggit = 0;
    int FiveRinggit = 0;
    int Ringgit = 0;
    int FiftyCents = 0;
    int TenCents = 0;
    int FiveCents = 0;
    int Cent = 0;
    double PurchasePrice = 0;
    double Payment = 0;
    double Change = 0;
    //input or prompt
    cout<<"-----------------PURCHASE AND CHANGE-----------------\n";
    cout<<"Purchase : RM";
    cin>>PurchasePrice;
    while(Payment < PurchasePrice)
    {
    cout<<"Payment : RM";
    cin>>Payment;
    }
    //process
    Change = Payment - PurchasePrice;
    //convert
    Cent = Change * 100;
    //figure out how much change is owed
    //Number of RM50 notes
    FiftyRinggit = Cent / 5000;
    Cent = Cent - (FiftyRinggit * 5000);
    //Number of RM10 notes
    TenRinggit = Cent / 1000;
    Cent = Cent - (TenRinggit * 1000);
    //Number of RM5 notes
    FiveRinggit = Cent / 500;
    Cent = Cent - (FiveRinggit * 500);
    //Number of RM1 notes
    Ringgit = Cent / 100;
    Cent = Cent - (Ringgit * 100);
    //Number of 50 cents coins
    FiftyCents = Cent / 50;
    Cent = Cent - (FiftyCents * 50);
    //Number of 10 cents coins
    TenCents = Cent / 10;
    Cent = Cent - (TenCents * 10);
    //Number of 5 cents coins
    FiveCents = Cent / 5;
    Cent = Cent - (FiveCents * 5);
    //output
    cout<<"Change : RM"<<Change<<endl;
    cout<<"RM50="<<FiftyRinggit<<endl;
    cout<<"RM10="<<TenRinggit<<endl;
    cout<<"RM5="<<FiveRinggit<<endl;
    cout<<"RM1="<<Ringgit<<endl;
    cout<<"50 cents="<<FiftyCents<<endl;
    cout<<"10 cents="<<TenCents<<endl;
    cout<<"5 cents="<<FiveCents<<endl;
    cout<<"1 cent="<<Cent<<endl<<endl;
    cout<<"--------------------------END------------------------"<<endl;
    system("pause");
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.