Solved

# Currency Exchange Program

Posted on 2004-08-11
Medium Priority
565 Views
I see there are a lot of questions regarding this common assignment....

I'm trying to store the currency selection, calculate the conversion and then display the results...I'm just looking for suggestions. I really want to figure this out...

#include <stdio.h>

/*function prototyping*/
float fnCalculateConversion(int[],float[]);

/*main program starts here*/
int main()
{
/*define variables*/
int numberOfCurrencies;
int selectionNumbers;
int n;
int s;
float AMOUNT = 0.0;
#define ARGENTINA 2.95400; /*argentina pesos equal to \$1 US dollar*/
#define UNITED_KINGDOM 0.54382; /*united kingdom pounds equal to \$1 US dollar*/
#define HONG_KONG 7.79900; /*hong kong dollars equal to \$1 US dollar*/
#define JAPAN 109.626; /*japan yen equal to \$1 US dollar*/

/*program title*/
printf("Welcome to the Currency Conversion Program\n");

printf("----------------------------------------\n\n");
printf("Press [1] for Argentina Pesos\n\n");
printf("Press [3] for United Kingdom Pounds\n\n");
printf("Press [4] for Hong Kong Dollars\n\n");
printf("Press [5] for Japan Yen\n\n");
Printf("Press [q] to Quit Program\n\n");

do {
/*ask for user input -  numbers of currencies to convert*/
printf("How many of the above currencies would you like to convert [max=5]: ");
scanf("%d", &numberOfCurrencies);

} while (numberOfCurrencies >5 || numberOfCurrencies <1);

/*read number of currencies to convert from user*/
for (n=0; n<numberOfCurrencies; n++)

do {
/*ask for user input - currencies to convert*/
printf("Please enter selection number of currencies to convert: ");
scanf("%d", &selectionNumbers);

} while (selectionNumbers >5 || selectionNumbers <1);

/*ask for user input - dollar amount*/
printf("Enter dollar amount to convert: ");
scanf("%f", &AMOUNT);

for (s=0; s<selectionNumbers; s++)

fnCalculateConversion(selectionNumbers, AMOUNT);

/* display results */
getchar();

return 0;

}

/* function to calculate conversion depending on selection by user */
float fnCalculateConversion(int selectionNumbers[], float AMOUNT[])

{
/* declare variables */
float conversion;
switch(selectionNumbers)

{
/* switch-case statements */
case 1:
conversion = ARGENTINA * &AMOUNT;
printf("\nYour U.S. Dollars equal \$%.2f Argentina Pesos\n\n",conversion);
break;

case 2:
break;

case 3:
conversion = UNITED_KINGDOM * &AMOUNT;
printf("\nYour U.S. Dollars equal \$%.2f United Kingdom Pounds\n\n",conversion);
break;

case 4:
conversion = HONG_KONG * &AMOUNT;
printf("\nYour U.S. Dollars equal \$%.2f Hong Kong Dollars\n\n",conversion);
break;

case 5:
conversion = JAPAN * &AMOUNT;
printf("\nYour U.S. Dollars equal \$%.2f Japan Yen\n\n",conversion);
break;

}

/* returns calculation to user */
return conversion;

}
0
Question by:contesa
• 5
• 3
• 3
• +1

LVL 3

Assisted Solution

Sasho earned 800 total points
ID: 11778231
Try putting a { curly bracket after your for loop. The way the code is now the for loop loops only the do-while loop immediatly after it.
0

LVL 3

Assisted Solution

Sasho earned 800 total points
ID: 11778241
Then you need to close the for loop with }

I am assuming you want to do it after you display your results
0

LVL 3

Expert Comment

ID: 11778254
Also getchar will not display the results

Think about what your function is returning and where you are storing it.

0

Author Comment

ID: 11778477
Hi Sasho, thanks for the quick response...

