enums are variables that can have only one of the given values.
Here, the typename of the enum is 'exType'. And possible values are 'BUY' and 'SELL'
You declare instances of the enum just like any other variable, and assign/compare the same way:
1 2 3 4 5
exType exchange; // like saying "int myvar;", it creates a variable of type 'exType' named 'exchange'
exchange = BUY; // set it to 'BUY'
if(exchange == SELL) // does it equal 'SELL'?
It's a scope problem. exType can't be defined in main. Just as you can't use a variable declared in main in another function, you can't use a type (in this case exType) that's defined in another function.
You need to put exType's definition in a broader scope. Probably global.
What is exType ex mean?
I thought I explained that in my post.
it creates a variable of type 'exType' named 'ex'.
1 2 3
int myvar; // creates an int named myvar
exType ex; // create an exType named ex
For future reference, it helps if you post the errors as well.
Anyway I tried compiling and got 1 warning and 2 errors.
main.cpp(19): warning C4700: uninitialized local variable 'exch' used
This warning is just what it says.
1 2 3
int exch; // you create exch here, but don't initialize it
while (exch == 1 || exch ==2 || exch ==3 || exch ==4 || exch <=0 || exch >=6) // then you immediate use it
If you don't initialize your variables, then their contents are unpredictable. exch might be 0, or it might be 5, or it might be -65461377. There's no way to know unless you assign it.
As for the errors:
1 2
1>main.cpp(90): error C4716: 'changeRate' : must return a value
1>main.cpp(107): error C4716: 'showMenu' : must return a value
Again these are pretty self explanitory. Your changeRate and showMenu functions are supposed to return a value, but you aren't returning anything.
That doesn't matter. It's wrong and needs to be fixed. Variables are unpredicatable if uninitialized. You need to initialize it if you're going to be reading it right away.
If it happens to work for you you are just getting [un]lucky. The same program running on someone else's computer might not work for them.
As to my question now is Since BUY and SELL from enum exType {BUY, SELL};
How do I make it when I input 1 it asked how much I'm buying . Because atm it asks how much I'm buying but right after asks how much is selling.
This is the same problem. You have your variable 'ex', but you never set it to anything. You have to set it to what you want before you can read from it.
Also -- you don't need that global exType ex;. Get rid of it. Earlier I meant the enum itself had to be global.
#include <iostream>
usingnamespace std;
double changeRate ();
void showRates(double rate);//function to show rate
int showMenu(double rate); //function to show menu
double rate = 0.7333;// default rate Global Variable
enum exType {BUY, SELL};
double exchangeCurrency (double rate, exType ex);
int main()
{
enum exchange {SellEuro = 1, BuyEuro, ChangeRate, ShowRate, Exit};
int exch;
while (exch == 1 || exch ==2 || exch ==3 || exch ==4 || exch <=0 || exch >=6)
{
showMenu(rate);
cin>>exch;
cout<<endl;
cout.setf(ios::fixed | ios::showpoint);
cout.precision(2);
double amount;//input amount
switch(exch)
{
case SellEuro:
{
// exchangeCurrency(rate, ex);
break;
}
case BuyEuro:
{
cout<<"Buying Euro: Enter the amount to exchange: ";
cin>>amount;
cout<<endl;
cout<<"You will get "<<amount/(rate*0.96)<<" USD"<<endl<<endl<<endl;
break;
}
case ChangeRate:
{
changeRate();
break;
}
case ShowRate:
{
showRates(rate);
cout<<endl<<endl;
break;
}
case Exit:
{
cout<<"Exiting . . . "<<endl<<endl;
break;
}
default: cout<<"That is not a valid choice."<<endl<<endl<<endl;
}
}
system("Pause");
return 0;
}
//double exchangeCurrency (double rate, exType ex)
// {
// cout<<"Selling Euro: Enter the amount to exchange: ";
// cin>>amount;
// cout<<endl;
// cout<<"You will get "<<amount/(rate*1.04)<<" USD"<<endl<<endl<<endl;
//}
void showRates(double rate)
{
cout.setf(ios::fixed | ios::showpoint);
cout.precision(4);
cout<<"The current rate is: "<<rate<<endl;
cout<<"The current BUY rate is: "<<rate*1.04<<endl;
cout<<"The current SELL rate is: "<<rate*0.96<<endl;
cout<<endl;
}
double changeRate ()
{
cout<<"Enter new rate: ";
cin>>rate;
cout<<endl<<endl;
}
int showMenu(double rate)
{
cout<<"CURRENCY EXCHANGE Euro<>US"<<endl<<endl;
cout<<"---------------------------"<<endl<<endl;
cout<<" BUY RATE SELL RATE"<<endl;
cout.setf(ios::fixed | ios::showpoint);
cout.precision(4);
cout<<"$1 = "<<rate*1.04<<" "<<rate*0.96<<endl<<endl;
cout<<"Select the option "<<endl<<endl
<<"1. Sell Euro"<<endl<<endl
<<"2. Buy Euro"<<endl<<endl
<<"3. Change Exchange Rate"<<endl<<endl
<<"4. Show Exchange Rate"<<endl<<endl
<<"5. Exit"<<endl<<endl
<<">> ";
}
PS: don't be a douche. The whole reason I didn't want to answer in PM is because I wanted it to be public so other people could benefit from it. If you erase your posts it defeats the point.
As Disch made clear, here's the original post for the benefit of others.
So, I'm in compsci first year in college and I have an assignment where I'm a very confused to what it means.
I don't want to spend a long time explaining the description and meaning for the program so to sum it all up, it's about exchange rates with buy or sell for euro to USD
I'm not sure if it's required to give all the information but I'm required to use the following functions:
double exchangeCurrency (double rate, exType ex);
double changeRate ();
void showRates(double rate);
int showMenu(double rate);
where exType is defined as enum exType {BUY, SELL};.
I've used all the other function EXCEPT exchangeCurrency because I have no clue what exType ex is .Well, I know exType is an enumeration which is an integer but I don't know what "ex" is or what I'm supposed to do with it . When I put it into my program I get multiple errors about exType and such. I know I'm not explaining clearly but I will be glad to explain in more detail after input on whether or not I need more detail. lol. All help is greatly appreciated .