Thread: Currency Conversion problem

  1. #1
    Registered User
    Join Date
    Sep 2007

    Question Currency Conversion problem

    I am in school and I am having a problem with my code, when you enter a letter the program loops and the only way to correct it is to exit the program, also how would I correct this so that it would not run a negative number. Thanks in advance

    Currency Converter
    Week 5
    Richard Whitehead
    display Currency Conversion
    set a dollar value to variable
    input a dollar value, ask until quit command
    build 5 currency converts for 5 types
    get conversion tables from Internet for one US dollar
    display currency name and values when converted
    add warning about rate change
    return to system with "Q" or get return to begining
    preconditions: none
    main: display 5 currency values for a given input and exit
    postcondition:exit to o/s
    Ver. 1.0
    last changed Sept. 11 2007
    ver 1.2 
    12 September 2007
    added tabs in the title 
    tried different commands from different libraries...tried clrscr command did not like it so removed it 
    ver 1.3
    13 September 2007 
    added arrays to the program on 16 September 2007
    #include <conio.h>
    #include <stdio.h>
    #include <stdlib.h>
    /*Function prototyping*/
    float fnCalculateConversion(int, float); 
    float CURRENCIES[5] = {1.23884, 2.67000, 1.98395, 0.735994, 69.3389};
    /*Start of program*/
    int main()
       /*Defining the variables here*/
       int selectionNumbers;
      // int n;
       //int s;
       float AMOUNT = 0.0;
       /*Printing of program title*/
       printf("Currency Conversion Program\n");
       /*Printing of Menu*/
       printf("Press [1] for Australia Dollar\n\n");
       printf("Press [2] for Eastern Caribbean\n\n");
       printf("Press [3] for Belize Dollar\n\n");
       printf("Press [4] for Italy Euro\n\n");
       printf("Press [5] for Jamaican Dollar\n\n");
       printf("Press [q] to Quit Program\n\n");
       do {
           /*Prompting for user input - currencies*/
           printf("Please enter selection number of currencies to convert:");
           scanf("%d", &selectionNumbers);
           } while (selectionNumbers >5 || selectionNumbers <1);
       /*Prompting for user input - US dollars*/
       printf("Enter dollar amount to convert: ");
       scanf("%f", &AMOUNT);
        fnCalculateConversion(selectionNumbers ,AMOUNT);
        /*Result displayed here*/
        return 0;
     /*Function for calculation of conversation - user input*/
       float fnCalculateConversion(int selectionNumbers, float AMOUNT) 
         /*Naming of variable here*/
         float conversion;
         switch (selectionNumbers) 
           /*Conversion being done here*/
           case 1:
           conversion = CURRENCIES[0]*AMOUNT;
           printf("\nYour U.S. Dollars equal $%.2f Australian Dollar\n\n",conversion);
           case 2:
           conversion = CURRENCIES[1]*AMOUNT;
           printf("\nYour U.S. Dollars equal $%.2f Eastern Caribbean\n\n",conversion);
           case 3:
           conversion = CURRENCIES[2]*AMOUNT;
           printf("\nYour U.S. Dollars equal $%.2f Belize Dollar\n\n",conversion);
           case 4:
           conversion = CURRENCIES[3]*AMOUNT;
           printf("\nYour U.S. Dollars equal $%.2f Italy Euro\n\n",conversion);
           case 5:
           conversion = CURRENCIES[4]*AMOUNT;
           printf("\nYour U.S. Dollars equal $%.2f Jamaican Dollar\n\n",conversion);
         /*Result shown to user*/
         return conversion;
    /*The End*/

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    The way to start tackling the problem is to use the return result of scanf(). Have a quick look at the scanf() manual page to find out what it returns.

    The next thing you need to do is read the FAQ to find out how to tidy up the input stream of characters you don't want. Some people will tell you to use fflush(stdin). Ignore these people, they're clueless.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Cambridge, UK
    To include with Salem's post. You need to indent your code. That make it more ready.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  3. Help with variables (newbee)
    By stugatza in forum C Programming
    Replies: 7
    Last Post: 08-18-2004, 10:40 AM
  4. Currency Conversion Program
    By Dangerous_Dave in forum C Programming
    Replies: 2
    Last Post: 11-11-2003, 08:54 PM
  5. Replies: 2
    Last Post: 02-07-2002, 09:39 AM