Help Me Please with My Currency Converter

I am a beginner in C++ so if I did anything wrong in this program, please explain it without mocking me. I wrote this program so that it could convert currency however every time I try it, it always gives me 0. In other words, all my conversions always end up being 0.

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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
  #include <iostream>
using namespace std;

float a;
string nameofcurr;
float p;
float d;
float r;
float e;

float pounds (float x, float p)
{
    if (nameofcurr == "dollars") //converts dollars to pounds
    {
         p = 1.64 * x;
         return (p);
    }
    else if (nameofcurr == "rupees") // converts rupees to pounds
    {
        p = 101.25 * x;
        return (p);
    }
    else if (nameofcurr == "euros") // converts euros to pounds
    {
        p = 1.19 * x;
        return (p);
    }
    else if (nameofcurr == "pounds") // converts pounds to pounds
    {
        cout << "The number you entered is already the converted amount \n";
    }

}

float euros (float x, float e)
{
     if (nameofcurr == "dollars" ) //converts dollars to euros
    {
        e = 1.37 * x;
        return (e);
    }
    else if (nameofcurr == "rupees")// converts rupees to euros
    {
        e = 84.86 * x;
        return (e);
    }
    else if (nameofcurr == "euros") // converts euros to euros
    {
        cout << "The number you entered is already the converted amount \n";
    }
    else if (nameofcurr == "pounds") // converts pounds to euros
    {
        e = .84 * x;
        return (e);
    }


}

float rupees (float x, float r) // converts dollars to rupees
{
     if (nameofcurr == "dollars") //converts dollars to euros
    {
        r = .016 * x;
        return (r);
    }
    else if (nameofcurr == "rupees")// converts rupees to euros
    {
        cout << "The number you entered is already the converted amount \n";
    }
    else if (nameofcurr == "euros") // converts euros to euros
    {
        r = .012 * x;
    }
    else if (nameofcurr == "pounds") // converts pounds to euros
    {
        e = .0099 * x;
        return (e);
    }

}

float dollars (float x, float d) // converts any of the three currencies to dollars
{
     if (nameofcurr == "dollars") //converts dollars to euros
    {
        cout << "The number you entered is already the converted amount \n";
    }
    else if (nameofcurr == "rupees")// converts rupees to euros
    {
        d = 61.95 * x;
        return (d);
    }
    else if (nameofcurr == "euros") // converts euros to euros
    {
        d = .73 * x;
        return (d);
    }
    else if (nameofcurr == "pounds") // converts pounds to euros
    {
        d = .61 * x;
        return (d);
    }

}

int main()
{

    float x;
    cout << "Enter the number of dollars, rupees, euros, or pounds you would like to convert (do nont forget to type out the currency name all lowercase!) \n";
    cin >> x >> nameofcurr;
    if (nameofcurr != "dollars" || nameofcurr != "rupees" || nameofcurr != "euros" || nameofcurr != "pounds" )
    {
        cout << "Please type the currency name correctly \n";
        cin >> nameofcurr;
    }
    cout << "What currency would you like to convert to? Enter the number that is next to the currency to indicate which one you want." << endl;
    cout << "1. Pounds " << endl;
    cout << "2. Euros "  << endl;
    cout << "3. Rupees " << endl;
    cout << "4. Dollars" << endl;
    cin >> a;
    if (a == 1)
    {

        cout << "The conversion gives you " << pounds (x, p) <<  " pounds" << endl;
    }
    else if (a == 2)
    {
        cout << "The conversion gives you " << euros (x, e) <<  " euros" << endl;
    }
    else if (a == 3)
    {
        cout << "The conversion gives you " << rupees (x, r) << " rupees" << endl;
    }
    else if (a == 4)
    {
        cout << "The conversion gives you " << dollars (x, d) << " dollars" << endl;
    }
    else
        cout << "Please enter 1, 2, 3, or 4 based on the currency type you would like. \n";
    return 0;
}


There are no errors given. Also, even when I type the currency name properly, it still tells me to spell it correctly. I need to know how to fix this as well. Thank you very much for your time!
@shalinmehta
Your problem, lies with this line of code.
if (nameofcurr != "dollars" || nameofcurr != "rupees" || nameofcurr != "euros" || nameofcurr != "pounds" )

