Currency Converter C++

Howdy all! I'm really new to C++ and thought this was really cool when I made it. All it does is converts US Dollars to five other currencies. It's a console based program using a function I downloaded on a different website that lets me color the text. Anyways, I linked to the .h file (maybe some of you guys might find it cool/useful) at the top. I just want to get some feed back from you guys on what you think about the code. I am hoping you guys can give me some insight as to where I can clean things up.

I also have a question about this. When it asks if you want to convert again and yes is selected it just shows another menu. I am wondering if there is a way to clear the screen completely then show the menu again.

Thanks in advance guys :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// To get the console.h file go to http://www.codeproject.com/KB/cpp/AddColorConsole/Console_h_src.zip to download.  

#include <iostream>
#ifdef WIN32
#include <windows.h>
#endif
#include <console.h>

namespace con = JadedHoboConsole;

int main()
{
    using std::cout;
    using std::endl;
    using std::cin;
    
  char choice;

  do
  {
    int currency;
    float eurosdollars, dollarseuros, dollarsyen, yendollars, dollarsUKpounds, UKpoundsdollars, dollarkronor, kronordollar, dollarrubles, rublesdollar;

    
    cout << con::fg_white <<"\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
     << con::fg_blue <<"1. Dollars to Euros\t\t\t2. Euros to Dollars\n"
     << con::fg_green <<"\n3. Dollars to Yen\t\t\t4. Yen to Dollars\n"
	 << con::fg_red <<"\n5. Dollars to UK Pounds\t\t\t6. UK Pounds to Dollars\n"
	 << con::fg_white <<"\n7. Dollars to Sweden Kronor\t\t8. Sweden Kronor to Dollars\n"
	 << con::fg_magenta <<"\n9. Dollars to Russian Rubles\t\t10. Russian Rubles to Dollars\n"
     << con::fg_white <<"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl;
    cout << con::fg_yellow <<"\nPlease enter the number of the currency you want to convert: ";
    cin >> currency;
    
    if (currency == 1)
    {
      cout << con::fg_blue <<"\nPlease enter the amount of United States Dollars you would like to convert to European Euros: ";
      cin >> eurosdollars;
      cout << con::fg_blue <<"\nYou have entered " << eurosdollars << " Dollars which is equal to " << eurosdollars*0.678518116 << " Euros." << endl;
    }
    else if (currency == 2)
    {
      cout << con::fg_blue <<"\nPlease enter the amount of European Euros you would like convert to United States Dollars: ";
      cin >> dollarseuros;
      cout << con::fg_blue <<"\nYou have entered " << dollarseuros << " Euros which is equal to " << dollarseuros*1.4738 << " Dollars." << endl;
    }
    else if (currency == 3)
    {
      cout << con::fg_green <<"\nPlease enter the amount of United States Dollars you would like to convert to Japanese Yen: ";
      cin >> dollarsyen;
      cout << con::fg_green <<"\nYou have entered " << dollarsyen << " Dollars which is equal to " << dollarsyen*95.71255 << " Yen." << endl;
    }
    else if (currency == 4)
    {
      cout << con::fg_green <<"\nPlease enter the amount of Japanese Yen you would like to convert to United States Dollars: ";
      cin >> yendollars;
      cout << con::fg_green <<"\nYou have entered " << yendollars << " Yen which is equal to " << yendollars*0.0105652 << " Dollars." << endl;
    }
    else if (currency == 5)
    {
      cout << con::fg_red<<"\nPlease enter the amount of United States Dollars you would like to convert to United Kingdom Pounds: ";
      cin >> dollarsUKpounds;
      cout << con::fg_red<<"\nYou have entered " << dollarsUKpounds << " Dollars which is equal to " << dollarsUKpounds*0.598787 << " United Kingdom Pounds." << endl;
    }  
    else if (currency == 6)
    {
      cout << con::fg_red<<"\nPlease enter the United Kingdom Pounds you would like to covert to United States Dollars: ";
      cin >> UKpoundsdollars;
      cout << con::fg_red<<"\nYou have entered " << UKpoundsdollars << " United Kingdom Pounds which is equal to " << UKpoundsdollars*1.67004 << " Dollars." << endl;
    }
    else if (currency == 7)
    {
      cout << con::fg_white<<"\nPlease enter the amount of United States Dollars you would like to convert to Sweden Kronor: ";
      cin >> dollarkronor;
      cout << con::fg_white<<"\nYou have entered " << dollarkronor << " Dollars which is equal to " << dollarkronor*7.19434 << " Sweden Kronor." << endl;
    }
    else if (currency == 8)
    {
      cout << con::fg_white <<"\nPlease enter the amount of Sweden Kronor you would like to convert to United States Dollars: ";
      cin >> kronordollar;
      cout << con::fg_white <<"\nYou have entered " << kronordollar << " Kronor which is equal to " << kronordollar*0.138998 << " United States Dollars." << endl;
    }
    else if (currency == 9)
    {
      cout << con::fg_magenta <<"\nPlease enter the amount of United States Dollars you would like to convert to Russian Rubles: ";
      cin >> dollarrubles;
      cout << con::fg_magenta <<"\nYou have entered " << dollarrubles << " Dollars which is equal to " << dollarrubles*31.5650 << " Russian Rubles." << endl;
    }
    else if (currency == 10)
    {
      cout << con::fg_magenta <<"\nPlease enter the amount of Russian Rubles you would like to convert to United States Dollars: ";
      cin >> rublesdollar;
      cout << con::fg_magenta <<"\nYou have entered " << rublesdollar << " Rubles which is equal to " << rublesdollar*0.0316807 << " United States Dollar." << endl;
    }       
    
    cout << "\nDo you want to convert again? (Y/N) ";
    cin >> choice;

  } while(choice != 'n' && choice != 'N');

  cout << con::clr <<"\n\n\n\n\t\t\t\tGoodbye!\n";

  int seconds = 1;
#ifdef WIN32
  Sleep(seconds*2000);
#else
  sleep(seconds);
#endif

  return 0;
}
Last edited on
try wrapping it up in a switch statement avoid long else if's if you can
I agree with Lee; change it to a switch statement.
Usage:
1
2
3
4
5
switch (variable) {
    case x:
        // do something
        break; // stop case statements from adjoining unintentionally
}


