Currency Convertor

This is a discussion on Currency Convertor within the C Programming forums, part of the General Programming Boards category; Hi, Thanks for all the help so far. Any more sugestions would be greatly appreciated. Thanking you. Code: #include <stdio.h> ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    44

    Currency Convertor

    Hi,

    Thanks for all the help so far. Any more sugestions would be greatly appreciated.

    Thanking you.

    Code:
    #include <stdio.h>
    
    /*	  Define Constants for Currency Conversion Rates	*/
    
    #define POUND 0.70
    #define KRONE 7.43
    #define YEN 127
    
    int main()
    {
    
       /* Declare all varabials	*/
    
    	int menu_choice, error;
    	float local_amount, remote_amount;
    	float USD;
    
    	do
    	{
    
    		/*		Display Menu System		*/
    		/*		5 Options		*/
    
    		printf("1. Britain\n");
       	printf("2. Denmark\n");
       	printf("3. Japan\n");
       	printf("4. USA\n");
       	printf("5. Exit Program\n\n");
    
       	/* Ask user for there choice														  */
       	/* Asign the choice to varabial menu_choice, which is of int data type */
    		/* Keep Asking The User for input until they enter correct data		  */
    
    
    
          /*		This do loop will itterate until the user enteres the desired menu option to exit	*/
    
    
    		do
    		{
       		printf("Please Enter Your Country Selection From The Menu: \t");
       		error = scanf("%d",&menu_choice);
          	fflush(stdin);
    		}	while ( error == 0 || menu_choice < 1 || menu_choice > 5 );
    
    
    
       	if (menu_choice == 1)
          {
    
             /* Ask user for there choice */
       		/* Asign the choice to varabial menu_choice, which is of int data type */
          	/* Keep Asking The User for input until they enter correct data		  */
    
    			printf("\n\nDo you wish to convert from:\n\n");
    			printf("1. Euro to Pound(Sterling)\n");
             printf("2. Pound(Sterling) to Euro\n\n");
    
             do
             {
             	printf("Please Enter Your Choice From The Menu: \t");
             	error = scanf("%d",&menu_choice);
                fflush(stdin);
             }	while ( error == 0 || menu_choice < 1 || menu_choice > 2 );
    
          	if (menu_choice == 1)
             {
             	printf("\n\nEuro to Pound(Sterling):\n\n");
                printf("Please Enter Euro Amount to be Converted: \t");
                scanf("%f",&local_amount);
    
             	remote_amount = local_amount * POUND;
    
                printf("\n%.2f Euro is equal to %.2f Pound\n\n",local_amount, remote_amount);
                menu_choice = 0;
             }
    
             if (menu_choice == 2)
             {
    
             	printf("\n\nPound(Sterling) to Euro:\n\n");
    
             	printf("Please Enter Pound(Sterling) Amount to be Converted: \t");
                scanf("%f",&local_amount);
    
                remote_amount = local_amount / POUND;
    
                printf("\n%.2f Pound is equal to %.2f Euro\n\n",local_amount, remote_amount);
    
                menu_choice = 0;
             }
          }
    
          if (menu_choice == 2)
          {
          	printf("\n\nDo you wish to convert from:\n\n");
             printf("1. Euro to Krone\n");
             printf("2. Krone to Euro\n\n");
    
             do
             {
             	printf("Please Enter Your Choice From The Menu: \t");
                error = scanf("%d",&menu_choice);
                fflush(stdin);
             }	while ( error == 0 || menu_choice < 1 || menu_choice > 2 );
    
             if (menu_choice == 1)
             {
             	printf("\n\nEuro to Krone:\n\n");
                printf("Please Enter Euro Amount to be Converted: \t");
                scanf("%f",&local_amount);
    
                remote_amount = local_amount * KRONE;
    
                printf("\n%.2f Euro is equal to %.2f Krone\n\n",local_amount, remote_amount);
    
                menu_choice = 0;
    
             }
    
             if (menu_choice == 2)
          	{
    
          		printf("\n\nKrone to Euro:\n\n");
    
                printf("Please Enter Krone Amount to be Converted: \t");
                scanf("%f",&local_amount);
    
                remote_amount = local_amount / KRONE;
    
             	printf("\n%.2f Krone is equal to %.2f Euro\n\n",local_amount, remote_amount);
    
                menu_choice = 0;
    
             }
          }
    
       	if (menu_choice == 3)
          {
          	printf("\n\nDo you wish to convert from:\n\n");
             printf("1. Euro to Yen\n");
             printf("2. Yen to Euro\n\n");
    
             do
             {
             	printf("Please Enter Your Choice From The Menu: \t");
                error = scanf("%d",&menu_choice);
                fflush(stdin);
             }	while ( error == 0 || menu_choice < 1 || menu_choice > 2 );
    
             if (menu_choice == 1)
             {
             	printf("\n\nEuro to Yen:\n\n");
    
                printf("Please Enter Euro Amount to be Converted: \t");
                scanf("%f",&local_amount);
    
                remote_amount = local_amount * YEN;
    
                printf("\n%.2f Euro is equal to %.2f Yen\n\n",local_amount, remote_amount);
    
                menu_choice = 0;
    
             }
    
             if (menu_choice == 2)
             {
    
             	printf("\n\nYen to Euro:\n\n");
    
                printf("Please Enter Yen Amount to be Converted: \t");
                scanf("%f",&local_amount);
    
                remote_amount = local_amount / YEN;
    
                printf("\n%.2f Yen is equal to %.2f Euro\n\n",local_amount, remote_amount);
    
                menu_choice = 0;
    
             }
          }
    
       	if (menu_choice == 4)
          {
          	printf("\n\nDo you wish to convert from:\n\n");
             printf("1. Euro to US Dollars\n");
             printf("2. US Dollars to Euro\n\n");
    
             do
             {
             	printf("Please Enter Your Choice From The Menu: \t");
                error = scanf("%d",&menu_choice);
                fflush(stdin);
             }	while ( error == 0 || menu_choice < 1 || menu_choice > 2 );
    
             printf("\nExample:\t\t1 EURO = POUND 0.70\n\t\t\t1 EURO = KRONE 7.43\n\n");
    
             do
             {
             	printf("Please Enter Current US Dollar Exchange Rate: \t");
                error = scanf("%f",&USD);
                fflush(stdin);
             } 	while ( error == 0 || USD <= 0);
    
             if (menu_choice == 1)
             {
             	printf("\n\nEuro to US Dollars:\n\n");
    
                printf("Please Enter Euro Amount to be Converted: \t");
                scanf("%f",&local_amount);
    
                remote_amount = local_amount * USD;
    
                printf("\n%.2f (EURO) is equal to %.2f (USD)\n\n",local_amount, remote_amount);
    
                menu_choice = 0;
    
             }
    
             if (menu_choice == 2)
             {
    
             printf("\n\nUS Dollars to Euro:\n\n");
    
             printf("Please Enter US Dollar Amount to be Converted: \t");
             scanf("%f",&local_amount);
    
             remote_amount = local_amount / USD;
    
             printf("\n%.2f (USD) is equal to %.2f (Euro)\n\n",local_amount, remote_amount);
    
             menu_choice = 0;
    
             }
          }
    
       	if (menu_choice == 5)
          {
          	printf("Thank You Come Again!");
          }
    
    	} while(menu_choice == 0);
    
       printf("\n\n\n\n\n\n$$$$ UP!");
       return 0;
    }
    Dangerous Dave

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >float USD;
    This can be confusing. The general convention is for macros and named constants to have names in all capital letters. It's a matter of style, but a lot of people are used to this one.

    >fflush(stdin);
    Big no-no, flushing an input stream is undefined behavior.

    >while ( error == 0 || menu_choice < 1 || menu_choice > 5 );
    scanf can return EOF too. Better something more like this:
    Code:
    int out_of_range ( int value, int low, int high )
    {
      return value < low || high > value;
    }
    ...
    while ( error != 1 || out_of_range ( menu_choice, 1, 5 ) );
    >error = scanf("%d",&menu_choice);
    This is the second time you use the same loop for input, consider wrapping the functionality in a function.

    >scanf("%f",&local_amount);
    Always, always check scanf's return value. No exceptions. If you must use it, at least use it in a relatively safe way.

    You might consider using switch cases instead of unrelated if statements for running a menu option. It tends to keep things a bit cleaner. That's my quick glance suggestion list, if I missed anything you have my apologies.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple currency conversion program gone south!!
    By lildroopie in forum C Programming
    Replies: 1
    Last Post: 02-01-2009, 12:45 PM
  2. Basic C Help - Currency Converter
    By liljp617 in forum C Programming
    Replies: 9
    Last Post: 09-07-2008, 11:12 PM
  3. Help with variables (newbee)
    By stugatza in forum C Programming
    Replies: 7
    Last Post: 08-18-2004, 11:40 AM
  4. simple currency conversion problem
    By sweetgem in forum C++ Programming
    Replies: 2
    Last Post: 08-01-2002, 01:41 PM
  5. Currency Conversion Program
    By kgraw21 in forum C Programming
    Replies: 5
    Last Post: 04-19-2002, 09:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21