Link to home
Start Free TrialLog in
Avatar of cricket9601
cricket9601

asked on

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;
 
     }

               
}

SOLUTION
Avatar of PaulCaswell
PaulCaswell
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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;
     }

Avatar of cricket9601
cricket9601

ASKER

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');
        {
   
 
     }

               
 }