Currency conversion program

Discussion in 'C Programming' started by Just starting out, Sep 5, 2005.

  1. I am very new to C code and I'm having a lot of trouble with a homework
    assignment.

    This program is supposed to take the amount of Euros that the user
    enters and convert it to US dollars.

    It runs fine if the user enters a number, but if the user enters a
    letter it loops.

    I have been working on this for 3 hours now, trying different things
    left and right. I'm sure it has something to do with the isdigit
    function. So, I tried adding in a char cResponse and switching the
    value to fResponse after checking for a number.

    I am fried, please help!

    ---------------------------------------

    #include <stdio.h>
    #include <system.h>
    #include <ctype.h>

    main ()

    {

    //set variables
    float fUSD, fEUR, fResponse;

    //initialize variables
    fUSD = 0;
    fEUR = 0;
    fResponse = 0;

    //set values
    fUSD = 1.00;
    fEUR = .7435;


    //print headers for output screen
    printf("\n***Currency Conversion***\n");
    printf("\nConverts Euro into US dollar\n");
    printf("\nPlease enter the amount in Euro: ");

    //get user input
    scanf("%f", &fResponse);

    //check for number greater than zero
    while (fResponse<=0.0) {
    if (isdigit(fResponse)) {
    printf("\nThe number you entered is invald.\n");
    printf("Please enter a number larger than zero: ");
    scanf("%f", &fResponse);
    }
    if (isdigit(fResponse)==0)
    printf("\nYou entered a letter.\n");
    printf("Please enter a number larger than zero: ");
    scanf("%f", &fResponse);

    }

    printf("\nThe value of the Euro you entered converts to $%.2f US
    dollars.\n", (fUSD/fEUR) * fResponse);

    printf("\n\nPress any key to close this window. . .");

    //getch function to leave results on screen until any key is chosen
    getch ();

    }
    Just starting out, Sep 5, 2005
    #1
    1. Advertising

  2. Just starting out

    Guest

    Just starting out wrote:

    >>I am very new to C code and I'm having a lot of trouble with a homework
    >>assignment.



    >>This program is supposed to take the amount of Euros that the user
    >>enters and convert it to US dollars.



    >>It runs fine if the user enters a number, but if the user enters a
    >>letter it loops.

    why it loops almostly is due to the loop conditions, focus on the
    conditions to check out what's the key point of the problem by
    unfolding your loop statement.

    >>I have been working on this for 3 hours now, trying different things
    >>left and right. I'm sure it has something to do with the isdigit
    >>function.

    maybe, but, I dont's think it is indispensable.
    >> So, I tried adding in a char cResponse and switching the
    >>value to fResponse after checking for a number.



    >>I am fried, please help!



    ------------------------------­---------


    >#include <stdio.h>
    >#include <system.h>
    >#include <ctype.h>



    >main ()

    undefined behavior.
    int main(void)

    >{



    >//set variables
    >float fUSD, fEUR, fResponse;



    > //initialize variables
    > fUSD = 0;
    > fEUR = 0;
    > fResponse = 0;

    I think it may be better to initialize like the following:
    fUSD = 0.0;
    fEUR = 0.0;
    fResponse = 0.0;
    or
    fUSD = 0f;
    fEUR = 0f;
    fResponse = 0f;

    > //set values
    > fUSD = 1.00;
    > fEUR = .7435;



    >//print headers for output screen
    >printf("\n***Currency Conversion***\n");
    >printf("\nConverts Euro into US dollar\n");
    >printf("\nPlease enter the amount in Euro: ");



    >//get user input
    >scanf("%f", &fResponse);



    > //check for number greater than zero
    > while (fResponse<=0.0) {


    > if (isdigit(fResponse)) {
    > printf("\nThe number you entered is invald.\n");
    > printf("Please enter a number larger than zero: ");
    > scanf("%f", &fResponse);
    > }
    > if (isdigit(fResponse)==0)
    > printf("\nYou entered a letter.\n");
    > printf("Please enter a number larger than zero: ");
    > scanf("%f", &fResponse);


    >
    > }

    I think the code fragment "get user input" above could be replaced by
    this one:
    ***********************************************************
    while(scanf("%f", &fResponse) == 0)
    {
    printf("\nThe number you entered is invald.\n");
    printf("Please enter a number larger than zero: ");
    getchar();
    }
    ************************************************************
    if "scanf("%f", &fResponse)" get a digital data, it will return the
    number of data it gets,and return 0 while it gets nothing.in other
    words it gets nothing means the user input the wrong format data.
    getchar() will eat the new-line character left by the previous scanf()
    before the next loop starting.
    >>

    printf("\nThe value of the Euro you entered converts to
    $%.2f US
    >>dollars.\n", (fUSD/fEUR) * fResponse);



    >>printf("\n\nPress any key to close this window. . .");



    >>//getch function to leave results on screen until any key is chosen
    >>getch ();


    }
    , Sep 5, 2005
    #2
    1. Advertising

  3. Just starting out

    Stan Milam Guest

    Just starting out wrote:
    > I am very new to C code and I'm having a lot of trouble with a homework
    > assignment.
    >
    > This program is supposed to take the amount of Euros that the user
    > enters and convert it to US dollars.
    >
    > It runs fine if the user enters a number, but if the user enters a
    > letter it loops.
    >
    > I have been working on this for 3 hours now, trying different things
    > left and right. I'm sure it has something to do with the isdigit
    > function. So, I tried adding in a char cResponse and switching the
    > value to fResponse after checking for a number.
    >
    > I am fried, please help!
    >
    > ---------------------------------------
    >
    > #include <stdio.h>
    > #include <system.h>
    > #include <ctype.h>
    >
    > main ()
    >
    > {
    >
    > //set variables
    > float fUSD, fEUR, fResponse;
    >
    > //initialize variables
    > fUSD = 0;
    > fEUR = 0;
    > fResponse = 0;
    >
    > //set values
    > fUSD = 1.00;
    > fEUR = .7435;
    >
    >
    > //print headers for output screen
    > printf("\n***Currency Conversion***\n");
    > printf("\nConverts Euro into US dollar\n");
    > printf("\nPlease enter the amount in Euro: ");
    >
    > //get user input
    > scanf("%f", &fResponse);
    >
    > //check for number greater than zero
    > while (fResponse<=0.0) {
    > if (isdigit(fResponse)) {
    > printf("\nThe number you entered is invald.\n");
    > printf("Please enter a number larger than zero: ");
    > scanf("%f", &fResponse);
    > }
    > if (isdigit(fResponse)==0)
    > printf("\nYou entered a letter.\n");
    > printf("Please enter a number larger than zero: ");
    > scanf("%f", &fResponse);
    >
    > }
    >
    > printf("\nThe value of the Euro you entered converts to $%.2f US
    > dollars.\n", (fUSD/fEUR) * fResponse);
    >
    > printf("\n\nPress any key to close this window. . .");
    >
    > //getch function to leave results on screen until any key is chosen
    > getch ();
    >
    > }
    >


    I would look at the use of the scanf() function. Did anything actually
    get assigned to fResponse in scanf()?
    Stan Milam, Sep 5, 2005
    #3
  4. Just starting out

    Old Wolf Guest

    Just starting out wrote:
    > I am very new to C code and I'm having a lot of trouble with
    > a homework assignment.
    >
    > This program is supposed to take the amount of Euros that the user
    > enters and convert it to US dollars.
    >
    > It runs fine if the user enters a number, but if the user enters a
    > letter it loops.


    You need to check whether your scanf() function succeeds or
    fails, and take some action if it fails.

    (Read the manual for 'scanf' to find out how to do this).

    Currently you do no check, so if the user enters a letter then
    the program just carries on merrily as if they had entered
    whatever the fResponse variable already contained.

    > //check for number greater than zero
    > while (fResponse<=0.0) {
    > if (isdigit(fResponse)) {


    I am not sure if you understand what "isdigit" does. It
    operates on a character, and checks to see if that character
    is a '0', a '1', a '2' , ..., or a '9'.

    fResponse is a float. Checking to see whether it contains
    the integer code for a digit character is not very useful.

    In fact this test can never succeed, because the ASCII
    (I presume) codes for the digits are between 48 and 57, and
    you only enter this test if fResponse is 0 or a negative number.
    Old Wolf, Sep 5, 2005
    #4
  5. Just starting out

    Flash Gordon Guest

    wrote:
    > Just starting out wrote:
    >
    >>>I am very new to C code and I'm having a lot of trouble with a homework
    >>>assignment.

    >
    >>>This program is supposed to take the amount of Euros that the user
    >>>enters and convert it to US dollars.

    >
    >>>It runs fine if the user enters a number, but if the user enters a
    >>>letter it loops.

    >
    > why it loops almostly is due to the loop conditions, focus on the
    > conditions to check out what's the key point of the problem by
    > unfolding your loop statement.


    Actually the problem is with scanf. See Old Wolf's reply.

    >>>I have been working on this for 3 hours now, trying different things
    >>>left and right. I'm sure it has something to do with the isdigit
    >>>function.

    >
    > maybe, but, I dont's think it is indispensable.


    The use of isdigit is definitely a problem. The OP needs to decide
    whether to read a character at a time (in which case isdigit is usefule)
    or whether to use scanf to read a number in which case the return value
    of scanf should be checked.

    >>>So, I tried adding in a char cResponse and switching the
    >>>value to fResponse after checking for a number.

    >
    >>>I am fried, please help!

    >
    > ------------------------------­---------
    >
    >>#include <stdio.h>
    >>#include <system.h>
    >>#include <ctype.h>

    >
    >>main ()

    >
    > undefined behavior.


    No. It is perfectly valid (but bad style) on C89 and a constraint
    violation on C99 where implicit int has been removed from the language.

    > int main(void)


    That is correct.

    >>{

    >
    >>//set variables


    // style comments are only valid in C99, so the OP is not invoking the
    compiler as either a proper C99 or a proper C89 compiler but in some
    other mode. The OP should check the instructions for the compiler to see
    how to make it behave decently.

    Also, // comments are not advisable on news groups because they cause
    problems when the line wraps.

    >>float fUSD, fEUR, fResponse;

    >
    >> //initialize variables

    >
    > > fUSD = 0;


    You (kernelxu) seem to be inserting spaces at random in front of quote
    characters. This makes it harder to read you post, so please avoid doing
    this.

    > > fEUR = 0;
    > > fResponse = 0;

    > I think it may be better to initialize like the following:
    > fUSD = 0.0;
    > fEUR = 0.0;
    > fResponse = 0.0;
    > or
    > fUSD = 0f;
    > fEUR = 0f;
    > fResponse = 0f;


    That will make absolutely no difference. The OP's initialisation is
    perfectly OK.

    > > //set values
    > > fUSD = 1.00;
    > > fEUR = .7435;

    >
    >
    >
    >>//print headers for output screen
    >>printf("\n***Currency Conversion***\n");
    >>printf("\nConverts Euro into US dollar\n");
    >>printf("\nPlease enter the amount in Euro: ");

    >
    >
    >
    >>//get user input
    >>scanf("%f", &fResponse);


    You probably want the return value of scanf. Check your text book to see
    what it does with input that does not match the format specifier.

    >> //check for number greater than zero

    >
    > > while (fResponse<=0.0) {

    >
    >
    >> if (isdigit(fResponse)) {


    The use of isdigit here is wrong. You use it on characters, not floating
    point numbers, please read that part of your text book again as well.

    >> printf("\nThe number you entered is invald.\n");

    >
    > > printf("Please enter a number larger than zero: ");


    You need to end with a new line or flush stdout.

    >> scanf("%f", &fResponse);


    See previour comment on scanf.

    >> }

    >
    > > if (isdigit(fResponse)==0)
    > > printf("\nYou entered a letter.\n");

    >
    >> printf("Please enter a number larger than zero: ");


    You need to end with a new line or flush stdout.

    > > scanf("%f", &fResponse);


    See previour comment on scanf.

    > > }

    > I think the code fragment "get user input" above could be replaced by
    > this one:
    > ***********************************************************
    > while(scanf("%f", &fResponse) == 0)
    > {
    > printf("\nThe number you entered is invald.\n");
    > printf("Please enter a number larger than zero: ");


    The text of the second printf might not have been displayed. You either
    need to flush stdout or finish the line with a new line character.

    > getchar();
    > }


    This is still wrong.

    > ************************************************************
    > if "scanf("%f", &fResponse)" get a digital data, it will return the
    > number of data it gets,and return 0 while it gets nothing.in other
    > words it gets nothing means the user input the wrong format data.
    > getchar() will eat the new-line character left by the previous scanf()
    > before the next loop starting.


    Did you try running your code and entering more than a single bad
    character? scanf stops as soon as ti hits a matching failure, so more
    than one character might be left on the input stream.

    > printf("\nThe value of the Euro you entered converts to
    > $%.2f US
    >
    >>>dollars.\n", (fUSD/fEUR) * fResponse);

    >
    >
    >
    >>>printf("\n\nPress any key to close this window. . .");


    Again with the new line or flushing stdout.

    >>>//getch function to leave results on screen until any key is chosen
    >>>getch ();


    C does not have a "getch" function.

    > }
    >

    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
    Flash Gordon, Sep 5, 2005
    #5
  6. Just starting out

    Guest

    Thank you Flash Gordon for pointing out my fault.
    I am a newbie of C too, I love the group very much.
    Sometimes I just can't help myself to join the discussion.
    Please forgive my imperence. I will pay more attention on learning.
    , Sep 5, 2005
    #6
  7. I replaced the "get user input" code fragment with the while statement
    you recommended. It's better in that it doesn't loop anymore, but if I
    enter more than one letter it prints the two printf statements that
    many times. (if i enter stop it prints it 4 times)

    I'm looking into how to fix this part now. Thanks for your feedback!
    Just starting out, Sep 5, 2005
    #7
  8. Yes, I've scrapped the whole isdigit function for this program. I have
    a better understanding of that function now and realize it is not
    usable here.

    Thanks for your response!
    Just starting out, Sep 5, 2005
    #8
  9. Flash,

    Thanks for your feedback!

    I'm using Miracle C compiler for my assignments.

    I'm not sure what you mean by ending the printf statement with a new
    line or flush stdout. I wanted the user input to appear at the end of
    the "Please enter a number: " line. I checked my book for flush stdout
    and it looks like I need a new book.

    I replaced my original scanf with the "while(scanf..." code that
    kernelxu suggested. The two printf lines both print according to the
    number of letters the user enters. (ex. user enters STOP, the printf
    lines both print 4 times).

    Our class was told to use getch(); function in order to keep the window
    from automatically closing. Maybe it's a Miracle C thing.
    Just starting out, Sep 5, 2005
    #9
  10. On 5 Sep 2005 11:54:12 -0700, "Just starting out"
    <> wrote:

    >Flash,
    >
    >Thanks for your feedback!
    >
    >I'm using Miracle C compiler for my assignments.
    >
    >I'm not sure what you mean by ending the printf statement with a new
    >line or flush stdout. I wanted the user input to appear at the end of
    >the "Please enter a number: " line. I checked my book for flush stdout
    >and it looks like I need a new book.


    You have to provide some context. What code are you talking about.
    The fact that the google interface is broken means you have to do it
    manually.

    The function you want is fflush. What book are you using?

    >
    >I replaced my original scanf with the "while(scanf..." code that
    >kernelxu suggested. The two printf lines both print according to the
    >number of letters the user enters. (ex. user enters STOP, the printf
    >lines both print 4 times).
    >
    >Our class was told to use getch(); function in order to keep the window


    I would be better if you were told to use a standard function like
    getchar(). Not everyone has non-standard extensions like getch().

    >from automatically closing. Maybe it's a Miracle C thing.


    No, it's a Windows thing. When main() returns, your window may close
    before you can see its contents. Putting a getchar() prior to the
    return is intended to insure the window stays open until you hit Enter
    signifying you are done looking.


    <<Remove the del for email>>
    Barry Schwarz, Sep 5, 2005
    #10
  11. Just starting out

    Flash Gordon Guest

    Just starting out wrote:
    > Flash,
    >
    > Thanks for your feedback!


    Provide context otherwise we can't see what you are replying to. I, for
    one, don't bother to remember every single post I make to news groups
    and have my reader configured to only show unread posts. Others might
    not have even seen my post. Check any post by CBFalconer and read his
    sig for instructions on how to work around the crappy interface Google
    provide.

    > I'm using Miracle C compiler for my assignments.


    Irrelevant.

    > I'm not sure what you mean by ending the printf statement with a new
    > line or flush stdout. I wanted the user input to appear at the end of
    > the "Please enter a number: " line. I checked my book for flush stdout
    > and it looks like I need a new book.


    You use
    fflush(stdout);
    to flush the output. If you don't then the C language does not guarantee
    that the message will actually be displayed before the user presses
    return when entering data. It's called line buffering.

    > I replaced my original scanf with the "while(scanf..." code that
    > kernelxu suggested. The two printf lines both print according to the
    > number of letters the user enters. (ex. user enters STOP, the printf
    > lines both print 4 times).


    If I recall correctly I posted comments about the problems with his
    code. Read up on how scanf works. Of course, since you have not posted
    or quoted the code I can't see to tell you the specific problems.

    > Our class was told to use getch(); function in order to keep the window
    > from automatically closing. Maybe it's a Miracle C thing.


    It may well be, but it is not part of the C language.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
    Flash Gordon, Sep 5, 2005
    #11
  12. Flash Gordon wrote:
    > Just starting out wrote:
    > > ...
    > > I'm not sure what you mean by ending the printf statement with a
    > > new line or flush stdout. I wanted the user input to appear at the
    > > end of the "Please enter a number: " line. I checked my book for
    > > flush stdout and it looks like I need a new book.

    >
    > You use
    > fflush(stdout);
    > to flush the output. If you don't then the C language does not
    > guarantee that the message will actually be displayed before the
    > user presses return when entering data.


    Even using fflush(stdout), the C language won't make that guarantee.
    [Try redirecting stdout to an old line buffered printer one day.]

    > It's called line buffering.


    Indeed. Whilst the _stream_ can be flushed, there's no guarantee
    that the 'display device' won't have it's own buffering. Using
    fflush() will work for 99.999% of consoles, but for the maximum
    chance of the prompt appearing, it should end with a newline.

    i.e. use...

    puts("Enter an integer:");

    ....over...

    printf("Enter an integer: ");
    fflush(stdout);

    > > Our class was told to use getch(); function in order to keep the
    > > window from automatically closing. Maybe it's a Miracle C thing.


    No, it's largely a windoze thing. Nonetheless, there are better
    options,
    although that discussion is off-topic in clc.

    --
    Peter
    Peter Nilsson, Sep 6, 2005
    #12
  13. "Peter Nilsson" <> writes:
    [...]
    > Indeed. Whilst the _stream_ can be flushed, there's no guarantee
    > that the 'display device' won't have it's own buffering. Using
    > fflush() will work for 99.999% of consoles, but for the maximum
    > chance of the prompt appearing, it should end with a newline.
    >
    > i.e. use...
    >
    > puts("Enter an integer:");
    >
    > ...over...
    >
    > printf("Enter an integer: ");
    > fflush(stdout);


    As much as I'm a fanatic for portable code, I don't agree. Printing a
    prompt without a newline makes for a much friendlier interactive
    program. On a system where the printf/fflush form doesn't work, I
    wouldn't be sure that prompting would work at all.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Sep 12, 2005
    #13
  14. Barry Schwarz <> writes:
    [...]
    > You have to provide some context. What code are you talking about.
    > The fact that the google interface is broken means you have to do it
    > manually.


    No, you don't have to do it manually. All you have to do is (all
    together now):

    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.

    and it will quote the previous article and fill in proper
    attributions.

    Google's unforgivable blunder is, first, putting this in a hidden menu
    rather than making it the default, and second, not fixing it to this
    day.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Sep 12, 2005
    #14
  15. >> Indeed. Whilst the _stream_ can be flushed, there's no guarantee
    >> that the 'display device' won't have it's own buffering. Using
    >> fflush() will work for 99.999% of consoles, but for the maximum
    >> chance of the prompt appearing, it should end with a newline.
    >>
    >> i.e. use...
    >>
    >> puts("Enter an integer:");
    >>
    >> ...over...
    >>
    >> printf("Enter an integer: ");
    >> fflush(stdout);

    >
    >As much as I'm a fanatic for portable code, I don't agree. Printing a
    >prompt without a newline makes for a much friendlier interactive
    >program. On a system where the printf/fflush form doesn't work, I
    >wouldn't be sure that prompting would work at all.


    For whatever it's worth, there are hardcopy printers, some with
    keyboards, that don't print anything but whole lines. The prompt
    wouldn't appear as ink on paper until after you'd answered it. There
    are others where the prompt is printed but the printhead is in
    the way until the paper has advanced.

    Gordon L. Burditt
    Gordon Burditt, Sep 12, 2005
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martyn Fewtrell

    String (Currency) to Decimal Conversion

    Martyn Fewtrell, Jun 21, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    4,145
    Martyn Fewtrell
    Jun 21, 2005
  2. Willing 2 Learn

    Currency Conversion using OO

    Willing 2 Learn, Oct 11, 2005, in forum: C++
    Replies:
    3
    Views:
    438
    Greg Comeau
    Oct 11, 2005
  3. Willing 2 Learn

    Currency Conversion using OO

    Willing 2 Learn, Oct 12, 2005, in forum: C++
    Replies:
    2
    Views:
    516
    Bob Hairgrove
    Oct 12, 2005
  4. Willing 2 Learn

    Currency Conversion using OO

    Willing 2 Learn, Oct 12, 2005, in forum: C++
    Replies:
    0
    Views:
    256
    Willing 2 Learn
    Oct 12, 2005
  5. powerranger

    currency conversion webservice

    powerranger, Jan 25, 2004, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    93
    Jan Tielens
    Jan 26, 2004
Loading...

Share This Page