Mathematical Issue From Compiler?

For some odd reason when I convert euro into pound it gives me the incorrect answer even though the equation is correct... Why?


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

using namespace std;

class money{
private:
    string D = "Dollar";
    string P = "Pound";
    string E = "Euro";
    //double dollar;
    //double YEN_PER_DOLLAR;
    //double EUROS_PER_DOLLAR;

public:

    void Info(){
        string answer;
        string answer2;
        //string answer3;
        double amount;
        double amount2;
        //double amount3;
        //double rate;

        cout << "List of available currencies:" << endl;
        cout << "------------------------------" << endl;
        cout << D << endl;
        cout << P << endl;
        cout << E << endl;
        cout << endl;

        cout << "Currency you want to start with: ";
        cin >> answer;
        //MAKES SURE USER ENTERS VALID CURRENCY
        while(answer != "Dollar" && answer != "Euro" && answer != "Pound"){
            cout << "Enter a valid currency: ";
            cin >> answer;
        }

        cout << "Enter amount: ";
        cin >> amount;
        cout << "Currency you want to convert into: ";
        cin >> answer2;
        //DOLLAR->POUND
        if(answer == "Dollar" || answer == "dollar" && answer2 == "Pound" || answer2 == "pound"){
            cout << amount << " Dollar(s) = " << amount/1.47754137115 << " Pound(s)" << endl;
        }
        //DOLLAR->EURO
        else if(answer == "Dollar" || answer == "dollar" && answer2 == "Euro" || answer2 == "euro"){
            cout << amount << " Dollar(s) = " << amount/1.08601216333 << " Euro(s)" << endl;
        }
        //POUND->DOLLAR
        else if(answer == "Pound" || answer == "pound" && answer2 == "Dollar" || answer2 == "dollar"){
            cout << amount << " Pound(s) = " << amount/.67677314564 << " Dollar(s)" << endl;
        }
        //POUND->EURO
        else if(answer == "Pound" || answer == "pound" && answer2 == "Euro" || answer2 == "euro"){
            cout << amount << " Pound(s) = " << amount/.73496986623 << " Euro(s)" << endl;
        }
        //EURO->DOLLAR
        else if(answer == "Euro" || answer == "euro" && answer2 == "Dollar" || answer2 == "dollar"){
            cout << amount << " Euro(s) = " << amount/.92081031307 << " Dollar(s)" << endl;
        }
        //EURO->POUND
        else if(answer == "Euro" || answer == "euro" && answer2 == "Pound" || answer2 == "pound"){
            cout << amount << " Euro(s) = " << amount *.735 << " Pound(s)" << endl;
        //ERROR HERE: I INPUT 50 * .735 WHICH SHOULD = 36.75 BUT..
        //FOR SOME REASON I GET 54.3? WHY??
        }

    }
};

int main()
{
    cout << "*** CURRENCY CONVERTER ***" << endl;
    cout << "Remember this program is accurate as of 01/01/2016" << endl;
    cout << endl;

    money m;
    m.Info();

    return 0;
}
You should crank up your compiler to complain about everything, and then pay real close attention to what it says.

The problem is with precedence between || and &&. Use parentheses. For example:

44
45
46
47
        //DOLLAR->POUND
        if((answer == "Dollar" || answer == "dollar") && (answer2 == "Pound" || answer2 == "pound")){
            cout << amount << " Dollar(s) = " << amount/1.47754137115 << " Pound(s)" << endl;
        }

More commentary coming soon.
You don't get "54.3". You do get:
Currency you want to start with: Euro
Enter amount: 50.0
Currency you want to convert into: Pound
50 Euro(s) = 54.3 Dollar(s)

Dollars?

Furthermore, online compiler points out:
 In member function 'void money::Info()':
45:53: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
49:58: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
53:56: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
57:56: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
61:54: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
65:54: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
21:16: warning: unused variable 'amount2' [-Wunused-variable] 


In other words, the condition on line 61 is true, because 'answer' is "Euro". If the 'answer' is "euro", then the 'answer2' has to be tested too.
Last edited on
If you include <cctype> library, there is a function that can convert any uppercase character to lowercase.
 