You are using the OR symbol between nameofcur instead of AND, ( '&&' ). With OR, if one of the checks is false, the whole line becomes false, so you get the line that says the currency name is not spelled correctly. Change the checking, and you should be okay.

PS : To get your program working, I also had to add #include <string>
Last edited on
@whitenite1

Don't you mean that if one check is true, the whole line becomes true?
@NT3

Yeah, I think I did mean that. Hopefully, I didn't confuse the OP too much. ;) Thanks for pointing it out.
Okay thank you so much everybody! Also, is there any better way to write this program without the use of so many if statements?
@shalinmehta

You can use switch instead of multiple if statements......
Here's another approach:
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
123
124
125
126
127
128
129
130
131
132
133
#include <iostream>
#include <map>

using namespace std;

class CurrencyConverter
{
	protected:
	map<string, double> table;
	string name;
	
	public:
	CurrencyConverter(const string &name) : name(name)
	{
		table["pounds"] = 0.0;
		table["dollars"] = 0.0;
		table["rupees"] = 0.0;
		table["euros"] = 0.0;
	}
	
	virtual ~CurrencyConverter() = 0;
	virtual void Convert(double amount) = 0;
	
	void Change(double amount) { Convert(amount); }
	void Print(const string &currency)  
	{ 
		if (table.find(currency)!= table.end())
		{
			cout << table[name] << " " << name << " is worth " << table[currency] << " " << currency << endl << endl;
		}
		else
		{
			cout << "invalid or mis-spelt currency name, please try again." << endl << endl;
		}
	}
	
	void PrintAll() const 
	{
		cout << "The values are" << endl;
		for (map<string, double>::const_iterator it = table.begin(); it != table.end(); ++it)
		{
			cout << it->second << " " << it->first << endl;
		}
		
		cout << endl << endl;
	}
};

CurrencyConverter::~CurrencyConverter() {}

class RupeesConverter : public CurrencyConverter
{
	public:
	explicit RupeesConverter(double amount) : CurrencyConverter("rupees") { Convert(amount); }
	virtual ~RupeesConverter() {};
	void Convert(double amount)
	{ 
		table["pounds"]  = amount * 0.0099;
		table["dollars"] = amount * 0.016;
		table["rupees"]  = amount;
		table["euros"]   = amount * 0.012;
	}
};

class PoundsConverter : public CurrencyConverter
{
	public:
	explicit PoundsConverter(double amount) : CurrencyConverter("pounds") { Convert(amount); }
	virtual ~PoundsConverter() {};
	void Convert(double amount)
	{ 
		table["pounds"]  = amount;
		table["dollars"] = amount * 1.64;
		table["rupees"]  = amount * 101.25;
		table["euros"]   = amount * 1.19;
	}
};

class DollarsConverter : public CurrencyConverter
{
	public:
	explicit DollarsConverter(double amount) : CurrencyConverter("dollars") { Convert(amount); }
	virtual ~DollarsConverter() {};
	void Convert(double amount)
	{ 
		table["pounds"]  = amount * 0.61;
		table["dollars"] = amount;
		table["rupees"]  = amount * 61.95;
		table["euros"]   = amount * 0.73;
	}
};

class EurosConverter : public CurrencyConverter
{
	public:
	explicit EurosConverter(double amount) : CurrencyConverter("euros") { Convert(amount); }
	virtual ~EurosConverter() {};
	void Convert(double amount)
	{ 
		table["pounds"]  = amount * 0.84;
		table["dollars"] = amount * 1.37;
		table["rupees"]  = amount * 84.86;
		table["euros"]   = amount;
	}
};

int main()
{
	EurosConverter ec(200.00);
	ec.PrintAll();
	ec.Print("rupees");
	ec.Print("dollars");
	ec.Change(125.00);
	ec.PrintAll();
	ec.Print("pounds");
	ec.Print("rupees");

	PoundsConverter pc(253.20);
	pc.PrintAll();
	pc.Print("rupees");
	pc.Print("dollars");
	pc.Change(10525.00);
	pc.PrintAll();
	pc.Print("euros");
	pc.Print("rupees");
	pc.Change(617285.00);
	pc.PrintAll();
	pc.Change(2295.00);
	pc.PrintAll();
	
	/*etc*/
	return 0;	
}


Actually I don't think Change is needed. Convert can be used directly
Last edited on
Topic archived. No new replies allowed.