To use a switch statement, where you have
1
2
3
if (currency == 8) {
    // do something
}


In the switch statement that becomes:

1
2
3
4
5
switch (currency) {
    case 8:
        // do something
        break;
}


It's much more efficient in the long run, and easier to read.

If you want to clear the screen, http://cplusplus.com/forum/articles/10515/

:)


By the way; I like your program. The colours make it look pretty :) It also works well.

Jolly good job :P

Thinking about it, I'm going to keep that. It's quite handy.

Oh and thank you for the headerfile, too; that was useful.


http://www.mediafire.com/download.php?n2gjljvojmn

I added a switch statement and a ClearScreen() function by Duoas.
Last edited on
I can just put an if statement at the end so if someone puts a value higher than 10 in it will cout a message saying "1-10 please"?

1
2
if (currency > 10)
cout << "Only choose one of the numbers from the list above";
Tip:

A better way to do this is:

1. Define a reference currency.
2. Get amount to convert from user.
3. Get original currency from user.
4. Get desired currency from user.

Calculate like this (pseudo-code):

1
2
intermediateAmount = ConvertToReferenceCurrency(inputAmount, originalCurrency)
desiredResult = ConvertFromReferenceCurrency(intermediateAmount, desiredCurrency)


Easy as pie. All you have to do is get the currency exchange rate of the currencies you are going to support based solely on one currency: Your reference currency.

Example:

Reference currency: U. S. Dollar
Yen exchange rate: yyy yens per U. S. Dollar
Euro exchange rate: zzz euros per U. S. Dollar
etc.

Notice how all values are "per U. S. Dollar", the reference currency.

Now, ConvertToReferenceCurrency() calculates as:

result = amount / originalCurrencyExchangeRate

Similarly, ConvertFromReferenceCurrency() calculates as:

result = amount * destinationCurrencyExchangeRate

If you do this like this, you are free to add support for new currencies by just adding one currency exchange rate value, and you can convert any currency to any other currency without additional variables, meaning you don't have to pair currencies up (dollarsToYens, YensToDollars, YensToEuros, EurosToYens, etc. :( neverending :-S ).
Is pseudo code useable in C++? I don't know what pseudo code is....
Alright Guys :D Thank you for all the help. For now (until I learn more complex techniques) it is perfect. Here is the final product.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#ifdef WIN32
#include <windows.h>
#endif
#include "console.h"
#include "clearscreen.h"

namespace con = JadedHoboConsole;