tolower ( ch );


You can take advantage of that function. You can convert all letters in user input to lowercase and it can reduce the number of relational operations significantly.

You can use something like this...
1
2
3
4
cin >> answer;

for ( int iter = 0 ; iter < answer.length () ; iter ++ )
    answer [iter] = tolower ( answer [iter] );


So if user input is "Pound", it will be changed to "pound"
If I typed something stupid like "pOuNd", it will still be converted to "pound"

After you convert a string to all lowercase you can simply do this
 
if ( answer == "pound" && answer2 == "euro" )



Another way to simplify this process (It could be simpler for user too) is to use integer to select the option. Like this,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Pseudocode

Enter the amount: 
cin >> amount

Select the currency
1. Euro
2. Pound
3. Dollar

User types 1 for euro...

Currency you want to convert into
1. Euro
2. Pound
3. Dollar

User types 2 for pound

if ( answer == 1 && answer2 == 2 )
.
.
.


It's easier for us to just type 1 2 or 3 then typing the entire string.

It's easier for you to code the input validator for integers
Last edited on
Alright, time for my further comments...

You are trying to use a class for this, but it has a design error. Don't worry, it is a common problem when learning.

Your class should not be doing I/O or otherwise behaving as the main program.

Your class should only be interested in:
- keeping an amount
- keeping a currency type (which is the exchange rate)

The things it should know how to do are:
- change the currency rate (and update the amount to match)

Everything else should be done in main(): getting user input, asking for type to change to, etc.

Hope this helps.
Problem solved! Thanks for everyone's help. This is by FAR the best C++ community/website around. I just switched it around so the user just enters 1-3 to select their currency.
Duoas, thanks for the drop of knowledge. I will keep that in mind on future projects.

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

using namespace std;

class money{
private:
    string D = "Dollar";
    string P = "Pound";
    string E = "Euro";
    //double dollar;
    //double YEN_PER_DOLLAR;
    //double EUROS_PER_DOLLAR;

public:

    void Info(){
        int answer;
        int answer2;
        //string answer3;
        double amount;
        //double amount2;
        //double amount3;
        //double rate;

        cout << "List of available currencies:" << endl;
        cout << "------------------------------" << endl;
        cout << "1) " << D << endl;
        cout << "2) " << P << endl;
        cout << "3) " << E << endl;
        cout << endl;

        cout << "Currency you want to start with: ";
        cin >> answer;
        //MAKES SURE USER ENTERS VALID CURRENCY
        while(answer < 1 || answer > 3){
            cout << "Enter a valid currency: ";
            cin >> answer;
        }

        cout << "Enter amount: ";
        cin >> amount;
        cout << "Currency you want to convert into: ";
        cin >> answer2;
        //DOLLAR->POUND
        if(answer == 1 && answer2 == 2){
            cout << amount << " Dollar(s) = " << amount/1.47754137115 << " Pound(s)" << endl;
        }
        //DOLLAR->EURO
        else if(answer == 1 && answer2 == 3){
            cout << amount << " Dollar(s) = " << amount/1.08601216333 << " Euro(s)" << endl;
        }
        //POUND->DOLLAR
        else if(answer == 2 && answer2 == 1){
            cout << amount << " Pound(s) = " << amount/.67677314564 << " Dollar(s)" << endl;
        }
        //POUND->EURO
        else if(answer == 2 && answer2 == 3){
            cout << amount << " Pound(s) = " << amount/.73496986623 << " Euro(s)" << endl;
        }
        //EURO->DOLLAR
        else if(answer == 3 && answer2 == 1){
            cout << amount << " Euro(s) = " << amount/.92081031307 << " Dollar(s)" << endl;
        }
        //EURO->POUND
        else if(answer == 3 && answer2 == 2){
            cout << amount << " Euro(s) = " << amount *.735 << " Pound(s)" << endl;
        
        }

    }
};

int main()
{
    cout << "*** CURRENCY CONVERTER ***" << endl;
    cout << "Remember this program is accurate as of 01/01/2016" << endl;
    cout << endl;

    money m;
    m.Info();

    return 0;
}
Topic archived. No new replies allowed.