Thread: Currency Converter with for loop question

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    4

    Currency Converter with for loop question

    I am working on this assignment where the program converts 3 US dollar amts into Danish Krones, Euros & Hong Kong Dollars. I have the program running but i am assuming some coding is wrong in my for loop. the 1st time it runs through the conversions are correct but the 2nd & 3rd time the conversions take the old conversion then multiply the new dollar amt to it creating a huge incorrect number. here is my code and help is appreciated.

    Code:
    #include <stdio.h>
    
    
    int main (void)
    {
        /*Variable declarations*/
        float usdollar = 0, krones = 0, euros = 0, hkdollars = 0, total = 0;
        int us_conversion = 3, i;
        
            /*Welcome and program description*/
        printf("Welcome to the Bombard Money Exchange\n\n");
        printf("The program will convert three US dollar amounts to\n
    Danish Krones, Euros and Hong Kong Dollars.\n\n");
        
        /*User enters exchange rates*/
        printf("First, Please enter the exchange rates:\n");    
    
    
        printf("Danish Krones: ");
        scanf("%f", &krones);     
        fflush (stdin);    
    
    
        printf("Euros: ");
        scanf("%f", &euros);    
        fflush (stdin);    
    
    
        printf("Hong Kong Dollars: ");
        scanf("%f", &hkdollars);     
        fflush (stdin);        
        
        /*loop to convert 3 amounts of US Dollars*/
        
        for (i=1; i <= us_conversion; ++i)
        {
            printf("\n\nEnter US Dollars #%i to be converted: ", i);
            scanf("%f", &usdollar);
            fflush (stdin);
        
            printf("\n%.2f US dollars is equivelent to:\n\n ", usdollar);
            krones = krones * usdollar;
            printf("\t%.2f Danish Krones\n", krones);
            euros = euros * usdollar;
            printf("\t%.2f Euros\n", euros);
            hkdollars = hkdollars * usdollar;
            printf("\t%.2f Hong Kong Dollars\n", hkdollars);
            total = total + usdollar;
        } /*end for loop*/
        
        total = (float) total + usdollar;
        printf("\n\nYou have converted a total of $%.2f\n", total);
    }
    /*end main*/

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    The problem is that you are disturbing the user entered conversion rates in your calculations...

    Try it like this instead...
    Code:
            printf("\n%.2f US dollars is equivelent to:\n\n ", usdollar);
            //krones = krones * usdollar;
            printf("\t%.2f Danish Krones\n", krones * usdollar);

  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
    You should get rid of all those fflush(stdin) calls.
    SourceForge.net: Fflush - cpwiki

    > krones = krones * usdollar;
    > printf("\t%.2f Danish Krones\n", krones);
    And what will be your exchange rate on the next iteration of the loop?

    Perhaps if you had a more meaningful name, say dollarsToKrones, the (il)logic would be more apparent?
    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
    Registered User
    Join Date
    Oct 2011
    Posts
    4
    Quote Originally Posted by Salem View Post
    You should get rid of all those fflush(stdin) calls.
    Thanks for the help. My professor wants us to use the fflush(stdin) after each scanf statement in our code...

    the sample the exchange rates would be:
    5.74087 danish krones
    .770602 euros
    7.77639 hong kong dollars

    with US dollar amts of 12.95, 32.00, 10

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Rebecca Bombard View Post
    Thanks for the help. My professor wants us to use the fflush(stdin) after each scanf statement in our code...
    Your professor is WRONG. With rare exception flushing an input stream is undefined behaviour. fflush() is designed to ensure that output streams are written to their targets ... for files this means written to disk, for the screen it means shown on the display... stdin is NOT an output stream...

    As Quzah would say... you don't flush a faucet... do you?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Post this to your "professor".
    Quote Originally Posted by c99
    7.19.5.2 The fflush function
    Synopsis
    1
    #include <stdio.h>
    int fflush(FILE *stream);
    Description
    2 If stream points to an output stream or an update stream in which the most recent
    operation was not input, the fflush function causes any unwritten data for that stream
    to be delivered to the host environment to be written to the file; otherwise, the behavior is
    undefined.
    3 If stream is a null pointer, the fflush function performs this flushing action on all
    streams for which the behavior is defined above.
    Returns
    4
    The fflush function sets the error indicator for the stream and returns EOF if a write
    error occurs, otherwise it returns zero.
    Do you see "input" mentioned (in the affirmative sense) anywhere?
    Index of /jtc1/sc22/wg14/www/docs/n869

    Is this same "professor" also rotting your brain by using TurboC as the course compiler by any chance?
    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.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    4
    Quote Originally Posted by Salem View Post
    Is this same "professor" also rotting your brain by using TurboC as the course compiler by any chance?
    no, he suggests we use C free but doesn't require a specific complier. i looked at my syllabus and it states -2 points for anytime fflush (stdin) is not used. so i thank you for pointing that out and i will keep it in mind, but for my assignments i'll keep it in for my professor to be happy.

    fyi: i figured out my issue by assigning more variables so that the exchange rates have a different variable than the conversion; so my equations would be

    krones = us_to_krones * us_dollar

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Rebecca Bombard View Post
    no, he suggests we use C free but doesn't require a specific complier. i looked at my syllabus and it states -2 points for anytime fflush (stdin) is not used. so i thank you for pointing that out and i will keep it in mind, but for my assignments i'll keep it in for my professor to be happy.

    fyi: i figured out my issue by assigning more variables so that the exchange rates have a different variable than the conversion; so my equations would be

    krones = us_to_krones * us_dollar
    Rebecca... did you even look at my post #2? You don't need more variables... You can do the conversion right in the printf() statement and then you can discard the line I commented out entirely.

    Also... In your place I would speak to my professor... "Hey teach, I found this and a bunch of guys on the forum confirmed that fflush(stdin) is a bad thing to do... Since working programmers don't use it, we should not be losing points over it." .... People are basically lazy, habit driven creatures... change does not come without cause.

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    4
    Quote Originally Posted by CommonTater View Post
    Rebecca... did you even look at my post #2? You don't need more variables... You can do the conversion right in the printf() statement and then you can discard the line I commented out entirely.
    yes, i did see that BUT it's another thing my professor said we shouldn't do. he did state equations in printf statements are valid, but he doesn't want us to use them.....

    so i guess it's another reason for you to hate my professor haha

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Rebecca Bombard View Post
    yes, i did see that BUT it's another thing my professor said we shouldn't do. he did state equations in printf statements are valid, but he doesn't want us to use them.....

    so i guess it's another reason for you to hate my professor haha
    Well, I do have to admit I'm starting to think he's something of an idiot...

    When you don't need to keep the result, assigning it to a variable is just plain silly.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I think I've just found my retirement hobby.
    Sign up for intro C programming courses around the country, then rip 'em a new one whenever the tutor starts spouting nonsense.
    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.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Salem View Post
    I think I've just found my retirement hobby.
    Sign up for intro C programming courses around the country, then rip 'em a new one whenever the tutor starts spouting nonsense.
    Good lord man... come this way first, my bags are already packed!

    Really... some of the stuff I've seen here just makes me want to scream...

    Remember that kid last winter who swore up and down he was in a C course but his teacher kept giving them C++ code...

  13. #13
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Rebecca Bombard View Post
    no, he suggests we use C free but doesn't require a specific complier. i looked at my syllabus and it states -2 points for anytime fflush (stdin) is not used.
    Yet another reason why I am glad I was not a CS major. The equivalent in my field would be for the prof to demand Newtonian physics be applied at the quantum level. Seriously, why is CS such a mis-represented topic?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    Yet another reason why I am glad I was not a CS major. The equivalent in my field would be for the prof to demand Newtonian physics be applied at the quantum level. Seriously, why is CS such a mis-represented topic?
    Times like this I am so glad I'm self-educated... the crap some of these schools teach is beyond disheartening.

    "Here I want you to use this code even though it orphans varaibles and relies upon undefined behavior"... Yeah, that'll work...

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    #include <stdio.h>
    
    
    int main (void)
    {
        /*Variable declarations*/
        float usdollar = 0, krones = 0, euros = 0, hkdollars = 0, total = 0;
        int us_conversion = 3, i;
        
            /*Welcome and program description*/
        printf("Welcome to the Bombard Money Exchange\n\n");
        printf("The program will convert three US dollar amounts to Danish Krones, Euros and Hong Kong Dollars.\n\n");
        
        /*User enters exchange rates*/
        printf("First, Please enter the exchange rates:\n");    
    
        printf("Danish Krones: ");
        scanf("%f", &krones);     
    
        printf("Euros: ");
        scanf("%f", &euros);    
    
        printf("Hong Kong Dollars: ");
        scanf("%f", &hkdollars);     
        
        printf("\n\nEnter US Dollars to be converted: ", i);
        scanf("%f", &usdollar);
       
        printf("\n%.2f US dollars is equivelent to:\n\n ", usdollar);
        printf("\t%.2f Danish Krones\n", krones * usdollar);
        printf("\t%.2f Euros\n", euros * usdollar);
        printf("\t%.2f Hong Kong Dollars\n", hkdollars * usdollar);
     
        return 0;
    }
    Last edited by CommonTater; 10-03-2011 at 04:11 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. converter currency in C
    By MyRedz in forum C Programming
    Replies: 7
    Last Post: 10-16-2008, 10:39 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

Tags for this Thread