Functions and classes

Very confused on how to do this assignment. My program is to create a currency exchange (pesos to dollars) using a class. I need to display out the the currency started with, the amount to be exchange, and the amount in dollars after exchanged.

From what I am understanding from the assignment, the showSelection function shows the user the options and then calls displayExchange to display results, The displayExchange function calls on outputType to get the type of money being exchanged. The outputType calls function convert to convert the currency into U.S. dollars.

I know my functions are the reason why the program is not working, probably a few other reasons to. But I don't know how to make it work. Any suggestions or examples will be appreciated. Thanks. Below is my code. I noted in bold comments what the compiler errors to all the time.

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

#include <iostream>

using namespace std;

//Name and members of class
class exchange

{
    public:
void showSelection();
void displayExchange(char& ch, double& num);
void outputType (char& type);
void convert (char& letter, double& num1);


private:

char moneyType;
double moneyAmount;

};



int main()

{



//calling showselection from class exchange
exchange showSelection();



return 0;

}

//function showSelection to get currency
void exchange::showSelection()
{

  cout<< "This program is to exchange your currency into other currencies \n\n" << endl;
  cout<< " Select your Currency \n"  << endl;
  cout << "(P) for pesos, (D) for U.S. dollars \n" << endl;
  cout << "(F) for Swiss francs, " "(E) for Euros, and\n" << endl;
  cout << "(Q) for quit program" << endl;
  cin>> moneyType;
  moneyType = static_cast<char>(toupper(moneyType));

//loop selection
  while (moneyType != 'Q')
 {
    switch (moneyType)
    {
        case 'P':
        case 'D':
        case 'F':
        case 'E':
        cout << "Enter money amount";
        cin >> moneyAmount;
        //function to display currency exchange
        displayExchange(moneyType, moneyAmount);

        break;
        case 'Q':
        cout<<"closing program";
        break;
        default :
        cout << "Invalid selection" << endl;
    }

cout<< "This program is to exchange your currency into other currencies \n\n" << endl;
  cout<< " Select your Currency \n"  << endl;
  cout << "(P) for pesos, (D) for U.S. dollars \n" << endl;
  cout << "(F) for Swiss francs, " "(E) for Euros, and\n" << endl;
  cout << "(Q) for quit program" << endl;
  cin>> moneyType;
  moneyType = static_cast<char>(toupper(moneyType));

 }

}



//function to print out display of currency
void exchange::displayExchange(char& moneyType, double& moneyAmount)

{


//THE MAJOR ERROR APPEARS TO BE HERE!!!!!!!!!! 
cout << " MoneyType = " << outputType(moneyType) << "\n Amount = " << moneyAmount;
  cout <<  "\n Exchange in U.S. dollars = " << convert(moneyType, moneyAmount);


}

//function to output type of money
void exchange::outputType (char& moneyType)
{


    if (moneyType = 'P')
    cout << "pesos";
    else if (moneyType = 'D')
    cout << "dollars";
    else if (moneyType = 'F')
    cout << "Swiss Francs";
    else (moneyType = 'E')
    cout << "Euros";

}
//functione to convert currency
void exchange::convert(char& moneyType, double& moneyAmount)
{


    if (moneyType = 'P')
    cout << (moneyAmount * 9.815);
    else if (moneyType = 'D')
    cout << moneyAmount;
    else if (moneyType = 'F')
    cout << (moneyAmount * 1.4054);
    else (moneyType = 'E')
    cout << (moneyAmount * .9553);

}


The first statement in main is not correct. Before using anything in your class, you should make an instance of that class.
exchange MyExchange;
Only then are you able to use the functions of the class instance MyExchange. You can do this by calling the function using the instance's name followed by a period.
MyExchange.showSelection();
I'd advice you to read more on classes and how to make them work. Good luck!
Last edited on
Thanks for the help. I have been reading, just having a hard time understanding it to make it work.
Topic archived. No new replies allowed.