Newbie Help: Currency Converter

This is a discussion on Newbie Help: Currency Converter within the C Programming forums, part of the General Programming Boards category; Here's what I'm supposed to do: " The user should be asked to enter their country selection from the menu. ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    7

    Newbie Help: Currency Converter

    Here's what I'm supposed to do:
    " The user should be asked to enter their country selection from the menu. If they pick either Britain, Denmark or Japan, they should be asked whether they wish to convert from Euro to either Sterling/Krone/Yen (select just one of these depending on their choice) or vice versa. Your program should then display this equivalent currency amount.

    If the user selects USA from the menu, they should be asked one extra question. Not only should they be asked whether they wish to covert from Euro to Dollar or vice-versa, they must also enter the current exchange rate between these currencies. Your program must then use this rate and calculate the converted currency value.

    Your program should continually run and re-display the main menu allowing the user to start over again. Only when the user enters option 5 (i.e. Exit program option), your program should terminate. The program must take account of input errors by the user and display appropriate error messages."
    But whenever I enter a character that is not a number, the program doesn't display an Error. How do I resolve this?
    Please take into consideration that I have only learnt the following at this point: if else, switch statements. while, do-while, for loops. I'm currently learning Arrays.
    Is there anyway to simplify the code? (as it looks too complicated at this stage)

    Code:
    #include <stdio.h>
    #include <conio.h>
    main()
    {
    	/* Define Variables */
    	int option, option2;
    	float eur_sterling, sterling_eur, sterling, sterling_euro;
    	float eur_krone, krone_eur, krone, krone_euro;
    	float eur_yen, yen_eur, yen, yen_euro;
    	float euro_dollar1, euro_dollar2, euro_dollar;
    	float dollar_eur1, dollar_eur2, dollar_euro;
    
    	/* Start loop */
    	do
    	{
    	/* Display menu asking user to select a country or quit */
    	printf("Euro Currency Converter\n");
    	printf("1. Britain\n");
    	printf("2. Denmark\n");
    	printf("3. Japan\n");
    	printf("4. USA\n");
    	printf("5. Exit Program\n");
    	printf("Please select an option: ");
    	scanf("%d", &option);
    
    		/* Initiate if statement for 1. Britain */
    		if (option == 1)
    		{
    			/* Ask user if converting Euro to Sterling or vice versa */
    			printf("If converting from Euro to Sterling, press 1\n");
    			printf("If converting from Sterling to Euro, press 2\n");
    			scanf("%d", &option2);
    
    			if (option2 == 1)
    			{
    				/* Ask user to enter amount for conversion */
    				printf("Please enter the amount you wish to convert (Euro to Sterling): ");
    				scanf("%f", &eur_sterling);
    
    				/* Calculate Exchange rate from Euro to Sterling */
    				sterling = eur_sterling * 0.70;
    				printf("%.2f Euro = %.2f Sterling\n\n", eur_sterling, sterling);
    			}
    
    			else if (option2 == 2)
    			{
    				/* Ask user to enter amount for conversion */
    				printf("Please enter the amount you wish to convert (Sterling to Euro): ");
    				scanf("%f", &sterling_eur);
    
    				/* Calculate Exchange rate from Sterling to Euro */
    				sterling_euro = sterling_eur * 1.42;
    				printf("%.2f Sterling = %.2f Euro\n\n", sterling_eur, sterling_euro);
    			}
    
    			else
    			{
    				printf("Error: Invalid Code\n\n");
    			}
    		}
    
    		/* Initiate else if statement for 2. Denmark */
    		else if (option == 2)
    		{
    			/* Ask user if converting Euro to Krone or vice versa */
    			printf("If converting from Euro to Krone, press 1\n");
    			printf("If converting from Krone to Euro, press 2\n");
    			scanf("%d", &option2);
    
    			if (option2 == 1)
    			{
    				/* Ask user to enter amount for conversion */
    				printf("Please enter the amount you wish to convert (Euro to Krone): ");
    				scanf("%f", &eur_krone);
    
    				/* Calculate Exchange rate from Euro to Krone */
    				krone = eur_krone * 7.45;
    				printf("%.2f Euro = %.2f Krone\n\n", eur_krone, krone);
    			}
    
    			else if (option2 == 2)
    			{
    				/* Ask user to enter amount for conversion */
    				printf("Please enter the amount you wish to convert (Krone to Euro): ");
    				scanf("%f", &krone_eur);
    
    				/* Calculate Exchange rate from Krone to Euro */
    				krone_euro = krone_eur * 0.13;
    				printf("%.2f Krone = %.2f Euro\n\n", krone_eur, krone_euro);
    			}
    
    			else
    			{
    				printf("Error: Invalid Code\n\n");
    			}
    		}
    
    		/* Initiate else if statement for 3. Japan */
    		else if (option == 3)
    		{
    			/* Ask user if converting Euro to Yen or vice versa */
    			printf("If converting from Euro to Yen, press 1\n");
    			printf("If converting from Yen to Euro, press 2\n");
    			scanf("%d", &option2);
    
    			if (option2 == 1)
    			{
    				/* Ask user to enter amount for conversion */
    				printf("Please enter the amount you wish to convert (Euro to Yen): ");
    				scanf("%f", &eur_yen);
    
    				/* Calculate Exchange rate from Euro to Yen */
    				yen = eur_yen * 138;
    				printf("%.2f Euro = %.2f Yen\n\n", eur_yen, yen);
    			}
    
    			else if (option2 == 2)
    			{
    				/* Ask user to enter amount for conversion */
    				printf("Please enter the amount you wish to convert (Yen to Euro): ");
    				scanf("%f", &yen_eur);
    
    				/* Calculate Exchange rate from Yen to Euro */
    				yen_euro = yen_eur * 0.01;
    				printf("%.2f Yen = %.2f Euro\n\n", yen_eur, yen_euro);
    			}
    
    			else
    			{
    				printf("Error: Invalid Code\n\n");
    			}
    		}
    
    		/* Initiate else if statement for 4. USA */
    		else if (option == 4)
    		{
    			/* Ask user if converting Euro to Dollar or vice versa */
    			printf("If converting from Euro to Dollar, press 1\n");
    			printf("If converting from Dollar to Euro, press 2\n");
    			scanf("%d", &option2);
    
    			if (option2 == 1)
    			{
    				/*Ask user to enter exchange rate from Euro to Dollar */
    				printf("Please enter the exchange rate (1 Euro = ? Dollar): ");
    				scanf("%f", &euro_dollar1);
    
    				/* Ask user to enter amount for conversion */
    				printf("Please enter the amount you wish to convert (Euro to Dollar): ");
    				scanf("%f", &euro_dollar2);
    
    				/* Calculate Exchange rate from Euro to Dollar */
    				euro_dollar = euro_dollar1 * euro_dollar2;
    				printf("%.2f Euro = %.2f Dollar\n\n", euro_dollar2, euro_dollar);
    			}
    
    			else if (option2 == 2)
    			{
    				/*Ask user to enter exchange rate from Dollar to Euro */
    				printf("Please enter the exchange rate (1 Dollar = ? Euro): ");
    				scanf("%f", &dollar_eur1);
    
    				/* Ask user to enter amount for conversion */
    				printf("Please enter the amount you wish to convert (Dollar to Euro): ");
    				scanf("%f", &dollar_eur2);
    
    				/* Calculate Exchange rate from Dollar to Euro */
    				dollar_euro = dollar_eur1 * dollar_eur2;
    				printf("%.2f Dollar = %.2f Euro\n\n", dollar_eur2, dollar_euro);
    			}
    
    			else
    			{
    				printf("Error: Invalid Code\n\n");
    			}
    		}
    
    		else if (option == 5)
    		{
    			printf("Exit Program\n");
    		}
    
    		/* If neither options 1-5 were selected print Error */
    		else
    		{
    		printf("Error: Invalid Code\n\n");
    		}
    	}
    
    	while (option != 5);
    	/* End loop */
    
    	getch();
    	return 0;
    
    } //End main
    Last edited by Ashfury; 11-05-2005 at 01:34 PM.

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    But whenever I enter a character that is not a letter, the program doesn't display an Error. How do I resolve this?
    Check the returnvalue of scanf. It will return 1 if it was a number and 0 if you enter something else.
    Kurt

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    7
    How do I check the returnvalue of scanf?

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    ret  = scanf("%d", & option );
    if ( ret == 1 && option > 0 && option < 6 ) ( 
        if ( option == 1) {
              ....
        }
          ....
    }
    else {
       puts( "enter a number between 1..5" );
    }
    Kurt

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    In that else, you might want to clear stdin from errors (in case the user entered a letter where a number was expected) with clearerr(), and remove the remaining characters from stdin with something like this:
    Code:
    int c;
    while((c = getchar()) != '\n' && c != EOF);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    7
    Quote Originally Posted by dwks
    In that else, you might want to clear stdin from errors (in case the user entered a letter where a number was expected) with clearerr(), and remove the remaining characters from stdin with something like this:
    Code:
    int c;
    while((c = getchar()) != '\n' && c != EOF);
    Thank you very much! But may I ask what EOF means as I have not seen that before.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    End Of File. If the input is from a file, it indicates the end of the input. (On DOS/Windows systems, you can generate this by pressing CTRL-Z.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Is there anyway to simplify the code?
    Using an array.

    Try an array that looks something like this:
    Code:
    char *names[] = {
        "Krone",
        "Euro",
        "Sterling"
    };
    and one like this
    Code:
    double rate[] = {
        1,  /* conversion rate from Krone to Krone */
        2.22,  /* conversion rate from Euro to Krone */
        /* etc */
    };
    To convert from Sterling to Euro, use the Sterling-to-Krone, and then Krone-to-Euro.

    Does that help?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    7
    Thanks! Good thing I'm doing Arrays at the moment. LOL

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    7
    Sorry for posting again, but can you tell me where to insert those codes? I tried what dwks directed but still doesn't work correctly. If possible, can you add the new code onto my existing code and paste itin your post?

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    I didn't think it would help. Maybe this will.

    Input the currencies from the user. Find their number using strcmp() and names[]. Then use the conversion factors in rate[] to convert them.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Currency Converter - stream pull
    By verbity in forum C# Programming
    Replies: 3
    Last Post: 10-18-2008, 05:29 PM
  2. converter currency in C
    By MyRedz in forum C Programming
    Replies: 7
    Last Post: 10-16-2008, 10:39 PM
  3. Basic C Help - Currency Converter
    By liljp617 in forum C Programming
    Replies: 9
    Last Post: 09-07-2008, 10:12 PM
  4. Currency Converter
    By mikmac in forum C Programming
    Replies: 3
    Last Post: 06-10-2003, 11:50 PM
  5. currency converter
    By Shalinee in forum C++ Programming
    Replies: 1
    Last Post: 03-19-2003, 02:27 AM

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