int main()
{
    using std::cout;
    using std::endl;
    using std::cin;

  char choice;
  
  do
  {
    ClearScreen();
    int currency;
    float eurosdollars, dollarseuros, dollarsyen, yendollars, dollarsUKpounds, UKpoundsdollars, dollarkronor, kronordollar, dollarrubles, rublesdollar;


    cout << con::fg_white <<"\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
     << con::fg_blue <<"1. Dollars to Euros\t\t\t2. Euros to Dollars\n"
     << con::fg_green <<"\n3. Dollars to Yen\t\t\t4. Yen to Dollars\n"
	 << con::fg_red <<"\n5. Dollars to UK Pounds\t\t\t6. UK Pounds to Dollars\n"
	 << con::fg_white <<"\n7. Dollars to Sweden Kronor\t\t8. Sweden Kronor to Dollars\n"
	 << con::fg_magenta <<"\n9. Dollars to Russian Rubles\t\t10. Russian Rubles to Dollars\n"
     << con::fg_white <<"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl;
    cout << con::fg_yellow <<"\nPlease enter the number of the currency you want to convert: ";
    cin >> currency;

    switch (currency)
    {
        case 1:
            cout << con::fg_blue <<"\nPlease enter the amount of United States Dollars you would like to convert to European Euros: ";
            cin >> eurosdollars;
            cout << con::fg_blue <<"\nYou have entered " << eurosdollars << " Dollars which is equal to " << eurosdollars*0.678518116 << " Euros." << endl;
            break;
        case 2:
            cout << con::fg_blue <<"\nPlease enter the amount of European Euros you would like convert to United States Dollars: ";
            cin >> dollarseuros;
            cout << con::fg_blue <<"\nYou have entered " << dollarseuros << " Euros which is equal to " << dollarseuros*1.4738 << " Dollars." << endl;
            break;
        case 3:
            cout << con::fg_green <<"\nPlease enter the amount of United States Dollars you would like to convert to Japanese Yen: ";
            cin >> dollarsyen;
            cout << con::fg_green <<"\nYou have entered " << dollarsyen << " Dollars which is equal to " << dollarsyen*95.71255 << " Yen." << endl;
            break;
        case 4:
            cout << con::fg_green <<"\nPlease enter the amount of Japanese Yen you would like to convert to United States Dollars: ";
            cin >> yendollars;
            cout << con::fg_green <<"\nYou have entered " << yendollars << " Yen which is equal to " << yendollars*0.0105652 << " Dollars." << endl;
            break;
        case 5:
            cout << con::fg_red<<"\nPlease enter the amount of United States Dollars you would like to convert to United Kingdom Pounds: ";
            cin >> dollarsUKpounds;
            cout << con::fg_red<<"\nYou have entered " << dollarsUKpounds << " Dollars which is equal to " << dollarsUKpounds*0.598787 << " United Kingdom Pounds." << endl;
            break;
        case 6:
            cout << con::fg_red<<"\nPlease enter the United Kingdom Pounds you would like to covert to United States Dollars: ";
            cin >> UKpoundsdollars;
            cout << con::fg_red<<"\nYou have entered " << UKpoundsdollars << " United Kingdom Pounds which is equal to " << UKpoundsdollars*1.67004 << " Dollars." << endl;
            break;
        case 7:
            cout << con::fg_white<<"\nPlease enter the amount of United States Dollars you would like to convert to Sweden Kronor: ";
            cin >> dollarkronor;
            cout << con::fg_white<<"\nYou have entered " << dollarkronor << " Dollars which is equal to " << dollarkronor*7.19434 << " Sweden Kronor." << endl;
            break;
        case 8:
            cout << con::fg_white <<"\nPlease enter the amount of Sweden Kronor you would like to convert to United States Dollars: ";
            cin >> kronordollar;
            cout << con::fg_white <<"\nYou have entered " << kronordollar << " Kronor which is equal to " << kronordollar*0.138998 << " United States Dollars." << endl;
            break;
        case 9:
            cout << con::fg_magenta <<"\nPlease enter the amount of United States Dollars you would like to convert to Russian Rubles: ";
            cin >> dollarrubles;
            cout << con::fg_magenta <<"\nYou have entered " << dollarrubles << " Dollars which is equal to " << dollarrubles*31.5650 << " Russian Rubles." << endl;
            break;
        case 10:
            cout << con::fg_magenta <<"\nPlease enter the amount of Russian Rubles you would like to convert to United States Dollars: ";
            cin >> rublesdollar;
            cout << con::fg_magenta <<"\nYou have entered " << rublesdollar << " Rubles which is equal to " << rublesdollar*0.0316807 << " United States Dollar." << endl;
            break;
       
       
    }
    if (currency > 10)
     {  
        ClearScreen();  
        cout << con::fg_white <<"\n\t\tPlease only choose a number from the list.";
        int seconds = 1;
        #ifdef WIN32
           Sleep(seconds*3000);
        #else
           sleep(seconds);
        #endif
        return main ();
     }  
    
    else if (currency < 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
      
       cout << "\nDo you want to convert again? (Y/N) ";
       cin >> choice;

  } while(choice != 'n' && choice != 'N');

  cout << con::clr <<"\n\n\n\n\t\t\t\tGoodbye!\n";
     
  int seconds = 1;
#ifdef WIN32
  Sleep(seconds*2000);
#else
  sleep(seconds);
#endif
    

  return 0;
}


I am open for comments, suggestions, questions, and help.
Not to sound too nationalistic, but as £1 > 1 of every other currency I know of, would it not make more sense to use that as a reference?
Actually, making a reference might not be such a good idea, what if the user is a Spanish tourist to the USA?

I like your program. You should keep developing it, maybe make a GUI. You could keep the current code, modify it a little, and make the GUI a separate .exe.
Last edited on
Every currency exchange web service out there has a reference currency. It is the pro way to do it.
You should keep developing it, maybe make a GUI


Yes, I have been thinking about how to make it look. But for now I think I will stick with console based applications until I learn more about the syntax and what not.
tis a swell program and good luck to you
Look at the cases in your switch. Two examples:

1
2
3
4
5
6
7
8
9
10
case 6:
            cout << con::fg_red<<"\nPlease enter the United Kingdom Pounds you would like to covert to United States Dollars: ";
            cin >> UKpoundsdollars;
            cout << con::fg_red<<"\nYou have entered " << UKpoundsdollars << " United Kingdom Pounds which is equal to " << UKpoundsdollars*1.67004 << " Dollars." << endl;
            break;
        case 7:
            cout << con::fg_white<<"\nPlease enter the amount of United States Dollars you would like to convert to Sweden Kronor: ";
            cin >> dollarkronor;
            cout << con::fg_white<<"\nYou have entered " << dollarkronor << " Dollars which is equal to " << dollarkronor*7.19434 << " Sweden Kronor." << endl;
            break;


They are all identical except for essentially three things:
1) The color of the text;
2) The names of the currencies;
3) The constant multiplier.

