Create and use a function to perform a calculation

Posted on 2004-08-31
Medium Priority
Last Modified: 2010-04-15
I must have a mental block.  I am having the hardest time trying to create and use a function to perform a calculation.  Listed below is the code.
I am trying to pass variables to a function and have it calcuate a float amount (dest_amt) using the convert function I attempted to creete.
I also need to figure out a way to accept an int return using a function for the menu selection instead of a scanf.  I started to create the accept function
for this.

Thanks in advance for your time.

#include <stdio.h>

  float convert (int y, float dollar_amt, float conv_factor, float dest_amt);
  int accept (int menu_option);
  int main()
         /* Initialize variables */
         static float conv_factor[5]={1.4013345291,110.864745,.54896794,.817126982,1.332196526};
         static int menu_option=0;
         static int x,y;
         static float dollar_amt,dest_amt;
         static char* title[1]={"Currency Converter"};
         static char* menu_option_prompt[1]={"Select Menu Option: "};
         static char* input_amt[1]={"Input amount to convert in U.S. Dollars $ "};
         static char* disp[2]={"The amount you input in U.S. Dollars is worth ", " in "};
         static char* cur[5]={"Australian Dollars.", "Japanese Yen.", "British Pounds.", "European Union Euros.", "Canadian Dollars."};
         static char* cur_menu[6]={"1) Australian Dollars", "2) Japanese Yen", "3) British Pounds", "4) European Union Euros", "5) Canadian Dollars", "6) Stop Program"};
         /* Input Section creating the menu options */
      while (menu_option !=6)
         x = 0;
         /* Build Menu */
        while (x !=6)
         x = x + 1;
        if(menu_option ==6)
        return 0;
        if(menu_option <1 || menu_option >6)
        return main();
        y = menu_option - 1;      
        /* Prompt for input amount */
        /* Function to calculate conversion and display data */
        float convert(int y, float dollar_amt, float conv_factor[y])
       float dest_amt=0;
       return (dest_amt);
           return 0;
Question by:nfunaro
LVL 23

Accepted Solution

brettmjohnson earned 2000 total points
ID: 11948368
You are going overboard on your arrays.

> static char* title[1]={"Currency Converter"};
> ...
>   printf(title[0]);
>   printf("\n\n");

can be simplified to

char* title = "Currency Converter";

Or even simpler:

  puts("Currency Converter\n");

>      x = 0;
>       /* Build Menu */
>       while (x !=6)
>               {
>       printf(cur_menu[x]);
>       printf("\n");
>       x = x + 1;
>            }

This loop has an initialization (x = 0), a top-of-loop-test (while (x != 6)), and an increment at the end ( x = x + 1 ).
When all these are present, it is preferable to use the for() loop syntax for much less clutter:

/* Build Menu */
for (x = 0; ( x < 6); x++)
  printf("%s\n", cur_menu[x]);

>       if(menu_option <1 || menu_option >6)
>        return main();

I am not sure recursion is the appropriate way to handle this type of error.
Your command loop already repeatedly prompts for a valid input.  I think
a break, continue, or even goto would be a better control flow construct
than recursion.

>     float convert(int y, float dollar_amt, float conv_factor[y])
>     {
>      float dest_amt=0;
>      dest_amt=(dollar_amt*conv_factor[y]);
>     return (dest_amt);
>     }

You cannot implement the function in the middle of the main() function.
It needs to stand alone.  However, since it does nothing other than a
multiplication, why not simply do that in-line and forego the extra function:

dest_amount = dollar_amt * conv_factor[y];

>        printf(disp[0]);
>       printf("%f",dest_amt);
>       printf(disp[1]);
>      printf(cur[y]);
>      printf("\n\n");

printf takes a format string that allows it to format for display several items,
strings numbers, delimiters, etc.  Learn how to use it effectively since long
lists of calls to printf displaying one item at a time is not only clutter, it is very
slow to process.

  printf("%s %f %s %s\n\n", disp[0], dest_amt, disp[1], cur[y]);

Make some of these simplifications, learn how to properly use printf() to
format several items for display at once, eliminate extraneous clutter and
complexity, and your true desired design will be much more clear to you.


Author Comment

ID: 11948397
This is really great feedback.  There is, however, one small problem.  I need to use a function to bring back the calculated amount.  This was the main reason for the question.  Everything else was fantastic help though.  Can you help with the function piece?

Featured Post

Will You Be GDPR Compliant by 5/28/2018?

GDPR? That's a regulation for the European Union. But, if you collect data from customers or employees within the EU, then you need to know about GDPR and make sure your organization is compliant by May 2018. Check out our preparation checklist to make sure you're on track today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Suggested Courses

601 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