Home > Net >  Converting dollars to proper currency
Converting dollars to proper currency

Time:11-22

how do I define the value euros = 1.06, pesos = 9.73, and yen = 124.35 as constant in the global section of this following program? I want to convert the dollar to pesos, yen, and euro.

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


 void convertMulti(float d, float& , float& );
 void convertMulti(float d , float& , float& , float& );

int main ()
{
float dollars;
float euros = 1.06;
float pesos = 9.73;
float yen = 124.35;

cout << fixed << showpoint << setprecision(2);
cout << "Please input the amount of American Dollars you want converted "
<< endl;
cout << "to euros and pesos" << endl;
cin >> dollars;

convertMulti(dollars,euros,pesos);
 cout << "$" << dollars << "is converted to" << euros << "euros and" << pesos << "pesos"
     << endl;

cout << "Please input the amount of American Dollars you want converted\n";
cout << "to euros, pesos and yen" << endl;
cin >> dollars;

convertMulti(dollars, euros, pesos, yen);
cout << "$" << dollars << "is converted to " << euros << "euros, " << pesos << "pesos, and"
<< yen << "yen" << endl;

void convertMulti(float d, float& e, float& p)
{

 e = d * e; 
 p = d * p;
}

void convertMulti(float d, float& e, float& p, float& y)
{
    e = d * e, p = d * p, y = d * y;
}

CodePudding user response:

You can use #define: #define eur 1.6 (not exactly recommended), or const double eur = 1.6. But this may lead to floating point error, and those can accumulate very quick if you exchange back and forth from many unit.

You can avoid this using fixed-point arithmetic. Basically, you round all number up a certain point, for examples 100 times (1/100). Probably something like this (untested C code):

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

const int euros = 106;
const int pesos = 973;
const int yen = 12435;

const double scale = 100.0;

int main()
{
    double dollars; cout << "Input dollars : "; cin >> dollars;

    cout << fixed << setprecision(5);
    cout << dollars << " dollars = " << dollars * euros / scale << " euros" << '\n';
    cout << dollars << " dollars = " << dollars * pesos / scale << " pesos" << '\n';
    cout << dollars << " dollars = " << dollars * yen / scale << " yen" << '\n';
}

It wouldn't matter too much if you're only doing 1 conversion and using double, but after a few more it'd probably get out of hand.

CodePudding user response:

You can just define as constants outside of main using #define (make sure to add "f" after the value to indicate a float):

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


void convertMulti(float d, float& , float& );
void convertMulti(float d , float& , float& , float& );

#define EUROS 1.06f;
#define PESOS 9.73f;
#define YEN 124.35f;


int main ()
{
...
}

Personally though, I would recommend removing the "f" as to make it a double to avoid potential rounding issues:

#define EUROS 1.06;
#define PESOS 9.73;
#define YEN 124.35;
  •  Tags:  
  • c
  • Related