Thread: converter currency in C

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    61

    converter currency in C

    hi..i am trying to do a currency converter in C..
    but there are errors that i can't figure out....
    and it don't accept negative numbers and characters...but mistake here..can anyone help?
    is there any simplified version or other C examples of a converter please...
    Code:
    #include<stdio.h>
    
    /* Conversion rates */
    float rate_peso = 0.787746;	
    float rate_franc = 1.23073;	
    float rate_euro = 0.781138;	
    float rate_kroner = 6.77551;	
    float rate_rand = 6.62313;	
    
    /*List functions*/
    void welcome();
    void result1(float num_usd);
    void goodbye();	
    float pesoToUSD(float rate_peso);
    float inputamount();
    
    main()
    {
    
    /* Define variables */
    float peso;	
    float num_usd;	
    
    /* Program execution */
    welcome();
    peso = inputamount();
    num_usd = pesoToUSD(peso);
    result1(num_usd);
    goodbye();
    return 0;
    }
    
    void welcome()
    {
    printf("\nCurrency Conversion\n");
    }
    
    /*Prompts for the number of Pesos to convert*/
    float inputamount()
    {
    /* local variables */
    float temp = 1.0;	 //Holds the return value
    char Ch;	 //input and error checking variable
    char *input;	 //Holds input string for conversion
    int isGood = 0;	 //Boolean 0 is 'false', 1 is 'true'
    int x = 0;	 //for loop counter
    int dec_count = 0;	 //counts number of decimals
    
    /*The do-while loop*/
    do
    {
    /* Input*/
    printf("\nEnter a positive number of Pesos to convert to USD.\n");
    gets (input);
    for(x=0; x < strlen(input); x++)
    
    {
    //Check each character in input string.
    //ASCII values 48-57 are the digits. (Win32)
    if (((input[x] >= 48 && input[x] <= 57) || (input[x] == '.' && dec_count == 0)))
    {
    isGood = 1; //indicates that input is valid
    }
    else
    {
    printf("\a");	//audible error signal
    isGood = 0;	//Repeat the do-while
    break;	 //breaks the for loop
    }
    if (input[x] == '.')
    dec_count++;
    }
    }while(isGood == 0);
    
    sscanf(input, "%f", &temp);	//reads the input string into a float
    return temp;
    }
    float pesoToUSD(float peso)
    {	
    float temp;
    temp = peso/rate_peso;
    //printf("\npeso=%f rate=%f temp=%f\n", peso, rate_peso, temp);
    return temp;
    }
    
    /*output*/
    
    void result1(float num_usd)
    {
    printf("\n\nThe amount of Pesos you entered is equal to $%.2f USD.\n", num_usd);
    }
    
    void goodbye()
    
    {
    
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Is this a serious question? You wrote a dozen lines, just so that the machine would beep at you if you typed in a nondigit (like, say, '-'), and you can't figure out why it won't take negative numbers?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    char *input; //Holds input string for conversion
    gets (input);

    Oh dear, Oh dear, Oh dear, Oh dear, Oh dear, Oh dear.
    Better put down whatever book you're learning from and find something else.

    This isn't even close to being even a little bit right.

    Oh, and would it kill you to use some indentation?
    That's f-ugly code.
    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.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I really hope this is one of those "fix this erroneous code" assignments.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Let's see what's wrong with the code...
    Unreadable code mess.
    Use of gets.
    Use of implicit main.
    Use of pointers without allocating memory.
    Assigning doubles to floats.
    Use of magic numbers.
    Oh dear.
    Sorry, but your code ranks 0 on a scale of 1 to 10.

    Learn to indent.
    Read: http://cpwiki.sourceforge.net/Gets
    Read: http://cpwiki.sourceforge.net/Implicit_main
    Read: http://cpwiki.sourceforge.net/Common...kes_and_errors
    Learn how to write a proper float number (not a double number).
    Learn how to use chars instead of magic numbers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by Elysia View Post
    Let's see what's wrong with the code...
    Unreadable code mess.
    Use of gets.
    Use of implicit main.
    Use of pointers without allocating memory.
    Assigning doubles to floats.
    Use of magic numbers.
    Oh dear.
    Sorry, but your code ranks 0 on a scale of 1 to 10.

    Learn to indent.
    Read: http://cpwiki.sourceforge.net/Gets
    Read: http://cpwiki.sourceforge.net/Implicit_main
    Read: http://cpwiki.sourceforge.net/Common...kes_and_errors
    Learn how to write a proper float number (not a double number).
    Learn how to use chars instead of magic numbers.
    Not only using gets() but using gets() with an unallocated pointer. Its like the gift that just keeps on giving... only Santa is giving out seg faults this season.

  7. #7
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by master5001 View Post
    Not only using gets() but using gets() with an unallocated pointer. Its like the gift that just keeps on giving... only Santa is giving out seg faults this season.
    I hope he gets a debugger from his parents, then.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    61
    well thanks..i will try to fix it first..huhuhu

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. Basic C Help - Currency Converter
    By liljp617 in forum C Programming
    Replies: 9
    Last Post: 09-07-2008, 10:12 PM
  3. Newbie Help: Currency Converter
    By Ashfury in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 01:21 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, 03:27 AM