Link to home
Start Free TrialLog in
Avatar of MSAJM
MSAJM

asked on

need help with looping on currency conversion program

I need help setting up a loop to allow the user to choose from these 5 currencies and also be continuous until selecting "Quit" or "Exit".  Can you please tell me where to fit this into the following program and how to go about wording it.  Ex: if....then, or while....do.... statements???

//Week 3 Individual Assignment//
//Currency Conversion Program//
#include <stdio.h>

//Programming functions//

void TITLE_DISPLAY(void);
void USD_CONVERSION(void);
void CURR_CONVERSION(char*curr_name,float curr_conv_rate);
char curr_conversion;
void ERROR_CHECK(float user_input,float curr_conv_rate);

int main (void)  //Currency Conversion Version 1 10/14//

{
      char curr_name;     //Declaring variables for currency rates effective 10/14/03//
      float curr_conv_rate = 0.0;
      
      TITLE_DISPLAY();  //Displays title on screen//
      USD_CONVERSION(); //displays currency conversions on screen//
      CURR_CONVERSION("Canadian Dollar", 0.75572);//Canadian dollar conversion//
      CURR_CONVERSION("Japan Yen", 0.00919);//Japan Yen conversion//
      CURR_CONVERSION("Euro Dollar", 1.17323);//Euro conversion//
      CURR_CONVERSION("Chile Pesos", 0.00155);//Chile cunversion//
      CURR_CONVERSION("Australia Dollars", 0.68966);//Australia conversion//
return 0;
}

void TITLE_DISPLAY(void) //Displays Title of Program on screen//
{
      printf("\n");//displays blank line//
      printf("Currency Conversion Program");
      printf("\n\n");//displays blank line//
} //End Title Display function//

void USD_CONVERSION(void) //Displays currency conversion for 1 US dollar on screen//
{
      //variables//
      float cad_rate;
      float jpy_rate;
      float eur_rate;
      float clp_rate;
      float aud_rate;
      
      //Converstion rates as of 10/14/2003.//
      cad_rate = 1.3227; //Canadian Dollar//
      jpy_rate = 108.82; //Japan Jen//
      eur_rate = 0.8528; //Euro Dollar//
      clp_rate = 643.80; //Chile Pesos//
      aud_rate = 1.4507; //Australia Dollars//
      
      //displays currency conversions for 1 US dollar//
      printf("1.00 U.S. Dollar is equil to:");//displays "1.00 U.S. Dollar is equil to:" on screen//
      printf("\n\n");//prints blank line to screen//
      printf(" %.2f Canadian Dollars.",cad_rate);//displays Canadian Dollar conversion on screen//
      printf("\n");
      printf(" %.2f Japan Yen.",jpy_rate);
      printf("\n");
      printf(" %.2f Euro Dollar.",eur_rate);
      printf("\n");
      printf(" %.2f Chile Peso.",clp_rate);
      printf("\n");
      printf(" %.2f Australia Dollars.",aud_rate);
      printf("\n\n");

}  //End of USD conversion function.//

void CURR_CONVERSION(char*curr_name,float curr_conv_rate)  
//This function prompts the user for an amount of Canadian Dollars to convert, then displays the amount in U.S Dollars//
 
{
     //Declaration of variables.//

     float user_input;
     
     /*The following will prompt the user for Canadian Dollars input, will check if the
     input is a negative number or if it is a value that would produce the result
     more than $1,000.00 when converted, will computate conversion to U.S. Dollars,
     then display results on the screen.*/                        
     
     
     printf("\n");
     printf("Enter the amount of %s you wish to convert to U.S. Dollars? ",curr_name);
     
     do
     {
       scanf("%f",&user_input);
       ERROR_CHECK(user_input,curr_conv_rate);      //Calls the ERROR_CHECK function to check input.//            
     }
     while (user_input < 0.0 || (user_input*curr_conv_rate) > 1000.00);  //End do-while loop.//
               
     printf("\n");
     printf("%.2f",user_input);
     printf(" %s converts to $%.2f U.S. Dollars.",curr_name,(user_input*curr_conv_rate));
     printf("\n\n");

               
}  //End of CUR_CONVERSION function.//


void ERROR_CHECK(float user_input,float curr_conv_rate) //This function will check the user's input for validity//
     
     //Declaration of variables.//
     
        {
         if (user_input < 0.0) //Error checking input for negative number.//
         {
             printf("\n");
             printf("Amount entered is a negative number, please reenter a positive number. ");
         }
            else if ((user_input*curr_conv_rate) > 1000.00) //Error checking input for maximum reached.//
          {
              printf("\n");
              printf("Amount entered exceeds the maximum allowed, please reenter a lesser amount. ");
            }
            getch();
         
         } //End of ERROR_CHECK function.//
       
     
       
