Exchange Calculator using classes

I am a beginner in wit c++ and would appreciate some friendly advice. I made a exchange calculator from EUR to a chosen currency and I have to use classes.

If someone could help to improve(and explain it so that also I would understand) my quite hiddeous but compiling code, I would be very grateful.
As said, it compiles but I can't get the 0 == quit working.


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

.h file
#pragma once
#include <iostream>
using namespace std;

class Currency {

public:
	//Declaring the variables
	string currencyName;
	double conversionRate;

public:
	//Default Constructor
	Currency();
	//Constructor with two parameters
	Currency(string currencyName, double conversionRate);
	//Destructor
	~Currency();

};
.cpp file
#include <iostream>
#include "MyCurrencyConverter.h"
using namespace std;


Currency::Currency(string pCurrencyName, double pConversionRate)
{
	currencyName = pCurrencyName;
	conversionRate = pConversionRate;

}

Currency::Currency()
{
	currencyName = "";
	conversionRate = 0;
}

Currency::~Currency()
{

}
main.cpp file:
#include <iostream>
#include "MyCurrencyConverter.h"
using namespace std;


int main()
{
    //Initiate all wanted currencies(give value for name parameter and conversion rate parameter)
   

    //create parameters for user input choice=currency & eurAmount for amount of Euros to be exchanged
    int choice;
    double euroAmount;
    //Create Currency "Menu"
    cout << "####################################################################" << endl;
    cout << "##      Choose the Currency;                          ##" << endl;
    cout << "##                                                    ##" << endl;
    cout << "##  1 = Us Dollar                                     ##" << endl;
    cout << "##  2 = Uk Sterling                                   ##" << endl;
    cout << "##  3 = Japanese Yen                                  ##" << endl;
    cout << "##  4 = Australian Dollar                             ##" << endl;
    cout << "##  5 = Canadian Dollar                               ##" << endl;
    cout << "##  6 = Swiss Franc                                   ##" << endl;
    cout << "##  7 = Chinese Renminbi                              ##" << endl;
    cout << "##  0 = Quit                                          ##" << endl;
    cout << "##                                                    ##" << endl;
    cout << "##                                                    ##" << endl;
    cout <<"#########################################################" << endl;

    cin >> choice;//User Input for choosing the currency
    cout << "Give amount of Euros to be exchanged: " << endl;
    cin >> euroAmount;//user input for amount of euros

    //Giving values to objects in class Currency
    Currency usd("US Dollar", 1.10);
    Currency gbp("UK Sterling", 0.84);
    Currency jpy("Japanese Yen", 120.26);
    Currency aud("Australian Dollar", 1.66);
    Currency cad("Canadian Dollar", 1.47);
    Currency chf("Swiss Franc", 1.06);
    Currency rmb("Chinese Yuan", 7.70);



    //The if Loop for calculating the conversion based on user input. Based on choice the function calculates
    //The conversion from EUR  to the  chosen currency.
    if (choice == 1)
    {
         cout << " With " << euroAmount << " Euro, you will get " << euroAmount * usd.conversionRate << " " << usd.currencyName << "." << endl;
    }
    else if (choice == 2)
    {
        cout << " With " << euroAmount << " Euro, you will get " << euroAmount * gbp.conversionRate << " " << gbp.currencyName << "." << endl;
    }
    else if (choice == 3)
    {
        cout << " With " << euroAmount << " Euro, you will get " << euroAmount * jpy.conversionRate << " " << jpy.currencyName << "." << endl;
    }
    else if (choice == 4)
    {
        cout << " With " << euroAmount << " Euro, you will get " << euroAmount * aud.conversionRate << " " << aud.currencyName << "." << endl;
    }

    else if ( choice == 5)
    {
        cout << " With " << euroAmount << " Euro, you will get " << euroAmount * cad.conversionRate << " " << cad.currencyName << "." << endl;
    }
    else if ( choice == 6)
    {
        cout << " With " << euroAmount << " Euro, you will get " << euroAmount * chf.conversionRate << " " << chf.currencyName << "." << endl;
    }
    else if (choice == 7)
    {
        cout << " With " << euroAmount << " Euro, you will get " << euroAmount * rmb.conversionRate << " " << rmb.currencyName << "." << endl;
    }

    else
    {
        cout << "Try again" << endl;
    }
}

