If the user enters a currency that is not recognized by the program, an error message should be displayed and the program should exit.
INPUT:
There are three inputs:
1. The source currency. This is the currency to be converted from, and can be one of three characters: '$' (Canadian Dollar), 'Y' (Japanese Yen) or 'E' (Euro).
2. The destination currency. This is the currency to be converted to, and must be one of the same three characters as for the source currency.
3. The source value. This is a floating-point number that is the amount of the source currency to be converted.
OUTPUT:
There are two possible outputs:
1. A statement showing the source and destination currencies and values, if there are no errors. Values should be rounded to 2 decimal places; or
2. A statement that an error occured, if the source and/or destination currency symbols were invalid.
EXCHANGE RATES:
Please use only these exchange rates (so the marking script can know the expected result precisely):
1 Canadian Dollar = 0.69 Euro 1 Canadian Dollar = 116.00 Yen 1 Euro = 166.60 Yen
USE OF SWITCH:
You must use a switch statement to determine the source currency and act accordingly. You are free to use "if" or "switch" to determine the destination currency.
USE OF A SUCCESS FLAG:
You must make use of a boolean variable to determine if an error occurred. Set this variable to 1 (i.e. "true") when the program starts, and change it to 0 (i.e. "false") if you encounter an invalid currency symbol. At the end of your switch, use an "if" statement to test this variable (called a "flag") to determine which output to give (the conversion or the error message).
SAMPLE:
Below are several samples showing the format of the prompts and the output. Please pay careful attention to the details and ensure your program behaves exactly the same.
Example 1: Convert Dollar to Yen
$ ./lab2.exe Enter the source currency: $ Enter the destination currency: Y Enter the value: 325.00 $325.00 = Y37700.00 $
Example 2: Convert Yen to Euro
$ ./lab2.exe Enter the source currency: Y Enter the destination currency: E Enter the value: 12345.67 Y12345.67 = E74.10 $
Example 3: Error: invalid currency symbol
$ ./lab2.exe Enter the source currency: A Enter the destination currency: $ Enter the value: 123.45 There was an error with the input. $
OTHER ERROR CONDITIONS:
You do not have to deal with error conditions other than entering an invalid currency symbol. In particular, you may assume the user will type one character for each currency. You may also assume the user enters a valid floating-point number for the source value.
char source_currency, destination_currency;
char newline;
scanf("%c%c", ¤cy, &newline);
The character the user entered for the currency is the first %c, the newline ('\n') is the second one. You can reuse the "newline" variable for the second scanf() (to read in the destination currency).
#include#define ... // use this to set the conversion rates. int main() { // Variable declarations, including: source and destination currencies, source and destination values, and the success flag. success = 1; switch( ... ) { ... // switch on the source currency; if any currency type is unknown, set success = 0. ... // otherwise do the requested conversion. } // Now do the output; if the success flag is "true" then output the conversion; // otherwise output the error statement. return 0; }
2 points: Gives correct values for conversions
2 points: Catches input errors (i.e. invalid currency symbols)
2 points: Uses "switch" properly and as specified
2 points: Uses the "success" flag properly and as specified
2 points: format of prompts and output statement as specified