//End of program -- Version 2 of the Currency Conversion Program.//
Avatar of rthomsen
rthomsen

You'll want to put this loop in your main function in place of the current CURR_CONVERSION Calls.  Add a new variable and a loop that looks like this:

Note:  this is only pseudo code and will not work as is

int SelectCur = 0;

while SelectCur != 6
{
  //this loop will continue to prompt the user until they choose a value between 1 and 6
     do
     {
       printf("\n");
       printf("Select a currency type: 1-Canadian, 2-Japan,...,6-Quit");
       scanf("%d",&SelectCur;
     }
     while (SelectCur < 1 || SelectCur > 6);  //End do-while loop.//

    if (SelectCur == 1)
      {
        CURR_CONVERSION("Canadian Dollar", 0.75572);//Canadian dollar conversion//
      }
    if (SelectCur ==2 )
     {
       CURR_CONVERSION("Japan Yen", 0.00919);//Japan Yen conversion//
     }

//Add Similar if statements for the other conversion types, the main loop will exit when the  user chooses 6

}

Hope this helps.  Good luck!
Avatar of MSAJM

ASKER

OK, is there anything else I should take out of the program?  I am getting an error message on my compiler about a phrase error...

C:\Program Files\Miracle C\money7.c: line 139: Parse Error, expecting `'}''
'else if ((user_input*curr_conv_rate) > 1000.00)     { printf("\n")'
aborting compile

Here is the new program....

//Week 5 Individual Assignment//
//Currency Conversion Program//
#include <stdio.h>

//Programming functions//

void TITLE_DISPLAY(void);
void USD_CONVERSION(void);
void CURR_CONVERSION(char*curr_name,float curr_conv_rate);
char curr_conversion;
void ERROR_CHECK(float user_input,float curr_conv_rate);
float user_input;

int main (void)  //Currency Conversion Version 1 10/14//
{

      char curr_name;     //Declaring variables for currency rates effective 10/14/03//
      float curr_conv_rate = 0.0;
      int SelectCur = 0;
      while (SelectCur != 6)
{
      do
      {
      printf("Currency Conversion Program Version 3\n");
      printf("\n");
      printf("Select a currency: 1-Canadian, 2-Japan, 3-Euro, 4-Chile, 5-Australia, 6-Quit");
      scanf("%d", &SelectCur);
      }
      while (SelectCur < 1 || SelectCur > 6);
      if (SelectCur == 1)
      {
      CURR_CONVERSION("Canadian Dollar", 0.75572);
      }
      if (SelectCur == 2)
      {
      CURR_CONVERSION("Japan Yen", 0.00919);
      }
      if (SelectCur == 3)
      {
      CURR_CONVERSION("Euro Dollar", 1.17323);
      }
      if (SelectCur == 4)
      {
      CURR_CONVERSION("Chile Pesos", 0.00155);
      }
      if (SelectCur == 5)
      {
      CURR_CONVERSION("Australia Dollars", 0.68966);
      }
      {
      
      USD_CONVERSION(); //displays currency conversions on screen//
      CURR_CONVERSION("Canadian Dollar",curr_conv_rate = 0.75572);//Canadian dollar conversion//
      CURR_CONVERSION("Japan Yen",curr_conv_rate = 0.00919);//Japan Yen conversion//
      CURR_CONVERSION("Euro Dollar",curr_conv_rate = 1.17323);//Euro conversion//
      CURR_CONVERSION("Chile Pesos",curr_conv_rate = 0.00155);//Chile cunversion//
      CURR_CONVERSION("Australia Dollars",curr_conv_rate = 0.68966);//Australia conversion//
return 0;

}


 //Displays currency conversion for 1 US dollar on screen//
{
      //variables//
      float cad_rate;
      float jpy_rate;
      float eur_rate;
      float clp_rate;
      float aud_rate;
      
      //Converstion rates as of 10/14/2003.//
      cad_rate = 1.3227; //Canadian Dollar//
      jpy_rate = 108.82; //Japan Jen//
      eur_rate = 0.8528; //Euro Dollar//
      clp_rate = 643.80; //Chile Pesos//
      aud_rate = 1.4507; //Australia Dollars//
      
      //displays currency conversions for 1 US dollar//
      printf("1.00 U.S. Dollar is equil to:");//displays "1.00 U.S. Dollar is equil to:" on screen//
      printf("\n\n");//prints blank line to screen//
      printf(" %.2f Canadian Dollars.",cad_rate);//displays Canadian Dollar conversion on screen//
      printf("\n");
      printf(" %.2f Japan Yen.",jpy_rate);
      printf("\n");
      printf(" %.2f Euro Dollar.",eur_rate);
      printf("\n");
      printf(" %.2f Chile Peso.",clp_rate);
      printf("\n");
      printf(" %.2f Australia Dollars.",aud_rate);
      printf("\n\n");

}  //End of USD conversion function.//

//This function prompts the user for an amount of Canadian Dollars to convert, then displays the amount in U.S Dollars//
 
{
     //Declaration of variables.//

     float user_input;
     
     /*The following will prompt the user for Canadian Dollars input, will check if the
     input is a negative number or if it is a value that would produce the result
     more than $1,000.00 when converted, will computate conversion to U.S. Dollars,
     then display results on the screen.*/                        
     
     
     printf("\n");
     printf("Enter the amount of %s you wish to convert to U.S. Dollars? ",curr_name);
     
     do
     {
       scanf("%f",&user_input);
       ERROR_CHECK(user_input,curr_conv_rate);      //Calls the ERROR_CHECK function to check input.//            
     }
     while (user_input < 0.0 || (user_input*curr_conv_rate) > 1000.00);  //End do-while loop.//
               
     printf("\n");
     printf("%.2f",user_input);
     printf(" %s converts to $%.2f U.S. Dollars.",curr_name,(user_input*curr_conv_rate));
     printf("\n\n");

               
}  //End of CUR_CONVERSION function.//


//This function will check the user's input for validity//
     
     //Declaration of variables.//
     
        {
        float user_input;
         if (user_input < 0.0); //Error checking input for negative number.//
         {
             printf("\n");
             printf("Amount entered is a negative number, please reenter a positive number. ");
         }
            else if ((user_input*curr_conv_rate) > 1000.00) //Error checking input for maximum reached.//
         {
              printf("\n");
              printf("Amount entered exceeds the maximum allowed, please reenter a lesser amount. ");
            } //End of ERROR_CHECK function.//
         
         } //End of program -- Version 3 of the Currency Conversion Program.//


You have a semicolon at the end of your if statement

if (user_input < 0.0); //Error checking input for negative number.//


Parse error means that your semicolons are improperly placed (missing or added) or your bracketing is not correct.  You should check over all of your semicolons and bracketing.

Also..In your main function you should remove all of the CURR_CONVERSION function calls that come after your loop as these will continue to prompt the user for each one after they select quit.

I would also put the  USD_CONVERSION(); //displays currency conversions on screen//
function before the loop as this function just displays the conversion information.

good luck!
Avatar of MSAJM

ASKER

Cripes, I know you getting sick of me, but I am having problems.  I changed the semicolon, taken out the CURR_CONVERSION statements, and looked over the program, but I can't find what else is missing.  I have nearly pulled all my hair out.  I am now getting the message...

(null) Parse Error, expecting `'}''
''
aborting compile
which means a missing bracket???  Right?? Where is it?  Who knows?  Not me.  Please endulge my stupidity, this is my first programming class, and it has almost gotten the best of me.

//Week 5 Individual Assignment//
//Currency Conversion Program//
#include <stdio.h>

//Programming functions//

void TITLE_DISPLAY(void);
void USD_CONVERSION(void);
void CURR_CONVERSION(char*curr_name,float curr_conv_rate);
char curr_conversion;
void ERROR_CHECK(float user_input,float curr_conv_rate);
float user_input;

int main (void)  //Currency Conversion Version 1 10/14//
{

     char curr_name;     //Declaring variables for currency rates effective 10/14/03//
     float curr_conv_rate = 0.0;
     int SelectCur = 0;
     while (SelectCur != 6)
{
     do
     {
     printf("Currency Conversion Program Version 3\n");
     USD_CONVERSION(); //displays currency conversions on screen//
     printf("\n");
     printf("Select a currency: 1-Canadian, 2-Japan, 3-Euro, 4-Chile, 5-Australia, 6-Quit");
     scanf("%d", &SelectCur);
     }
     while (SelectCur < 1 || SelectCur > 6);
     if (SelectCur == 1)
     {
     CURR_CONVERSION("Canadian Dollar", 0.75572);
     }
     if (SelectCur == 2)
     {
     CURR_CONVERSION("Japan Yen", 0.00919);
     }
     if (SelectCur == 3)
     {
     CURR_CONVERSION("Euro Dollar", 1.17323);
     }
     if (SelectCur == 4)
     {
     CURR_CONVERSION("Chile Pesos", 0.00155);
     }
     if (SelectCur == 5)
     {
     CURR_CONVERSION("Australia Dollars", 0.68966);
     }
     {
     
return 0;

}


 //Displays currency conversion for 1 US dollar on screen//
{
     //variables//
     float cad_rate;
     float jpy_rate;
     float eur_rate;
     float clp_rate;
     float aud_rate;
     
     //Converstion rates as of 10/14/2003.//
     cad_rate = 1.3227; //Canadian Dollar//
     jpy_rate = 108.82; //Japan Jen//
     eur_rate = 0.8528; //Euro Dollar//
     clp_rate = 643.80; //Chile Pesos//
     aud_rate = 1.4507; //Australia Dollars//
     
     //displays currency conversions for 1 US dollar//
     printf("1.00 U.S. Dollar is equil to:");//displays "1.00 U.S. Dollar is equil to:" on screen//
     printf("\n\n");//prints blank line to screen//
     printf(" %.2f Canadian Dollars.",cad_rate);//displays Canadian Dollar conversion on screen//
     printf("\n");
     printf(" %.2f Japan Yen.",jpy_rate);
     printf("\n");
     printf(" %.2f Euro Dollar.",eur_rate);
     printf("\n");
     printf(" %.2f Chile Peso.",clp_rate);
     printf("\n");
     printf(" %.2f Australia Dollars.",aud_rate);
     printf("\n\n");

}  //End of USD conversion function.//

//This function prompts the user for an amount of Canadian Dollars to convert, then displays the amount in U.S Dollars//
 
{
     //Declaration of variables.//

     float user_input;
     
     /*The following will prompt the user for Canadian Dollars input, will check if the
     input is a negative number or if it is a value that would produce the result
     more than $1,000.00 when converted, will computate conversion to U.S. Dollars,
     then display results on the screen.*/                        
     
     
     printf("\n");
     printf("Enter the amount of %s you wish to convert to U.S. Dollars? ",curr_name);
     
     do
     {
       scanf("%f",&user_input);
       ERROR_CHECK(user_input,curr_conv_rate);      //Calls the ERROR_CHECK function to check input.//            
     }
     while (user_input < 0.0 || (user_input*curr_conv_rate) > 1000.00);  //End do-while loop.//
               
     printf("\n");
     printf("%.2f",user_input);
     printf(" %s converts to $%.2f U.S. Dollars.",curr_name,(user_input*curr_conv_rate));
     printf("\n\n");

               
}  //End of CUR_CONVERSION function.//


//This function will check the user's input for validity//
     
     //Declaration of variables.//
     
        {
        float user_input;
         if (user_input < 0.0) //Error checking input for negative number.//
        {
             printf("\n");
             printf("Amount entered is a negative number, please reenter a positive number. ");
        }
            else if ((user_input*curr_conv_rate) > 1000.00) //Error checking input for maximum reached.//
        {
              printf("\n");
              printf("Amount entered exceeds the maximum allowed, please reenter a lesser amount. ");
            } //End of ERROR_CHECK function.//
         
         } //End of program -- Version 3 of the Currency Conversion Program.//


No problem MSAJM, I'm happy to help.  Here are a couple of things I see.  In your main function: the last bracket before return 0; is an open bracket {  but it's not opening anything and you have not closed your while loop so change the { to a }.  Also, I can't see where your function headers are other than main eg. void USD_CONVERSION(void) .  Make sure these are in there and in the right place.

Here's a tip for bracketing - use indentation as it makes it easier to see where you need to open and close your brackets.
for example:

if (codition == true)
{
     statement 1;
     statement 2;
}

Hope this helps.  Stick with it, programming is hard at first but once you get a good understanding of it you might really like it.
Avatar of MSAJM

ASKER

Ok, one more thing.  I can actually get it it compile, build, and run now, but I don't think my loop is set up correctly.  When I type in the currency amount that I want to convert, the program ends and doesn't give me the currency exchange vlaue.  Can you help me out with this??

Here we go again....

//Week 5 Individual Assignment//
//Currency Conversion Program//
#include <stdio.h>

//Programming functions//
void TITLE_DISPLAY(void);
void USD_CONVERSION(void);
void CURR_CONVERSION(char*curr_name,float curr_conv_rate);
char curr_conversion;
void ERROR_CHECK(float user_input,float curr_conv_rate);

int main (void)  //Currency Conversion Version 3 10/28//
{

     char curr_name;     //Declaring variables for currency rates effective 10/14/03//
     float curr_conv_rate = 0.0;
     int SelectCur = 0;
     while (SelectCur != 6)
{
     TITLE_DISPLAY();
     USD_CONVERSION(); //displays currency conversions on screen//
     do
{
     printf("\n");
     printf("Select a currency: 1-Canadian, 2-Japan, 3-Euro, 4-Chile, 5-Australia, 6-Quit");
     scanf("%d", &SelectCur);
}
     while (SelectCur < 1 || SelectCur > 6);
     if (SelectCur == 1)
{
     CURR_CONVERSION("Canadian Dollar", 0.75572);
}
     if (SelectCur == 2)
{
     CURR_CONVERSION("Japan Yen", 0.00919);
}
     if (SelectCur == 3)
{
     CURR_CONVERSION("Euro Dollar", 1.17323);
}
     if (SelectCur == 4)
{
     CURR_CONVERSION("Chile Pesos", 0.00155);
}
     if (SelectCur == 5)
{
     CURR_CONVERSION("Australia Dollars", 0.68966);
}    
return 0;
}
}

void TITLE_DISPLAY(void) //Displays Title of Program on screen//
{
      
      printf("\n");//displays blank line//
      printf("Currency Conversion Program");
      printf("\n\n");//displays blank line//
} //End Title Display function//


void USD_CONVERSION(void)//Displays currency conversion for 1 US dollar on screen//
{
     //variables//
     float cad_rate;
     float jpy_rate;
     float eur_rate;
     float clp_rate;
     float aud_rate;
     
     //Converstion rates as of 10/14/2003.//
     cad_rate = 1.3227; //Canadian Dollar//
     jpy_rate = 108.82; //Japan Jen//
     eur_rate = 0.8528; //Euro Dollar//
     clp_rate = 643.80; //Chile Pesos//
     aud_rate = 1.4507; //Australia Dollars//
     
     //displays currency conversions for 1 US dollar//
     printf("1.00 U.S. Dollar is equil to:");//displays "1.00 U.S. Dollar is equil to:" on screen//
     printf("\n\n");//prints blank line to screen//
     printf(" %.2f Canadian Dollars.",cad_rate);//displays Canadian Dollar conversion on screen//
     printf("\n");
     printf(" %.2f Japan Yen.",jpy_rate);
     printf("\n");
     printf(" %.2f Euro Dollar.",eur_rate);
     printf("\n");
     printf(" %.2f Chile Peso.",clp_rate);
     printf("\n");
     printf(" %.2f Australia Dollars.",aud_rate);
     printf("\n\n");

}  //End of USD conversion function.//

//This function prompts the user for an amount of Canadian Dollars to convert, then displays the amount in U.S Dollars//
void CURR_CONVERSION(char*curr_name,float curr_conv_rate)
{
     //Declaration of variables.//

     float user_input;
     
     /*The following will prompt the user for Canadian Dollars input, will check if the
     input is a negative number or if it is a value that would produce the result
     more than $1,000.00 when converted, will computate conversion to U.S. Dollars,
     then display results on the screen.*/                        
     
     
     printf("\n");
     printf("Enter the amount of %s you wish to convert to U.S. Dollars? ",curr_name);
     
     do
     {
       scanf("%f",&user_input);
       ERROR_CHECK(user_input,curr_conv_rate);      //Calls the ERROR_CHECK function to check input.//            
     }
     while (user_input < 0.0 || (user_input*curr_conv_rate) > 1000.00);  //End do-while loop.//
               
     printf("\n");
     printf("%.2f",user_input);
     printf(" %s converts to $%.2f U.S. Dollars.",curr_name,(user_input*curr_conv_rate));
     printf("\n\n");

               
}  //End of CUR_CONVERSION function.//

void ERROR_CHECK(float user_input,float curr_conv_rate)
//This function will check the user's input for validity//
     
     //Declaration of variables.//
     
        {
         if (user_input < 0.0) //Error checking input for negative number.//
        {
             printf("\n");
             printf("Amount entered is a negative number, please reenter a positive number. ");
        }
            else if ((user_input*curr_conv_rate) > 1000.00) //Error checking input for maximum reached.//
        {
              printf("\n");
              printf("Amount entered exceeds the maximum allowed, please reenter a lesser amount. ");
            } //End of ERROR_CHECK function.//
         
         } //End of program -- Version 3 of the Currency Conversion Program.//

ASKER CERTIFIED SOLUTION
Avatar of rthomsen
rthomsen

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