• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 452
  • Last Modified:

I am writing a currency conversion program that gives the user a choice of currency and a choice to convert another currency when finished.

Please look at my code.  I don't know what I am doing wrong.  I have 3 questions:
1.  I get the error   c:\program files\miracle c\moneyconversion v3 final.c: line 69: cannot redeclare function 'fnCalculate(makeSelection ,dollarValue)'    when I compile the program.  

2.The program is not testing for alpha characters in the do/while loop.  I've tried writing it several ways, but known work.

3.Also, please let me know if there are any other major errors.    Thanks,



#include <conio.h>
#include <stdio.h>
#include <stdlib.h>


/* print Money Conversion
  *Cricket9641
  *Last Update: 4/8/2006


  *The purpose of this program is give a user the choice to convert US Dollars to


  *Euro Dollars: conversion rate ==.833678
  *Canada Dollars:conversion rate ==1.167
  *Japanese Yen:conversion rate ==117.775
  *Switzerland Francs:conversion rate ==1.3025
  *Australia Dollars:conversion rate ==1.39567

  *Version 3.0--user chooses currency conversion:*/




  float fnCalculate(int, float);

  float CURRENCIES[5] = {.833678, 1.167, 117.775, 1.3025, 1.39567};

                                                
 int main()                        /*begin local variable*/
   {

 
   int makeSelection;                   
   int n;
   int s;
   float dollarValue = 0.0;
               
 
   printf("\t\t\t\tCurrency Conversion Program 3.0\n\n");                     /*tabs to center then displays title and skips four lines*/
   printf("\t\t\t\t---------------------------------\n\n\n\n");                  /*draw line and print menu*/
   
   
   printf("Press [1] for Euro Dollars\n\n\n");
   printf("Press [2] for Canada Dollars\n\n\n");
   printf("Press [3] for Japanese Yen\n\n\n");
   printf("Press [4] for Switzerland Francs\n\n\n");
   printf("Press [5] for Austalia Dollars\n\n\n");
 
           
   do {                                                      /*begin do-while loop*/
       
       printf("Good morning happy traveler!\n\n");                        /*welcome user screen*/
       printf("Please enter the number that corresponds to\n\n");
       printf("the currency you wish to convert today:\n\n");                   /*prompts user input*/
       scanf("%d", &makeSelection);                                    /*decimal signed int.*/
         
      } while (makeSelection >5 || makeSelection <1);                  /*check for user input; not less than 1, no more than 5*/
      
             
 
    printf("Please enter the amount of dollars you wish to convert today: ");     /*prompts user for dollar amount to convert*/
    scanf("%f", &dollarValue);
  }          
     
                                  
         
   fnCalculate(makeSelection ,dollarValue);

                                 /*display results*/
       
    getch();
         
    return 0;
         
  }        

   float fnCalculate(int makeSelection, float dollarValue) /*function; user input*/

  {
     
     float conversion;                  /*name variable*/
     switch (makeSelection)
   
  {
                                   /*program begins conversion*/
       case 1:
       conversion = CURRENCIES[0]*dollarValue;
       printf("\nYour U.S. Dollars equal $%.2f Euro Dollars\n\n",conversion);
       break;
                   
       case 2:
       conversion = CURRENCIES[1]*dollarValue;
       printf("\nYour U.S. Dollars equal $%.2f Canadian Dollars\n\n",conversion);
       break;
                         
       case 3:
       conversion = CURRENCIES[2]*dollarValue;
       printf("\nYour U.S. Dollars equal $%.2f Japanese Yen\n\n",conversion);
       break;
                         
       case 4:
       conversion = CURRENCIES[3]*dollarValue;
       printf("\nYour U.S. Dollars equal $%.2f Switzerland Francs\n\n",conversion);
       break;
                         
       case 5:
       conversion = CURRENCIES[4]*dollarValue;
       printf("\nYour U.S. Dollars equal $%.2f Austalian Dollars\n\n",conversion);
       break;
                         
   }
                            /*show user results of conversion*/
     do {
         return conversion;
        printf ("Would you like to continue (Y/N): ");
      scanf(%s, answer);
 
        while (toupper(answer) == 'Y');
        {
     return conversion;
 
     }

               
}

0
cricket9601
Asked:
cricket9601
2 Solutions
 
PaulCaswellCommented:
Hi cricket9601,

Firstly I'd like to say that this is the best currency-conversion  homework question solution I have seen, and I've seen many! :-)

You have obviously put a great deal of effort and thought into this. The formatting is of professional standard.The code is precise (apart from the most common errors we almost always see here). I believe you will go far! :-)

You have some common problems in this code:

