• C

Adding Conversions to my C Program.

I have the following program. I am trying to add to the program an area where the user enters the amount of foreign money to convert to US Dollars and the conversion is made. The out put is not correct and I need this immediately. Can you help?


//Headers for operations, etc.
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

int userinfo(int); //Calling error function, storing in memory

#define TRUE 1 //Flag for later use
#define FALSE 0 //Flag for later use

//Main Function for program
int main()
{

     /* Variables */
     float yen,canada,euro,real,aussie; /* 5 conversion variables - each variable is the US dollar equivalent to 1 foreign currency */
     int currency; /* the currency/money the user defined */
     float convert; //number of foreign currency to convert into USD
     char curr[50];

   while (currency != 6) //While loop to execute while user information is between 1 and 6.
   {
      /*Call Input Validation function*/
      currency = userinfo(currency);
     
      convert = (float)atof(curr);
     
     
     switch (currency) //CASE statement for the different current selections
     {
          case 1:      /* Japanese Yen Selection */
               yen = 0.00848104;
               printf("\n");
               printf("You have %f US Dollars.",yen*convert);
              // printf("%f Japanese Yen = %f",yen,yen*convert);
              // printf(" US Dollar\n");
               break;

          case 2:      /* Canadian Dollar Selection */
               canada = 0.863782;
               printf("\n");
               printf("%f Canadian Dollar = 1",canada);
               printf(" US Dollar\n");
               break;

          case 3:      /* European Euro Selection */
               euro = 1.1882;
               printf("\n");
               printf("%f European Euro = 1",euro);
               printf(" US Dollar\n");
               break;

          case 4:      /* Brazilian Real Selection */
               real = 0.47259;
               printf("\n");
               printf("%f Brazilian Real = 1",real);
               printf(" US Dollar\n");
               break;

          case 5:      /* Australian Dollar Selection */
               aussie = 0.738002;
               printf("\n");
               printf("%f Australian Dollar = 1",aussie);
               printf(" US Dollar\n");
               break;
               
          case 6:      /*User exits program*/
                printf("\n");
                printf("Thank You. Have a nice day. Now exiting the program.\n\n");
                break;

          default:     /* Default message in the event user does not enter 1 through 5 selection */
               printf("\n");
               printf("Please rerun the program and enter a number between 1 and 5.");
               printf("\nWould you want to continue [Y for Yes, anything else for No]?");
               fflush(stdin);
               currency = getch();

               if(currency == 'y' || currency == 'Y') /*Error checking to continue or end the program */
               {
                    main();
                    return 0;
               }
               else
   return 0;

         
         }  //CASE statement end
   } //WHILE loop end

     /* Display the results on the screen */
     fflush(stdout);
     printf("\nPress any key to end.");
     getche();
 
}  //MAIN end

//Error checking function
int userinfo()
{
      //Function local variables.
      char curr [50]=""; //array defined
      int i = 0; //counter for loop
      int flag = TRUE; //Flag
      int currency = 0;
      int ucurrency;
     

     
     /* Display the five possible curreny conversions */
     printf("\nCurrency Conversion \n\n");
     printf("1) Japanese Yen\n");
     printf("2) Canadian Dollar\n");
     printf("3) European Euro\n");
     printf("4) Brazilian Real\n");
     printf("5) Australian Dollar\n");
     printf("6) Exit Program\n\n");
     printf("Please select a currency conversion to display: ");
     fgets (curr, 50, stdin); //received and stores user input
     printf("\nEnter the amount of the Currency that you wish to convert: ");
     scanf("%s",curr);
     
     while(1) //While loop to check user information/input is valid.
      {
        for( i=0; curr[i] != '\0'; i++)
                {
                   if(!isdigit(curr[i]) || curr[i] != '.')
                     flag = FALSE;
                }
         if (flag = FALSE)
            {
               printf("\nYou have entered an invalid input. Please enter a value between 1-6."); //Error message to user.
            }
         else if  (flag = TRUE)
            {
               currency = atoi(curr);
            }
         if(flag = TRUE)
                 break;
       }
   return(currency); //returns value
}  
byrianrAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PaulCaswellCommented:
Hi byrianr,

Your problem is that you have two declarations of 'curr', one in userinfo and one in main. These are completely separate. Writing to one wont write to the other.

I would suggest you take bothe of them out and use a single 'curr' in global space.

E.G.