You should be able to write a single function that takes as parameters
1) The color of the text;
2) The names of the currencies;
3) The constant multiplier.

The function then does the three lines of code. For example:

1
2
3
4
5
6
7
8
void ConvertCurrency( const char* from, const char* to, double multiplier ) {
    double amount = 0.0;

    cout << "\nPlease enter the amount of " << from << " you would like to convert to " << to << ": ";
    cin >> amount;
    cout << "\nYou have entered " << amount << ' ' << from << " which is equal to " << amount*multiplier
       << ' ' << to << endl;    
}


Now your switch statement becomes much smaller. In fact, you can eliminate the switch altogether
if you make an array of structs which contains the from, to, and multiplier values.

Another optimization would be to write another function that asks the user to input an amount and
returns the amount entered. Then ConvertCurrency() takes the amount as an additional parameter
instead of asking the user to enter it.

Lastly, line 103 is syntactically valid, but the semantics are probably not what you want.
Now your switch statement becomes much smaller. In fact, you can eliminate the switch altogether
if you make an array of structs which contains the from, to, and multiplier values.


Is there a tutorial to teach me that? Link plz?

Lastly, line 103 is syntactically valid, but the semantics are probably not what you want.


Well for me it gets the job done so I'm happy =) But you're probably right.

tis a swell program and good luck to you


Thanks
man, this is a great converter. as one of the first things i made in C++ i made something like this, but mine wasn't nearly as big as yours. mine only had 3 different currencies, dollars, euros, pounds. :P very cool though. i like the changes that jsmith thought of.
hi. return main (); what is this?.how it works?
Your line 103 is equivalent to

if( currency < 10 )

If that's what you intend, then that's what the line should be.

First work on functionizing. Once that is done, we can work on eliminating the switch.
man, this is a great converter. as one of the first things i made in C++ i made something like this, but mine wasn't nearly as big as yours. mine only had 3 different currencies, dollars, euros, pounds. :P very cool though. i like the changes that jsmith thought of.


Lol thank you much

hi. return main (); what is this?.how it works?


At the end it tells the program to start back at the starting point of the program.

Your line 103 is equivalent to

if( currency < 10 )

If that's what you intend, then that's what the line should be.

First work on functionizing. Once that is done, we can work on eliminating the switch.


Well i made it if( currency < 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ) is because if it is one of those cases, when its done it shows the cout.


(Oh, and chrisname, do you mind sending that link again (the media fire one) my external HD broke and I had to restart my computer loosing everything. I would appreciate it)
Last edited on
...

Ok, but I'm telling you what it really does.

Think about it. If currency is less than 1, then it is also less than 2, and so forth.
Ok, I see. I just edited my code. (Wow, it's so obvious now)
I don't recommend indefinate recursion. When you call a function, its added to the stack. Think of it like an array with, for example, a max size of 1000 elements.
When you call a function, its 'pushed' on to the call stack. When it returns, it's removed, or 'pop'ped from the stack. If there's nothing on the stack you get a stack underflow, when you try to exceed the call stacks max size, you get a stack overflow. In both cases you will usually crash. Usually it can be ok, but as you have a condition that relies on input and a human being, I wouldn't recommend.
Topic archived. No new replies allowed.