>>       scanf("%d", &makeSelection);                              /*decimal signed int.*/
For a user to enter a number, they must press the return key. Here you are only reading the number and leaving the return character in the file stream. This return character will be read next time around as a zero value entered. The solution is to consume the entered return code by placing a matching return character at the end of the format. This will match and therefore consume the spurious return character.
       scanf("%d\n", &makeSelection);                              /*decimal signed int.*/

The same applies for:
>>    scanf("%f", &dollarValue);
and all other prompts.

Here:
   float fnCalculate(int makeSelection, float dollarValue) /*function; user input*/
  {
     float conversion;               /*name variable*/
     switch (makeSelection)
<HERE>
  {
       case 1:

you begin a case statement without a 'switch'. I will leave it to you to discover how to fix this.


The code:
                         /*show user results of conversion*/
     do {
        return conversion;
       printf ("Would you like to continue (Y/N): ");

will return immediately the 'return' instruction is executed. The 'printf' will never be performed. I doubt that that is what you intend. Ditto for the 'return' a few lines later.

Overall, I have only two extra suggestions.

Your prototype:
>>  float fnCalculate(int, float);
can also appear as:
   float fnCalculate(int makeSelection, float dollarValue);

The second option is generally considered to be of more benefit to other users of your code and is therefore encouraged.

I would suggest you dont try to put the request to the user for continuation in a separate function. It doesnt really belong where it is does it?

Make those changes and post what you've got if its still not working and we'll look at it again.

Paul
0
 
Kelvin_KingCommented:
To solve your compilation problem, I would remove the initial function declaration at the begining of the code:

>> float fnCalculate(int, float);

That should fix it.
0
 
MysidiaCommented:
You need to declare fnCalculate ahead of its definition in this case, you would put...

   float fnCalculate(int makeSelection, float dollarValue);

At the beginning of the program,  before the main()  function definition.

This is just an additional function declaration (without a function definition attached)

 OR

Move your main()  function below the definition of   your fnCalculate function.

(A function definition always has the additional effect of declaring the function)


The reason this is needed is that in C, a function must be declared in the file on a line earlier
in the file than the first call or other non-declaratory use of the function name appears in the program.

BUT,  unlike with variables,  if you don't declare the function first,  C doesn't flag an error for
"undeclared function", instead in the C programming language, the function is IMPLICITLY declared
on first use, if you didn't actually declare it.

As a function declaration which is equivalent to:  int the_function_name(...);

Calling a function without ever defining it will ordinarily cause an error to occur during the link phase
while building your program executable (not the compile phase);

Anyway, the reason for the particular error message you have is that at the point in your program where
the compiler sees this function call

  "fnCalculate(makeSelection ,dollarValue);"


It determines

(1)  'fnCalculate' is a function that has called, but not yet been declared

(2)  If a function is used but not declared, the compiler implictly declares it as an external function
      that  returns an int,  and has no specific list of parameters given.

Therefore,
      int  fnCalculate(...);   is created as an implied declaration created for the function.

It is just as if you put these statements in your code:
     extern int  fnCalculate();
     fnCalculate(makeSelection ,dollarValue);


However, you define the fnCalculate function later in the module:
  float fnCalculate(int makeSelection, float dollarValue)  { ... }

The first part of every function definition is a function declaration, SO you in effect
have two declarations of the function which conflict.

(The fact that the first one is an implicit declaration doesn't matter)

It is perfectly fine to declare the same function as many times as you want, as long as the
declaration is exactly the same every time.

Since a declaration for the function has already been established with a different function prototype,
the desired declaration failed.

0
 
MysidiaCommented:
re: PaulCaswell

>>       scanf("%d", &makeSelection);                              /*decimal signed int.*/
>For a user to enter a number, they must press the return key. Here you are only reading the number and leaving the >return character in the file stream. This return character will be read next time around as a zero value entered. The >solution is to consume the entered return code by placing a matching return character at the end of the format. This will >match and therefore consume the spurious return character.

Where did this come from?

\n is a whitespace character and will be ignored by scanf.

While it's harmless to add \n to the scanf format string, and it adds further constraint to
how the input can possibly be structured,  the return character will not be read or interpeted
as 0 or part of %d,  because scanf  skips all whitespace except when filling a character,
or string  format sequence.

In fact, the entry must begin with an integer, any non-integer non-whitespace will cause
scanf to fail entirely,  the result will be that the value of "makeSelection" is undefined after
the scanf call, bad input is the only case where the author's use of scanf fails,

and in that case it fails badly.

Ideally the input reading code should read more like...

     if ( scanf("%d", &makeSelection) < 1 ) {
         /* Ignore bad input */
          makeSelection = 0;
     }

0
 
cricket9601Author Commented:
I am just a beginner, and now I am totally confused.  
               if ( scanf("%d", &makeSelection) < 1 ) {
                 /* Ignore bad input */
                makeSelection = 0;
               }

Won't this do the same thing?
           do{
             canf("%d\n", &makeSelection);                                    
         
            } while (makeSelection >5 || makeSelection <1);
I still don't get the global function problem.  The function is declared before the main().  Can someone look at the changes and let me know where the problem is please? Do I have the libraries I need at the top?

#include <conio.h>
#include <stdio.h>
#include <stdlib.h>




/* print Money Conversion
  *Cricket9601
  *Last Update: 4/9/2006


  *The purpose of this program is give a user the choice to convert US Dollars to


  *Euro Dollars: conversion rate ==.833678
  *Canada Dollars:conversion rate ==1.167
  *Japanese Yen:conversion rate ==117.775
  *Switzerland Francs:conversion rate ==1.3025
  *Australia Dollars:conversion rate ==1.39567

  *Version 3.0--user chooses currency conversion:*/




  float fnCalculate(int makeSelection,float dollarValue);          /*Declare global variables*/

  float CURRENCIES[5] = {.833678, 1.167, 117.775, 1.3025, 1.39567}; /*Declare global variables*/

                                                
 int main()                        /*begin local variable*/
  {

 
   int makeSelection;                   
   int n;
   int s;
   float dollarValue = 0.0;
               
 
   printf("\t\t\t\tCurrency Conversion Program 3.0\n\n");                     /*tabs to center then displays title and skips four lines*/
   printf("\t\t\t\t---------------------------------\n\n\n\n");                  /*draw line and print menu*/
   
   
   printf("Press [1] for Euro Dollars\n\n\n");
   printf("Press [2] for Canada Dollars\n\n\n");
   printf("Press [3] for Japanese Yen\n\n\n");
   printf("Press [4] for Switzerland Francs\n\n\n");
   printf("Press [5] for Austalia Dollars\n\n\n");
 
           
   do {                                                      /*begin do-while loop*/
       
       printf("Good morning happy traveler!\n\n");                        /*welcome user screen*/
       printf("Please enter the number that corresponds to\n\n");
       printf("the currency you wish to convert today:\n\n");                   /*prompts user input*/
       scanf("%d\n", &makeSelection);                                    /*decimal signed int.; add \n so user does not have to press enter*/
         
      } while (makeSelection >5 || makeSelection <1);                  /*check for user input; not less than 1, no more than 5*/
      
             
 
    printf("Please enter the amount of dollars you wish to convert today: \n");     /*prompts user for dollar amount to convert*/
    scanf("%f\n", &dollarValue);
  {          
     
                                  /*program reading input*/
         
    float fnCalculate(int makeSelection,float dollarValue);

                                 /*display results*/
       
    return 0;
         
  }        

   float fnCalculate(int makeSelection, float dollarValue) /*function; user input*/

 
     
     float conversion;                  /*name variable*/
     switch (makeSelection)             /* Switch evaluates the expression (makeSelection)*/
    {
                                   /*program begins conversion*/
       case 1:
       conversion = CURRENCIES[1]*dollarValue;
       printf("\nYour U.S. Dollars equal $%.2f Euro Dollars\n\n",conversion);
       break;                              /* break-cases jumps out of the 'switch' block*/
                   
       case 2:
       conversion = CURRENCIES[2]*dollarValue;
       printf("\nYour U.S. Dollars equal $%.2f Canadian Dollars\n\n",conversion);
       break;                              /* break-cases jumps out of the 'switch' block*/
                         
       case 3:
       conversion = CURRENCIES[3]*dollarValue;
       printf("\nYour U.S. Dollars equal $%.2f Japanese Yen\n\n",conversion);
       break;                              /* break-cases jumps out of the 'switch' block*/
                         
       case 4:
       conversion = CURRENCIES[4]*dollarValue;
       printf("\nYour U.S. Dollars equal $%.2f Switzerland Francs\n\n",conversion);
       break;                              /* break-cases jumps out of the 'switch' block*/
                         
       case 5:
       conversion = CURRENCIES[5]*dollarValue;
       printf("\nYour U.S. Dollars equal $%.2f Austalian Dollars\n\n",conversion);
       break;                              /* break-cases jumps out of the 'switch' block*/
                                     
    }
                            /*show user results of conversion*/
     do {
         
        printf ("Would you like to continue (Y/N): \n");
      scanf("%s\n", answer);
 
        while (toupper(answer) == 'Y');
        {
   
 
     }

               
 }


0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now