Your code looks good at first glance.

I suggest you, adding a method which performs the conversion directly:

 
double Currency::convert( double amount ) { return amount * conversionRate; }


A destructor is not needed in your sample class.
Also, I suggest removing the default constructor.
Last edited on
I overworked your program, now it's split into several functions.
The most difficult part is, handling the input in a manner that wrong input will get filtered out.

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

class Currency {

public:
	//Declaring the variables
	std::string currencyName;
	double conversionRate;

public:
	//Constructor with two parameters
	Currency( std::string currencyName, double conversionRate );
	std::string getName() const;
	double convert( double euroAmount ) const;
};


Currency::Currency( std::string pCurrencyName, double pConversionRate)
: currencyName{pCurrencyName}, conversionRate{pConversionRate}
{}

std::string Currency::getName() const { return currencyName; }

double Currency::convert( double euroAmount ) const 
{ 
    // return with two-digit precision for fraction part
    return std::round(euroAmount * conversionRate * 100) / 100.0;
} 


double readCurrencyInput()
{
    std::cout << "Input your Euro amount: ";
    
    while( true )
    {
        std::string line;
        double amount;
        std::getline( std::cin, line );
 
        // stod (string to double) throws an exception if conversion fails
        try {
            amount = std::stod( line );
        } catch (...) {
            std::cout << "You entered a wrong input, try again.\n";
            continue;  // continues the while loop
        }
        
        return amount;
    }
}

int readChoice()
{
    std::cout << "Make your choice: ";
    while( true )
    {
        std::string line;
        int choice;
        std::getline( std::cin, line);
        try {
            choice = std::stoi( line );
        } catch (...) {
            std::cout << "You entered a wrong input, try again.\n";
            continue;
        }
        if( choice >= 0 && choice <= 7 )
            return choice;
        else
            std::cout << "You entered a wrong number, try again.\n";
    }
}
    

void printMenu()
{
    std::cout 
         << "########################################################\n" 
         << "##      Choose the Currency;                          ##\n"
         << "##                                                    ##\n"
         << "##  1 = Us Dollar                                     ##\n"
         << "##  2 = Uk Sterling                                   ##\n"
         << "##  3 = Japanese Yen                                  ##\n"
         << "##  4 = Australian Dollar                             ##\n"
         << "##  5 = Canadian Dollar                               ##\n"
         << "##  6 = Swiss Franc                                   ##\n"
         << "##  7 = Chinese Renminbi                              ##\n"
         << "##  0 = Quit                                          ##\n"
         << "##                                                    ##\n"
         << "##                                                    ##\n"
         << "########################################################\n";  
} 


int main()
{
    using std::cout;

    //Giving values to objects in class Currency
    Currency usd("US Dollar", 1.10);
    Currency gbp("UK Sterling", 0.84);
    Currency jpy("Japanese Yen", 120.26);
    Currency aud("Australian Dollar", 1.66);
    Currency cad("Canadian Dollar", 1.47);
    Currency chf("Swiss Franc", 1.06);
    Currency rmb("Chinese Yuan", 7.70);

    double euroAmount = readCurrencyInput();
    
    // setting the ouput format
    cout << std::fixed << std::setprecision(2);
    
    while( true )
    {
        printMenu();
        int choice = readChoice();
        if( choice == 0 ) {
            std::cout << "Bye!\n";
            return 0;
        }
        
        cout << "With " << euroAmount << " Euro, you will get ";
        switch( choice )
        {
            case 1: cout << usd.convert( euroAmount ) << " " << usd.getName(); break;
            case 2: cout << gbp.convert( euroAmount ) << " " << gbp.getName(); break;
            case 3: cout << jpy.convert( euroAmount ) << " " << jpy.getName(); break;
            case 4: cout << aud.convert( euroAmount ) << " " << aud.getName(); break;
            case 5: cout << cad.convert( euroAmount ) << " " << cad.getName(); break;
            case 6: cout << chf.convert( euroAmount ) << " " << chf.getName(); break;
            case 7: cout << rmb.convert( euroAmount ) << " " << rmb.getName(); break;
        }
        cout << '\n';
    }    
}
Topic archived. No new replies allowed.