The program actually runs (i'm shocked to say the least), the problem is that it doesn't store the user selection except for the last one entered. Then it displays that selection repeatedly, depending on how many currencies the user wanted to convert...did I confuse you yet?

For instance, user enters [3] for the number of currencies he wishes to conver --- maximum is only 5.
Then he enters the selection numbers from the menu (2, 3, 5)...
The program then asks for the dollar amount the user wants to convert.
When I press enter, I get three identical conversions (it seems to be only remembering the last menu selection and repeating that conversion).

My problem seems to be that the program isn't storing the menu selections....that's one of my problems...

0

LVL 55

Assisted Solution

Jaime Olivares earned 400 total points
ID: 11778515
I think you are turning your application a bit confusing with the question "how many currencies...?". Is it necessary?

As I see you are working with arrays, I will recommend you to store are your conversion factors in a single array, not in multiple #defines:

/* Argentina, Canada, UK, HK, Japan */
float CURRENCIES[] = {2.95400, 1.32352, 0.54382, 7.79900, 109.626 };

So, you can "map" the currency with the option selected easily, by example:
if Pesos is option 1 the factor is CURRENCIES[0]
if CanDollars is option 2, the factor is CURRENCIES[1]
Do you notice the "mapping" ?
Just convert your option variable in the proper array index.

if option = '2' then you need operate: option-'0', will give you 1   ('2' value is 50 decimal)

0

LVL 3

Assisted Solution

Sasho earned 800 total points
ID: 11778738
Your variable selectionNumbers is not an array that is why you cannot store more than one value in it.
0

LVL 3

Expert Comment

ID: 11778765
By the way what jaime is saying make a lot of sense as well. You should think along those lines.
0

Author Comment

ID: 11779582
Okay trying to follow instructions, this is what I have so far....however, I am getting a compiling error as follows:

line 68: bad types for * multiply
'(CURRENCIES) { case 1: conversion = CURRENCIES[0] * &AMOUNT'
aborting compile

Does this mean that I declared it incorrectly at the beginning of the program?

#include <stdio.h>

/*function prototyping*/
float fnCalculateConversion(float[],float[]);

/*main program starts here*/
int main()
{
/*define variables*/
int selectionNumbers;
int n;
int s;
float AMOUNT = 0.0;
float CURRENCIES[5] = {2.95400, 1.32352, 0.54382, 7.79900, 109.626};
/*conversions equal to \$1 US dollar respectively for argentina, canada,
united kingdon, hong kong, japan*/

/*program title*/
printf("Welcome to the Currency Conversion Program\n");

printf("----------------------------------------\n\n");
printf("Press [1] for Argentina Pesos\n\n");
printf("Press [3] for United Kingdom Pounds\n\n");
printf("Press [4] for Hong Kong Dollars\n\n");
printf("Press [5] for Japan Yen\n\n");
Printf("Press [q] to Quit Program\n\n");

do {
/*ask for user input - currencies to convert*/
printf("Please enter selection number of currencies to convert:");
scanf("%d", &selectionNumbers);

} while (selectionNumbers >5 || selectionNumbers <1);
{
/*ask for user input - dollar amount*/
printf("Enter dollar amount to convert: ");
scanf("%f", &AMOUNT);
}

for (s=0; s<selectionNumbers; s++)

fnCalculateConversion(CURRENCIES, AMOUNT);

/* display results */
getchar();

return 0;

}

/* function to calculate conversion depending on selection by user */
float fnCalculateConversion(float CURRENCIES[], float AMOUNT[])

{
/* declare variables */
float conversion;
switch(CURRENCIES)

{
/* switch-case statements */
case 1:
conversion = CURRENCIES[0] * &AMOUNT;
printf("\nYour U.S. Dollars equal \$%.2f Argentina Pesos\n\n",conversion);
break;

case 2:
conversion = CURRENCIES[1] * &AMOUNT;
break;

case 3:
conversion = CURRENCIES[2] * &AMOUNT;
printf("\nYour U.S. Dollars equal \$%.2f United Kingdom Pounds\n\n",conversion);
break;

case 4:
conversion = CURRENCIES[3] * &AMOUNT;
printf("\nYour U.S. Dollars equal \$%.2f Hong Kong Dollars\n\n",conversion);
break;

case 5:
conversion = CURRENCIES[4] * &AMOUNT;
printf("\nYour U.S. Dollars equal \$%.2f Japan Yen\n\n",conversion);
break;

}

/* returns calculation to user */
return conversion;

}

0

LVL 9

Accepted Solution

ankuratvb earned 800 total points
ID: 11780400
>float fnCalculateConversion(float CURRENCIES[], float AMOUNT[])
should be:

float fnCalculateConversion(float CURRENCIES[], float AMOUNT)

Since amount is a single float value and not an array.

Also,When you multiply:
>conversion = CURRENCIES[0] * &AMOUNT;
should be:
conversion = CURRENCIES[0] * AMOUNT;

Since you want the value in AMOUNT.
0

LVL 9

Assisted Solution

ankuratvb earned 800 total points
ID: 11780416
Also,you need to pass the selectionnumbers value to the function as well,and switch on that.

>switch(CURRENCIES)

CURRENCIES is an array and you can only switch on an integer.

Your fn .prototype should be something like:

float fnCalculateConversion(float CURRENCIES[], float AMOUNT,int selection)

and switch on the value of selection
switch(selection)
{
...
}
0

LVL 9

Expert Comment

ID: 11780431
Another thing:

You're printing the converted value in the fn. itself,and you're discarding the return value of the fn. as well.

In that case,you should make your function return void.
0

Author Comment

ID: 11790162

I have it running...one entry, one conversion, perform calculation, display result...using arrays........I'm now trying to expand for next week's assignment.

Thank you Jaime, Sasho, ankuratvb....you were all very helpful...I learned that I can't use an array to switch.

I am awarding all of you points.

Like Arnold says, I'll be back! :-)

/contesa
0

## Featured Post

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.