Solved

Currency Exchange Program

Posted on 2004-08-11
12
544 Views
Last Modified: 2010-04-15
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 CANADA 1.32352; /*canada dollars 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");
      
      /*build menu*/
            printf("----------------------------------------\n\n");
            printf("Press [1] for Argentina Pesos\n\n");
            printf("Press [2] for Canada Dollar\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);
            
            /*read currencies to convert*/
            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:
                              conversion = CANADA * &AMOUNT;
                              printf("\nYour U.S. Dollars equal $%.2f Canada Dollars\n\n",conversion);
                              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
Comment
Question by:contesa
  • 5
  • 3
  • 3
  • +1
12 Comments
 
LVL 3

Assisted Solution

by:Sasho
Sasho earned 200 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

by:Sasho
Sasho earned 200 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

by:Sasho
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

by:contesa
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

by:Jaime Olivares
Jaime Olivares earned 100 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

by:Sasho
Sasho earned 200 total points
ID: 11778738
Your variable selectionNumbers is not an array that is why you cannot store more than one value in it.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 3

Expert Comment

by:Sasho
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

by:contesa
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");
      
      /*build menu*/
            printf("----------------------------------------\n\n");
            printf("Press [1] for Argentina Pesos\n\n");
            printf("Press [2] for Canada Dollar\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);
      }            
            
            /*read currencies to convert*/
            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;
                              printf("\nYour U.S. Dollars equal $%.2f Canada Dollars\n\n",conversion);
                              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

by:
ankuratvb earned 200 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

by:ankuratvb
ankuratvb earned 200 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

by:ankuratvb
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

by:contesa
ID: 11790162
Thank you for your response....

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now