Dec 24, 2013 at 6:02am Dec 24, 2013 at 6:02am UTC
@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 Dec 24, 2013 at 6:03am Dec 24, 2013 at 6:03am UTC
Dec 24, 2013 at 6:09am Dec 24, 2013 at 6:09am UTC
@whitenite1
Don't you mean that if one check is true, the whole line becomes true?
Dec 24, 2013 at 12:56pm Dec 24, 2013 at 12:56pm UTC
@NT3
Yeah, I think I did mean that. Hopefully, I didn't confuse the OP too much. ;) Thanks for pointing it out.
Dec 24, 2013 at 1:32pm Dec 24, 2013 at 1:32pm UTC
Okay thank you so much everybody! Also, is there any better way to write this program without the use of so many if statements?
Dec 24, 2013 at 2:17pm Dec 24, 2013 at 2:17pm UTC
@shalinmehta
You can use switch instead of multiple if statements......
Dec 24, 2013 at 5:31pm Dec 24, 2013 at 5:31pm UTC
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 ¤cy)
{
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 Dec 24, 2013 at 5:33pm Dec 24, 2013 at 5:33pm UTC