Currency Converter....do I need pointers?


Hi,

I have been reading up on C++ for months now and have copied many examples but have now I have just set out on my own with trying a basic project and have become stuck almost almost straight away.

My idea is to create a currency converter where I enter the currency to be converted, then the amount in that currency and then the currency to be converted to.

Here is the code I have so far ( I appreciate its not much but I am only getting started ).

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
#include<iostream>
using namespace std;

string Currency1;
string Currency2;
float Amount1;
float Amount2;
double GBR = 1.64969;

int main () {
    
    cout << "Please enter the currency to be converted: " ;
    cin >> Currency1 ;
    cout << "Please enter the amount in " << Currency1 << " to be converted: " ;
    cin >> Amount1 ;
    cout << "Please enther the currency to be converted to: ";
    cin >> Currency2 ;
    
    if (Currency1 == "USD") {
        USD * Currency2
        
        
    }
    
        cout << "The amount in "<< Currency2 << " is: " << Amount1*Amount2 << "\n" ;
        
        return 0;
        
    }


I know that the line where I try to multiply the Amount1 with Currency2 is definitely wrong but I am wondering if anyone out there can help me with how to input a string and then link that through to multiplying by a number.

Thanks a bunch in advance!
There's really no reason to link them together. What you have now is perfectly fine. You get the currency they want to convert from. Then you get the amount of it. Lastly you get the currency they want to convert to. You don't need to actually link the currency to a value although you can using an std::map.

Here's your code fixed up.

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
#include <iostream>
#include <string>

int main()
{
	std::string currency1;
	std::string currency2;

	double amount1 = 0;
	double amount2 = 0;

	double poundToDollar = 0.61;
	double dollarToPound = 1.65;

	std::cout << "Please enter the currency to be converted from: ";
	std::cin >> currency1;

	std::cout << "Please enter the amount in " << currency1 << " to be converted: ";
	std::cin >> amount1;

	std::cout << "Please enter the currency to be converted to: ";
	std::cin >> currency2;

	if(currency1 == "USD" && currency2 == "Pound")
		amount2 += amount1 * dollarToPound;
	else if(currency1 == "Pound" && currency2 == "USD")
		amount2 += amount1 * poundToDollar;

	std::cout << "The amount in " << currency2 << " is " << amount2;

	std::cin.ignore();
	std::cin.ignore();

	return 0;
}


If you're going to expand on this and have many other conversions then I would probably read up on using std::map.
Last edited on

Thanks Renthalkx97!

I do indeed want to use more than one currency and so have been reading up on how to use std::map.

So far I have:

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
#include <iostream>
#include <map>

using namespace std;

string Currency1;
string Currency2;
float Amount1 = 0;
float Amount2 = 0;

int main () {
    
    map <string, float> rates;
    
    rates["EUR"] = 1.37757;
    rates["GBR"] = 1.64821;
    rates["INR"] = 0.01646;
    rates["AUD"] = 0.91220;
    rates["CAD"] = 0.89178;
    rates["ZAR"] = 0.09195;
    rates["NZD"] = 0.85401;
    rates["JPN"] = 1.64969;
    
    cout << "Please enter the currency to be converted: " ;
    cin >> Currency1 ;
    cout << "Please enter the amount in " << Currency1 << " to be converted: " ;
    cin >> Amount1 ;
    cout << "Please enther the currency to be converted to: ";
    cin >> Currency2 ;

    AmountinUSD = Amount1

    if (Currency2 == "USD" ) {
        Amount 2 =
        
    }
    
        cout << "The amount in "<< Currency2 << " is: " << Amount1*Amount2 << "\n" ;
        
        return 0;
        
    }


I think all of the rates (from USD to currency) are now in the map but I don't know how to call them. I was also thinking that to make it shorter I might include a line that converts which ever Currency1 it is into USD first and then from there into which ever Currency2 is. I am now not sure how to create that initial conversion into USD (line 31). Basically I need to know how to call the conversion rate associated with the string entered as Currency1.
Last edited on
http://www.cplusplus.com/reference/map/map/find/

see the example here, but instead of erasing the element (which the example does), you can get at your map contents with 'first' and 'second'.

For example:

1
2
auto it=rates.find("INR");
double myRate = it->second;


will get you your rate for INR.

p.s. you MUST #include<string>, and if i were you change your map to hold doubles rather than floats.
Last edited on
Topic archived. No new replies allowed.