//Main Function for program
int main()
{

     /* Variables */
     float yen,canada,euro,real,aussie; /* 5 conversion variables - each variable is the US dollar equivalent to 1 foreign currency */
     int currency; /* the currency/money the user defined */
     float convert; //number of foreign currency to convert into USD
     char curr[50]; //<<<< Remove this

...

//Error checking function
int userinfo()
{
      //Function local variables.
      char curr [50]=""; //array defined //<<<< Remove this

Then add a global version of it:

#define TRUE 1 //Flag for later use
#define FALSE 0 //Flag for later use

char curr [50]=""; //<<<< Add this

//Main Function for program
int main()
{





Paul
byrianrAuthor Commented:
Thank you, but this does not output the correct output. I need the program to show the choices of currency to convert, then I need the user to input the amount of foreign money to convert, the program make the conversion after error checking and then display the converted amount in USD. What happens now is when the user inputs the choice from the menu and then the amount to convert is asked, the output is the choice from the menu.

//Headers for operations, etc.
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

int userinfo(int); //Calling error function, storing in memory

#define TRUE 1 //Flag for later use
#define FALSE 0 //Flag for later use

char curr [50]="";

//Main Function for program
int main()
{

     /* Variables */
     float yen,canada,euro,real,aussie; /* 5 conversion variables - each variable is the US dollar equivalent to 1 foreign currency */
     int currency; /* the currency/money the user defined */
     float convert; //number of foreign currency to convert into USD
     //char curr[50];

   while (currency != 6) //While loop to execute while user information is between 1 and 6.
   {
      /*Call Input Validation function*/
      currency = userinfo(currency);
     
      convert = (float)atof(curr);
     
     
     switch (currency) //CASE statement for the different current selections
     {
          case 1:      /* Japanese Yen Selection */
               yen = 0.00848104;
               printf("\n");
               printf("You have %f US Dollars.",yen*convert);
              // printf("%f Japanese Yen = %f",yen,yen*convert);
              // printf(" US Dollar\n");
               break;

          case 2:      /* Canadian Dollar Selection */
               canada = 0.863782;
               printf("\n");
               printf("%f Canadian Dollar = 1",canada);
               printf(" US Dollar\n");
               break;

          case 3:      /* European Euro Selection */
               euro = 1.1882;
               printf("\n");
               printf("%f European Euro = 1",euro);
               printf(" US Dollar\n");
               break;

          case 4:      /* Brazilian Real Selection */
               real = 0.47259;
               printf("\n");
               printf("%f Brazilian Real = 1",real);
               printf(" US Dollar\n");
               break;

          case 5:      /* Australian Dollar Selection */
               aussie = 0.738002;
               printf("\n");
               printf("%f Australian Dollar = 1",aussie);
               printf(" US Dollar\n");
               break;
               
          case 6:      /*User exits program*/
                printf("\n");
                printf("Thank You. Have a nice day. Now exiting the program.\n\n");
                break;

          default:     /* Default message in the event user does not enter 1 through 5 selection */
               printf("\n");
               printf("Please rerun the program and enter a number between 1 and 5.");
               printf("\nWould you want to continue [Y for Yes, anything else for No]?");
               fflush(stdin);
               currency = getch();

               if(currency == 'y' || currency == 'Y') /*Error checking to continue or end the program */
               {
                    main();
                    return 0;
               }
               else
   return 0;

         
         }  //CASE statement end
   } //WHILE loop end

     /* Display the results on the screen */
     fflush(stdout);
     printf("\nPress any key to end.");
     getche();
 
}  //MAIN end

//Error checking function
int userinfo()
{
      //Function local variables.
      //char curr [50]=""; //array defined
      int i = 0; //counter for loop
      int flag = TRUE; //Flag
      int currency = 0;
      int ucurrency;
     

     
     /* Display the five possible curreny conversions */
     printf("\nCurrency Conversion \n\n");
     printf("1) Japanese Yen\n");
     printf("2) Canadian Dollar\n");
     printf("3) European Euro\n");
     printf("4) Brazilian Real\n");
     printf("5) Australian Dollar\n");
     printf("6) Exit Program\n\n");
     printf("Please select a currency conversion to display: ");
     fgets (curr, 50, stdin); //received and stores user input
     printf("\nEnter the amount of the Currency that you wish to convert: ");
     scanf("%s",curr);
     
     while(1) //While loop to check user information/input is valid.
      {
        for( i=0; curr[i] != '\0'; i++)
                {
                   if(!isdigit(curr[i]) || curr[i] != '.')
                     flag = FALSE;
                }
         if (flag = FALSE)
            {
               printf("\nYou have entered an invalid input. Please enter a value between 1-6."); //Error message to user.
            }
         else if  (flag = TRUE)
            {
               currency = atoi(curr);
            }
         if(flag = TRUE)
                 break;
       }
   return(currency); //returns value
}  
PaulCaswellCommented:
Problem number one is here:

     fgets (curr, 50, stdin); //received and stores user input
     printf("\nEnter the amount of the Currency that you wish to convert: ");
     scanf("%s",curr);

Here you are reading the currency number into 'curr' and then reading the amount into 'curr'. I doubt if this is what you want to do. Try reading the currency into 'curr' and then using 'atoi' to determine its integer value, save that as an integer called say currencyType. Then read the currency amount in and, using 'atof', store that in a 'float' variable called, say 'currencyAmount'.

Try that and see what you get. Post code again if you get stuck.

Paul
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

byrianrAuthor Commented:
OK. So now it seems to be working, but only for values up to 6, can you help?

//Headers for operations, etc.
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

int userinfo(int); //Calling error function, storing in memory

#define TRUE 1 //Flag for later use
#define FALSE 0 //Flag for later use

char curr [50]="";

//Main Function for program
int main()
{

     /* Variables */
     float yen,canada,euro,real,aussie; /* 5 conversion variables - each variable is the US dollar equivalent to 1 foreign currency */
     int currency; /* the currency/money the user defined */
     float convert; //number of foreign currency to convert into USD
     //char curr[50];

   while (currency != 6) //While loop to execute while user information is between 1 and 6.
   {
      /*Call Input Validation function*/
      currency = userinfo(currency);
     
      convert = (float)atof(curr);
     
     
     switch (currency) //CASE statement for the different current selections
     {
          case 1:      /* Japanese Yen Selection */
               yen = 0.00848104;
               printf("\n");
               printf("You have %f US Dollars.",yen*convert);
               break;

          case 2:      /* Canadian Dollar Selection */
               canada = 0.863782;
               printf("\n");
               printf("You have %f US Dollars.",canada*convert);
               break;

          case 3:      /* European Euro Selection */
               euro = 1.1882;
               printf("\n");
               printf("You have %f US Dollars.",euro*convert);
               break;

          case 4:      /* Brazilian Real Selection */
               real = 0.47259;
               printf("\n");
               printf("You have %f US Dollars.",real*convert);
               break;

          case 5:      /* Australian Dollar Selection */
               aussie = 0.738002;
               printf("\n");
               printf("You have %f US Dollars.",aussie*convert);
               break;
               
          case 6:      /*User exits program*/
                printf("\n");
                printf("Thank You. Have a nice day. Now exiting the program.\n\n");
                break;

          default:     /* Default message in the event user does not enter 1 through 5 selection */
               printf("\n");
               printf("Please rerun the program and enter a number between 1 and 5.");
               printf("\nWould you want to continue [Y for Yes, anything else for No]?");
               fflush(stdin);
               currency = getch();

               if(currency == 'y' || currency == 'Y') /*Error checking to continue or end the program */
               {
                    main();
                    return 0;
               }
               else
   return 0;

         
         }  //CASE statement end
   } //WHILE loop end

     /* Display the results on the screen */
     fflush(stdout);
     printf("\nPress any key to end.");
     getche();
 
}  //MAIN end

//Error checking function
int userinfo()
{
      //Function local variables.
      //char curr [50]=""; //array defined
      int i = 0; //counter for loop
      int flag = TRUE; //Flag
      int currency = 0;
      int ucurrency,userenter;
     

     
     /* Display the five possible curreny conversions */
     printf("\nCurrency Conversion \n\n");
     printf("1) Japanese Yen\n");
     printf("2) Canadian Dollar\n");
     printf("3) European Euro\n");
     printf("4) Brazilian Real\n");
     printf("5) Australian Dollar\n");
     printf("6) Exit Program\n\n");
     printf("Please select a currency conversion to display: ");
     scanf("%d",&userenter);
     //fgets (curr, 50, stdin); //received and stores user input
     printf("\nEnter the amount of the Currency that you wish to convert: ");
     scanf("%s",curr);
     
     while(1) //While loop to check user information/input is valid.
      {
        for( i=0; curr[i] != '\0'; i++)
                {
                   if(!isdigit(curr[i]) || curr[i] != '.')
                     flag = FALSE;
                }
         if (flag = FALSE)
            {
               printf("\nYou have entered an invalid input. Please enter a value between 1-6."); //Error message to user.
            }
         else if  (flag = TRUE)
            {
               currency = atoi(curr);
            }
         if(flag = TRUE)
                 break;
       }
   return(currency); //returns value
}  
PaulCaswellCommented:
Your second problem is that you are trying to do too much in 'userinfo'. I'd suggest you use two different functions, one to input the currency typa and one for the value. You should then be able to do something like:

...
 int currencyType = askUserForCurrencyType ();
 float currencyAmount = askUserForCurrencyValue ();
...

Paul
byrianrAuthor Commented:
Paul,

I am such a beginner at this, so what you are saying is not an easy task for me. I am trying to make this as simple as possible, can you assist me further? Do you have a chat program we could chat one on one?

Thanks,
Byrian
PaulCaswellCommented:
Hi byrianr,

Chatting here is best. EE doesnt like it if we take the question away. They dont get the benefit of it in their database then.

Just make the changes slowly and carefully and take some time to think about them and the results. Remember that the computer is doing exactly what you are asking it to do, no more nor less. It is NOT conspiring against you. :-)

Try to make the problem as small as possible. Making a new function is easy. Just prototype it at the top of the code:

int askUserForCurrency ( void );

then write it at the bottom:

int askUserForCurrency ( void )
{
      int userenter;
     /* Display the five possible curreny conversions */
     printf("\nCurrency Conversion \n\n");
     printf("1) Japanese Yen\n");
     printf("2) Canadian Dollar\n");
     printf("3) European Euro\n");
     printf("4) Brazilian Real\n");
     printf("5) Australian Dollar\n");
     printf("6) Exit Program\n\n");
     printf("Please select a currency conversion to display: ");
     scanf("%d",&userenter);
     return (userenter);
}

I'll leave the other one to you.

Paul
byrianrAuthor Commented:
Thanks, Paul. This is much easier said than done. I will work on it and see how I do. Thanks.
byrianrAuthor Commented:
I am working it. Here is the program so far.

//Headers for operations, etc.
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

int userinfo(int); //Calling error function, storing in memory

int askUserForCurrency ( void );

float currencyAmount(void);

#define TRUE 1 //Flag for later use
#define FALSE 0 //Flag for later use

char curr [50]="";

//Main Function for program
int main()
{

     /* Variables */
     float yen,canada,euro,real,aussie; /* 5 conversion variables - each variable is the US dollar equivalent to 1 foreign currency */
     int currency; /* the currency/money the user defined */
     float convert; //number of foreign currency to convert into USD

   while (currency != 6) //While loop to execute while user information is between 1 and 6.
   {
      /*Call Input Validation function*/
      currency = userinfo(currency);
     
      convert = (float)atof(curr);
     
     
     switch (currency) //CASE statement for the different current selections
     {
          case 1:      /* Japanese Yen Selection */
               yen = 0.00848104;
               printf("\n");
               printf("You have %f US Dollars.",yen*convert);
               break;

          case 2:      /* Canadian Dollar Selection */
               canada = 0.863782;
               printf("\n");
               printf("You have %f US Dollars.",canada*convert);
               break;

          case 3:      /* European Euro Selection */
               euro = 1.1882;
               printf("\n");
               printf("You have %f US Dollars.",euro*convert);
               break;

          case 4:      /* Brazilian Real Selection */
               real = 0.47259;
               printf("\n");
               printf("You have %f US Dollars.",real*convert);
               break;

          case 5:      /* Australian Dollar Selection */
               aussie = 0.738002;
               printf("\n");
               printf("You have %f US Dollars.",aussie*convert);
               break;
               
          case 6:      /*User exits program*/
                printf("\n");
                printf("Thank You. Have a nice day. Now exiting the program.\n\n");
                break;

          default:     /* Default message in the event user does not enter 1 through 5 selection */
               printf("\n");
               printf("Please rerun the program and enter a number between 1 and 5.");
               printf("\nWould you want to continue [Y for Yes, anything else for No]?");
               fflush(stdin);
               currency = getch();

               if(currency == 'y' || currency == 'Y') /*Error checking to continue or end the program */
               {
                    main();
                    return 0;
               }
               else
   return 0;

         
         }  //CASE statement end
   } //WHILE loop end

     /* Display the results on the screen */
     fflush(stdout);
     printf("\nPress any key to end.");
     getche();
 
}  //MAIN end

//Error checking function
int userinfo()
{
      //Function local variables.
      int i = 0; //counter for loop
      int flag = TRUE; //Flag
      int currency = 0;
      int ucurrency;
     
       
     while(1) //While loop to check user information/input is valid.
      {
        for( i=0; curr[i] != '\0'; i++)
                {
                   if(!isdigit(curr[i]) || curr[i] != '.')
                     flag = FALSE;
                }
         if (flag = FALSE)
            {
               printf("\nYou have entered an invalid input. Please enter a value between 1-6."); //Error message to user.
            }
         else if  (flag = TRUE)
            {
               currency = atoi(curr);
            }
         if(flag = TRUE)
                 break;
       }
   return(currency); //returns value
}  

int askUserForCurrency ( void )
{
      int userenter;
     /* Display the five possible curreny conversions */
     printf("\nCurrency Conversion \n\n");
     printf("1) Japanese Yen\n");
     printf("2) Canadian Dollar\n");
     printf("3) European Euro\n");
     printf("4) Brazilian Real\n");
     printf("5) Australian Dollar\n");
     printf("6) Exit Program\n\n");
     printf("Please select a currency conversion to display: ");
     scanf("%d",&userenter);
     return (userenter);
}

float askUserForCurrencyValue ()
{



      printf("\nPlease enter how much foreign currency you would like to convert to US DOllars: ");
      scanf("%s",curr);
     
     
     
}
PaulCaswellCommented:
>>This is much easier said than done.
Quite right! :-) And like almost everything in life, the more you practice the better you will get.

It WILL get easier! Just keep trying.

One good practice is to take a copy of what you've done often. Then, if you make a mistake, you can always go back to an earlier one.

Paul
byrianrAuthor Commented:
OK. So I have this, but have errors at the beginning.

//Headers for operations, etc.
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

int userinfo(int); //Calling error function, storing in memory

int askUserForCurrency ( void );

float currencyAmount(void);

#define TRUE 1 //Flag for later use
#define FALSE 0 //Flag for later use

char curr [50]="";

//Main Function for program
int main()
{

     /* Variables */
     float yen,canada,euro,real,aussie; /* 5 conversion variables - each variable is the US dollar equivalent to 1 foreign currency */
     int currency; /* the currency/money the user defined */
     float convert; //number of foreign currency to convert into USD
     
     int currencyType = askUserForCurrencyType (curr);
     float currencyAmount = askUserForCurrencyValue (curr);


   while (currency != 6) //While loop to execute while user information is between 1 and 6.
   {
      /*Call Input Validation function*/
      currency = userinfo(currency);
     
      convert = (float)atof(curr);
     
     
     switch (currency) //CASE statement for the different current selections
     {
          case 1:      /* Japanese Yen Selection */
               yen = 0.00848104;
               printf("\n");
               printf("You have %f US Dollars.",yen*convert);
               break;

          case 2:      /* Canadian Dollar Selection */
               canada = 0.863782;
               printf("\n");
               printf("You have %f US Dollars.",canada*convert);
               break;

          case 3:      /* European Euro Selection */
               euro = 1.1882;
               printf("\n");
               printf("You have %f US Dollars.",euro*convert);
               break;

          case 4:      /* Brazilian Real Selection */
               real = 0.47259;
               printf("\n");
               printf("You have %f US Dollars.",real*convert);
               break;

          case 5:      /* Australian Dollar Selection */
               aussie = 0.738002;
               printf("\n");
               printf("You have %f US Dollars.",aussie*convert);
               break;
               
          case 6:      /*User exits program*/
                printf("\n");
                printf("Thank You. Have a nice day. Now exiting the program.\n\n");
                break;

          default:     /* Default message in the event user does not enter 1 through 5 selection */
               printf("\n");
               printf("Please rerun the program and enter a number between 1 and 5.");
               printf("\nWould you want to continue [Y for Yes, anything else for No]?");
               fflush(stdin);
               currency = getch();

               if(currency == 'y' || currency == 'Y') /*Error checking to continue or end the program */
               {
                    main();
                    return 0;
               }
               else
   return 0;

         
         }  //CASE statement end
   } //WHILE loop end

     /* Display the results on the screen */
     fflush(stdout);
     printf("\nPress any key to end.");
     getche();
 
}  //MAIN end

//Error checking function
int userinfo()
{
      //Function local variables.
      int i = 0; //counter for loop
      int flag = TRUE; //Flag
      int currency = 0;
      int ucurrency;
     
       
     while(1) //While loop to check user information/input is valid.
      {
        for( i=0; curr[i] != '\0'; i++)
                {
                   if(!isdigit(curr[i]) || curr[i] != '.')
                     flag = FALSE;
                }
         if (flag = FALSE)
            {
               printf("\nYou have entered an invalid input. Please enter a value between 1-6."); //Error message to user.
            }
         else if  (flag = TRUE)
            {
               currency = atoi(curr);
            }
         if(flag = TRUE)
                 break;
       }
   return(currency); //returns value
}  

int askUserForCurrency ( void )
{
      int userenter;
     /* Display the five possible curreny conversions */
     printf("\nCurrency Conversion \n\n");
     printf("1) Japanese Yen\n");
     printf("2) Canadian Dollar\n");
     printf("3) European Euro\n");
     printf("4) Brazilian Real\n");
     printf("5) Australian Dollar\n");
     printf("6) Exit Program\n\n");
     printf("Please select a currency conversion to display: ");
     scanf("%d",&userenter);
     return (userenter);
}

float askUserForCurrencyValue ()
{
      
      printf("\nPlease enter how much foreign currency you would like to convert to US DOllars: ");
      scanf("%s",curr);
      return (curr);
   
}
PaulCaswellCommented:
Hi byrianr,

Your prototypes dont match the functions:

int askUserForCurrency ( void );

float currencyAmount(void);

should be:

int askUserForCurrency ( void );
float askUserForCurrencyValue (void);

Here:

     int currencyType = askUserForCurrencyType (curr);
     float currencyAmount = askUserForCurrencyValue (curr);

You are trying to pass 'curr' to the functions and the names are wrong. Use:

     int currencyType = askUserForCurrency ();
     float currencyAmount = askUserForCurrencyValue ();

Here:

               yen = 0.00848104;

My compiler complains about this (and all the others) with a warning. If yours does too, use:

     double yen,canada,euro,real,aussie; /* 5 conversion variables - each variable is the US dollar equivalent to 1 foreign currency */

This is because 0.00848104 is a 'double' value by default. You must either use 'float' or 'double' throughout.

My compiler complains about a few other things too but they may be just me. There are some problems in 'askUserForCurrencyValue' but you need to think about them first and try to fix them yourself before I help there. You'll learn more that way.

Keep going! You're getting there :-)

Paul
byrianrAuthor Commented:
Now I have an infinite loop... man, this is killing me....

//Headers for operations, etc.
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

int userinfo(int); //Calling error function, storing in memory

int askUserForCurrency ( void );
float askUserForCurrencyValue (void);

#define TRUE 1 //Flag for later use
#define FALSE 0 //Flag for later use

char curr [50]="";

//Main Function for program
int main()
{

     /* Variables */
     float yen,canada,euro,real,aussie; /* 5 conversion variables - each variable is the US dollar equivalent to 1 foreign currency */
     int currency; /* the currency/money the user defined */
     float convert; //number of foreign currency to convert into USD
     
     int currencyType = askUserForCurrency ();
     float currencyAmount = askUserForCurrencyValue ();

   while (currency != 6) //While loop to execute while user information is between 1 and 6.
   {
      /*Call Input Validation function*/
      currency = userinfo(currency);
     
      convert = (float)atof(curr);
     
     
     switch (currency) //CASE statement for the different current selections
     {
          case 1:      /* Japanese Yen Selection */
               yen = 0.00848104;
               printf("\n");
               printf("You have %f US Dollars.",yen*convert);
               break;

          case 2:      /* Canadian Dollar Selection */
               canada = 0.863782;
               printf("\n");
               printf("You have %f US Dollars.",canada*convert);
               break;

          case 3:      /* European Euro Selection */
               euro = 1.1882;
               printf("\n");
               printf("You have %f US Dollars.",euro*convert);
               break;

          case 4:      /* Brazilian Real Selection */
               real = 0.47259;
               printf("\n");
               printf("You have %f US Dollars.",real*convert);
               break;

          case 5:      /* Australian Dollar Selection */
               aussie = 0.738002;
               printf("\n");
               printf("You have %f US Dollars.",aussie*convert);
               break;
               
          case 6:      /*User exits program*/
                printf("\n");
                printf("Thank You. Have a nice day. Now exiting the program.\n\n");
                break;

          default:     /* Default message in the event user does not enter 1 through 5 selection */
               printf("\n");
               printf("Please rerun the program and enter a number between 1 and 5.");
               printf("\nWould you want to continue [Y for Yes, anything else for No]?");
               fflush(stdin);
               currency = getch();

               if(currency == 'y' || currency == 'Y') /*Error checking to continue or end the program */
               {
                    main();
                    return 0;
               }
               else
   return 0;

         
         }  //CASE statement end
   } //WHILE loop end

     /* Display the results on the screen */
     fflush(stdout);
     printf("\nPress any key to end.");
     getche();
 
}  //MAIN end

//Error checking function
int userinfo()
{
      //Function local variables.
      int i = 0; //counter for loop
      int flag = TRUE; //Flag
      int currency = 0;
      int ucurrency;
     
       
     while(1) //While loop to check user information/input is valid.
      {
        for( i=0; curr[i] != '\0'; i++)
                {
                   if(!isdigit(curr[i]) || curr[i] != '.')
                     flag = FALSE;
                }
         if (flag = FALSE)
            {
               printf("\nYou have entered an invalid input. Please enter a value between 1-6."); //Error message to user.
            }
         else if  (flag = TRUE)
            {
               currency = atoi(curr);
            }
         if(flag = TRUE)
                 break;
       }
   return(currency); //returns value
}  

int askUserForCurrency ( void )
{
      int userenter;
     /* Display the five possible curreny conversions */
     printf("\nCurrency Conversion \n\n");
     printf("1) Japanese Yen\n");
     printf("2) Canadian Dollar\n");
     printf("3) European Euro\n");
     printf("4) Brazilian Real\n");
     printf("5) Australian Dollar\n");
     printf("6) Exit Program\n\n");
     printf("Please select a currency conversion to display: ");
     scanf("%d",&userenter);
     return (userenter);
}

float askUserForCurrencyValue ()
{
      
      printf("\nPlease enter how much foreign currency you would like to convert to US DOllars: ");
      scanf("%s",curr);
      return (curr);
   
}
byrianrAuthor Commented:
OK, I have added more. Can you help? The math does not calculate right and the loop stops at 6?

//Headers for operations, etc.
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

int userinfo(int); //Calling error function, storing in memory

int askUserForCurrency ( void );
float askUserForCurrencyValue (void);

#define TRUE 1 //Flag for later use
#define FALSE 0 //Flag for later use

char curr [50]="";

//Main Function for program
int main()
{

     /* Variables */
     float yen,canada,euro,real,aussie; /* 5 conversion variables - each variable is the US dollar equivalent to 1 foreign currency */
     int currency; /* the currency/money the user defined */
     float convert; //number of foreign currency to convert into USD
     
   while (currency != 6) //While loop to execute while user information is between 1 and 6.
   {    
     
     int currencyType = askUserForCurrency ();
     float currencyAmount = askUserForCurrencyValue ();

  /* while (currency != 6) //While loop to execute while user information is between 1 and 6.
   {*/
      /*Call Input Validation function*/
      currency = userinfo(currency);
      convert = (float)atof(curr);
           
     switch (currency) //CASE statement for the different current selections
     {
          case 1:      /* Japanese Yen Selection */
               yen = 0.00848104;
               printf("\n");
               printf("You have %f US Dollars.",yen*convert);
               break;

          case 2:      /* Canadian Dollar Selection */
               canada = 0.863782;
               printf("\n");
               printf("You have %f US Dollars.",canada*convert);
               break;

          case 3:      /* European Euro Selection */
               euro = 1.1882;
               printf("\n");
               printf("You have %f US Dollars.",euro*convert);
               break;

          case 4:      /* Brazilian Real Selection */
               real = 0.47259;
               printf("\n");
               printf("You have %f US Dollars.",real*convert);
               break;

          case 5:      /* Australian Dollar Selection */
               aussie = 0.738002;
               printf("\n");
               printf("You have %f US Dollars.",aussie*convert);
               break;
               
          case 6:      /*User exits program*/
                printf("\n");
                printf("Thank You. Have a nice day. Now exiting the program.\n\n");
                break;

          default:     /* Default message in the event user does not enter 1 through 5 selection */
               printf("\n");
               printf("Please rerun the program and enter a number between 1 and 5.");
               printf("\nWould you want to continue [Y for Yes, anything else for No]?");
               fflush(stdin);
               currency = getch();

               if(currency == 'y' || currency == 'Y') /*Error checking to continue or end the program */
               {
                    main();
                    return 0;
               }
               else
   return 0;

         
         }  //CASE statement end
   } //WHILE loop end

     /* Display the results on the screen */
     fflush(stdout);
     printf("\nPress any key to end.");
     getche();
 
}  //MAIN end

//Error checking function
int userinfo()
{
      //Function local variables.
      int i = 0; //counter for loop
      int flag = TRUE; //Flag
      int currency = 0;
      int ucurrency;
     
       
     while(1) //While loop to check user information/input is valid.
      {
        for( i=0; curr[i] != '\0'; i++)
                {
                   if(!isdigit(curr[i]) || curr[i] != '.')
                     flag = FALSE;
                }
         if (flag = FALSE)
            {
               printf("\nYou have entered an invalid input. Please enter a value between 1-6."); //Error message to user.
            }
         else if  (flag = TRUE)
            {
               currency = atoi(curr);
            }
         if(flag = TRUE)
                 break;
       }
   return(currency); //returns value
}  

int askUserForCurrency ( void )
{
      int userenter;
     /* Display the five possible curreny conversions */
     printf("\nCurrency Conversion \n\n");
     printf("1) Japanese Yen\n");
     printf("2) Canadian Dollar\n");
     printf("3) European Euro\n");
     printf("4) Brazilian Real\n");
     printf("5) Australian Dollar\n");
     printf("6) Exit Program\n\n");
     printf("Please select a currency conversion to display: ");
     scanf("%d",&userenter);
     return (userenter);
}

float askUserForCurrencyValue ()
{
      
      printf("\nPlease enter how much foreign currency you would like to convert to US DOllars: ");
      scanf("%s",curr);
      return (curr);
   
}

PaulCaswellCommented:
Hi byrianr,

OK! Getting there! At least it compiles now eh? Do you get any warnings? If so, fix them first.

I notice you still have:

float yen,...

Remember to change that to 'double'.

The biggest problem is here:

float askUserForCurrencyValue ()
{
     
     printf("\nPlease enter how much foreign currency you would like to convert to US DOllars: ");
     scanf("%s",curr);
//<<< You've lost the code that checked for the validity of the string they entered. Its still in 'userinfo' (which you wont need any more soon).
     return (curr); //<<<< Return of 'curr' which is a 'char *'! You need to convert it to a float! You already had that code elsewhere, use 'atof'.
   
}

Then there's:

      currency = userinfo(currency);
      convert = (float)atof(curr);
           
     switch (currency) //CASE statement for the different current selections

You've just calculated the currency number and amount with:

   int currencyType = askUserForCurrency ();
     float currencyAmount = askUserForCurrencyValue ();

so the 'currency' and 'convert' variables are no longer valid or necessary.

If you dont understand what's happening here have a think about it and post if you are sure you cant work it out. I am deliberately not writing your code for you, not to be a pain but because you must understand it so please make sure you read it carefully and work out what it is doing, how and why.

You cannot rush this! You have a lot to learn. The amazing thing about learning to program is that once you understand even a little about what is going on it all gets very easy so long as you are prepared to think and concentrate.

Try acting like a computer and read each line of your code slowly and very carefully, referring to your textbooks whenever you see something you are unsure of. Remember that you cannot recall anything about what has happened before except what is left in variables and that you know absoulutely nothing about what will come after. The variables control the code and the code changes the variables. It really is that simple!

Paul
byrianrAuthor Commented:
Understood, thank you. Learning is always best. I have redone my program. I now have it working, but output is wild. Can you help?

// Headers for program/functions.
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

#define TRUE 1
#define FALSE 0

int userenter(int);
float currency();

int main()
{      

/*Variable declarations*/
   float yen,canada,euro,real,aussie;

   int iuserenter;
   
   float userout = 0.0;            //Output from the dollar input function
   float userreturn = 0.0;            //Ouput from dollar input function to be converted

   
   while (iuserenter != 6)
   {
      iuserenter = userenter(iuserenter);
      
      switch (iuserenter)
      {
         case 1:
            yen = 0.00848104;
            userreturn = currency();
            printf("\nYou have %f US Dollars", userreturn*yen);
            break;
         
         case 2:
                canada = 0.863782;
                userreturn = currency();
                printf("\nYou have %f US Dollars", userreturn*canada);
                break;
                
         case 3:
            euro = 1.1882;
                userreturn = currency();
                printf("\nYou have %f US Dollars", userreturn*euro);
                break;
                
         case 4:
                real = 0.47259;
                userreturn = currency();
                printf("\nYou have %f US Dollars", userreturn*real);
                break;
                
         case 5:
                aussie = 0.738002;
                userreturn = currency();
                printf("\nYou have %f US Dollars", userreturn*aussie);
                break;
                
         case 6:
            printf("\nExiting program, have a nice day.");
            break;
           
         default:
          printf("\nUser input error.");
          continue;
      }
      /*End switch*/
   }
   /*End while statement*/
   return(0);
   
   fflush(stdin);
   printf("\nPress any key to continue.");
   getche();  
}

int userenter()
{
      char curr [50]= "";
      int i = 0;
      int flag = TRUE;
      int iuserenter = 0;
     
      /*Print menu for selecting currency*/
      printf("\nCurrency Conversion \n\n");
      printf("1) Japanese Yen\n");
      printf("2) Canadian Dollar\n");
      printf("3) European Euro\n");
      printf("4) Brazilian Real\n");
      printf("5) Australian Dollar\n");
      printf("6) Exit Program\n\n");
      printf("\n\nWhich currency would you like to convert? Enter above item: ");
      gets(curr);
     
      while(1)
      {
         for( i=0; curr[i] != '\0'; i++)
                {
                   if(!isdigit(curr[i]) || curr[i] == '.')
                       flag = FALSE;
                }
         if (flag = FALSE)
            {
               printf("\nUser input error.");
            }
         else if  (flag = TRUE)
            {
               iuserenter = atoi(curr);
            }
         if(flag = TRUE)
            break;
       }
   return(iuserenter);
}  
         
float currency()
{
      char str[256]="";
      char cont;
      int decicount, counter, numeric;
      unsigned int i;
      float userout = 0.0;

      while (1)
      {
            numeric = TRUE;
            printf("\nEnter the amount to convert: ");
            scanf("%s", str);
            fflush(stdin);
            counter=0;
            decicount=0;

            if(!isdigit(str[0])&&str[0] != '-' &&str[0]!= '.')
                  numeric = FALSE;
            if( str[0] == '.')
                  decicount++;

            for(i=1; i<strlen(str); i++)
            {
                  if(!isdigit(str[i]) && str[i] != '.')
                        numeric = FALSE;
                  if( str[i] == '.')
                        decicount++;
            }
            if( decicount > 1)
                  numeric = FALSE;
            
            if(numeric == TRUE)
                  {
                        userout = atof(str);
                        
                        if( userout < 0.0)
                              printf("\n Input must be a positive dollar amount.");
                        
                        if( userout >= 0.0)
                              printf("\n The dollar amount you have entered for conversion is $%f", userout);
                              

                  }
            else
                  {
                        printf("\n\n%s: is not a valid dollar amount.\n", str);
                  }
            printf("\n\n\n\n\nDo you wish to re-input(Y/N)?");
            printf("\nPress Y to reinput.");
            Printf("\nPress N to continue with the conversion: ");
            scanf("%c", &cont);
            if(toupper(cont) == 'N')
            break;
      }

      return (userout);
}

            
PaulCaswellCommented:
Hi byrianr,

OK! Good! Lets have a look!

Here's the only problem I can see.

    int iuserenter;
   
   float userout = 0.0;          //Output from the dollar input function
   float userreturn = 0.0;          //Ouput from dollar input function to be converted

   
   while (iuserenter != 6)

Compiler reports that iuserenter is being used before being initialised. It will be a random number first time around so if it happens to be 6 the loop will never be entered.

There's a few other odd things happening, like 'User input error.' every time around the loop but it all looks fundamentally OK!

What do you think is wrong?

I didnt check the numbers themselves but the code looks like its ok.

Paul
byrianrAuthor Commented:
Ok. I have it working now. Check this out...

// Headers for program/functions.
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

#define TRUE 1
#define FALSE 0

int userenter(int);
float currency();

int main()
{      

/*Variable declarations*/
   float yen,canada,euro,real,aussie;
   int iuserenter;
   float userout;            //Output from the dollar input function
   float userreturn;            //Ouput from dollar input function to be converted

   
   while (iuserenter != 6)
   {
      iuserenter = userenter(iuserenter);
      
      switch (iuserenter)
      {
         case 1:
            yen = 0.00848104;
            userreturn = currency();
            printf("\nYou have %f US Dollars", userreturn*yen);
            break;
         
         case 2:
                canada = 0.863782;
                userreturn = currency();
                printf("\nYou have %f US Dollars", userreturn*canada);
                break;
                
         case 3:
            euro = 1.1882;
                userreturn = currency();
                printf("\nYou have %f US Dollars", userreturn*euro);
                break;
                
         case 4:
                real = 0.47259;
                userreturn = currency();
                printf("\nYou have %f US Dollars", userreturn*real);
                break;
                
         case 5:
                aussie = 0.738002;
                userreturn = currency();
                printf("\nYou have %f US Dollars", userreturn*aussie);
                break;
                
         case 6:
            printf("\nExiting program, have a nice day.");
            break;
           
         default:
          printf("\nUser input error.");
          continue;
      }
      /*End switch*/
   }
   /*End while statement*/
   return(0);
   
   fflush(stdin);
   printf("\nPress any key to continue.");
   getche();  
}

int userenter()
{
      char curr [50]= "";
      int i = 0;
      int flag = TRUE;
      int iuserenter = 0;
     
      /*Print menu for selecting currency*/
      printf("\n\nCurrency Conversion \n\n");
      printf("1) Japanese Yen\n");
      printf("2) Canadian Dollar\n");
      printf("3) European Euro\n");
      printf("4) Brazilian Real\n");
      printf("5) Australian Dollar\n");
      printf("6) Exit Program\n\n");
      printf("\n\nWhich currency would you like to convert? Enter above item: ");
      gets(curr);
     
      while(1)
      {
         for( i=0; curr[i] != '\0'; i++)
                {
                   if(!isdigit(curr[i]) || curr[i] == '.')
                       flag = FALSE;
                }
         if (flag = FALSE)
            {
               printf("\nUser input error.");
            }
         else if  (flag = TRUE)
            {
               iuserenter = atoi(curr);
            }
         if(flag = TRUE)
            break;
       }
   return(iuserenter);
}  
         
float currency()
{
      char str[256]="";
      char cont;
      int decicount, counter, numeric;
      unsigned int i;
      float userout = 0.0;

      while (1)
      {
            numeric = TRUE;
            printf("\nEnter the amount to convert: ");
            scanf("%s", str);
            fflush(stdin);
            counter=0;
            decicount=0;

            if(!isdigit(str[0])&&str[0] != '-' &&str[0]!= '.')
                  numeric = FALSE;
            if( str[0] == '.')
                  decicount++;

            for(i=1; i<strlen(str); i++)
            {
                  if(!isdigit(str[i]) && str[i] != '.')
                        numeric = FALSE;
                  if( str[i] == '.')
                        decicount++;
            }
            if( decicount > 1)
                  numeric = FALSE;
            
            if(numeric == TRUE)
                  {
                        userout = atof(str);
                        
                        if( userout < 0.0)
                              printf("\n Input must be a positive dollar amount.");
                        
                        if( userout >= 0.0)
                              return(userout);
                              

                  }
            else
                  {
                        printf("\n\n%s: is not a valid dollar amount.\n", str);
                  }
            break;
      }

      return (userout);
}

            
PaulCaswellCommented:
Hi byrianr,

Brilliant! Doesnt it feel good when it works and its ALL your own code!

I get the following warnings:

c:\dev\atest\atest.cpp(34) : warning C4101: 'userout' : unreferenced local variable
c:\dev\atest\atest.cpp(134) : warning C4101: 'cont' : unreferenced local variable
c:\dev\atest\atest.cpp(38) : warning C4700: uninitialized local variable 'iuserenter' used

But dont worry if your compiler doesnt complain about them.

Apart from that you've got it cracked. It works fine here.

Well done!

Paul
byrianrAuthor Commented:
Thank you for your assistance.
PaulCaswellCommented:
No problems! See you again sometime. Good luck in your course.

Paul

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.