Newbie Program That Started Off Easy - Simple Math w/ Interactive Menu

Discussion in 'C Programming' started by Mike, Oct 31, 2004.

  1. Mike

    Mike Guest

    Hello,

    This program uses C. It is meant to convert whatever the current
    exchange rate is to dollars. It compiles, builds, and executes
    correctly except for a few bugs. When building I get an error that
    states;

    Linker Error (Severity 4)
    Module "a" in file "c:\program files\miracle c\yendollarext.obj"
    references unresolved external "_rewind"
    at offset 0303H in segment "_text".

    I am not sure how to resolve this error.

    Also, I attempted to have the program create an error whenever someone
    either "chooses" a number higher than 1 "else if(choice > '1')" or
    attempts to use an alpha character

    "if (isalpha(yen))

    {

    printf("\n Sorry, that was not a valid option.\n\n");"

    break;

    in which the program shuts down without displaying the error message.
    Any ideas? Here is the code, all help is appreciated.

    #include <stdio.h>
    #define DRAINO rewind(stdin);fflush(stdout);
    #define STOP rewind(stdin); getchar();



    int main( void )
    {
    double yen = 0.0;
    double dollars = 0.0;
    double yentoDollar = 0.0;

    int choice=0;


    printf("\nYen to Dollar Currency Conversion\n\n");

    printf("Enter 1 to begin Conversionn\n");

    printf("Enter 0 to Quit the Program\n\n");


    printf("Please select your option:\n\n");

    scanf("%d", &choice);


    if (choice == 1)

    {

    printf("\nYou want to convert Yen into US dollars.\n\n");
    printf("Please enter the current exchange rate between Dollars and
    Yen.\n\n");
    scanf( "%lf", &yentoDollar );


    if (isalpha(yentoDollar))

    {

    printf("Sorry, that was not a valid option.");
    getchar ();
    break;

    }

    printf("\nHow many Yen do you have?\n\n");
    scanf( "%lf", &yen );
    if (isalpha(yen))

    {

    printf("\n Sorry, that was not a valid option.\n\n");

    break;

    }
    dollars = yen * yentoDollar;
    printf( "\nThe conversion to US dollars of %.2f yen is %.2f
    dollars\n", yen, dollars );


    rewind(stdin);

    getchar();

    }

    else if (choice == 0)

    {
    printf("Program will terminate after you click Enter.\n");
    printf("\nHave a nice day!");
    getchar ();
    return 0;

    }

    else if(choice > '1')

    {

    printf("\nProgram will terminate after you click Enter.\n");
    printf("\nHave a nice day!");
    getchar ();
    return 0;

    }

    }

    Thanks in advance!

    Mike
     
    Mike, Oct 31, 2004
    #1
    1. Advertising

  2. Mike

    CBFalconer Guest

    Re: Newbie Program That Started Off Easy - Simple Math w/ InteractiveMenu

    Mike wrote:
    >
    > This program uses C. It is meant to convert whatever the current
    > exchange rate is to dollars. It compiles, builds, and executes
    > correctly except for a few bugs. When building I get an error that
    > states;
    >

    .... snip ...
    >
    > in which the program shuts down without displaying the error
    > message. Any ideas? Here is the code, all help is appreciated.
    >
    > #include <stdio.h>
    > #define DRAINO rewind(stdin);fflush(stdout);
    > #define STOP rewind(stdin); getchar();


    You can't rewind stdin.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Oct 31, 2004
    #2
    1. Advertising

  3. Mike

    -berlin.de Guest

    Mike <> wrote:
    > This program uses C. It is meant to convert whatever the current
    > exchange rate is to dollars. It compiles, builds, and executes
    > correctly except for a few bugs. When building I get an error that
    > states;


    > Linker Error (Severity 4)
    > Module "a" in file "c:\program files\miracle c\yendollarext.obj"
    > references unresolved external "_rewind"
    > at offset 0303H in segment "_text".


    > I am not sure how to resolve this error.


    That's a question that's more or less off-topic here in clc since
    it's not about the language C but a problem with a certain compiler/
    linker combination. There probably exist better places to ask such
    questions, i.e. a forum that deals with your compiler/linker.

    I re-indented the program a bit to make it more readable...

    > #include <stdio.h>
    > #define DRAINO rewind(stdin);fflush(stdout);
    > #define STOP rewind(stdin); getchar();


    Chuck already told you that you can't rewind() stdin. But since you
    don't use these macros you can delete them anyway.

    > int main( void )


    > {
    > double yen = 0.0;
    > double dollars = 0.0;
    > double yentoDollar = 0.0;
    > int choice=0;
    >
    > printf("\nYen to Dollar Currency Conversion\n\n");
    > printf("Enter 1 to begin Conversionn\n");
    > printf("Enter 0 to Quit the Program\n\n");
    > printf("Please select your option:\n\n");
    > scanf("%d", &choice);


    Let's hope the user really enters a number otherwise strange things
    may happen... Using fgets() to read a complete line and then check
    what it contains and, if possible, get the value you're looking for
    from the line would be a lot safer.

    > if (choice == 1)
    > {
    > printf("\nYou want to convert Yen into US dollars.\n\n");
    > printf("Please enter the current exchange rate between Dollars "
    > "and Yen.\n\n");
    > scanf( "%lf", &yentoDollar );
    >
    > if (isalpha(yentoDollar))


    'yentoDollar' is a double, not a char, so you can't call isalpha()
    on it. What's that supposed to do anyway? If the scanf() call worked
    as expected then you can be sure that 'yentoDollar' is a floating
    point number (if it's value is reasonable is a different question).

    > {
    > printf("Sorry, that was not a valid option.");
    > getchar ();
    > break;


    If there was an error wouldn't it make more sense to exit() the
    program instead of using a 'break' that doesn't make sense here
    since you're not within a loop?

    > }
    >
    > printf("\nHow many Yen do you have?\n\n");
    > scanf( "%lf", &yen );
    > if (isalpha(yen))


    Same problem as above: 'yen' is a double, not a char.

    > {
    > printf("\n Sorry, that was not a valid option.\n\n");
    > break;
    > }
    >
    > dollars = yen * yentoDollar;
    > printf( "\nThe conversion to US dollars of %.2f yen is %.2f "
    > "dollars\n", yen, dollars );
    >
    > rewind(stdin);


    That rewind() doesn't make sense. What do you expect it to do?

    > getchar();
    > }
    > else if (choice == 0)
    > {
    > printf("Program will terminate after you click Enter.\n");


    Where am I supposed to click on? Didn't you mean "press" instead
    of "click"?;-)

    > printf("\nHave a nice day!");
    > getchar ();
    > return 0;
    > }
    > else if (choice > '1')


    I guess you want to compare 'choice' to the number 1, not the character
    '1'. And since you seem to want to catch everything here except 0 and 1
    (which you already dealt with) you should drop the if clause here
    (remember that 'choice' also could be negative).

    > {
    > printf("\nProgram will terminate after you click Enter.\n");
    > printf("\nHave a nice day!");
    > getchar ();
    > return 0;
    > }
    >


    You're missing a return statement here (you end here if 'choice' was 1).

    > }

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, Oct 31, 2004
    #3
  4. [forewarning - I am relatively new to C myself, and this is the first
    time I've responded in c.l.c. Take my answers with a grain of salt,
    and a google search for man \function name\ is a better reference than
    I]


    (Mike) wrote in message news:<>...
    > Hello,
    >
    > This program uses C. It is meant to convert whatever the current
    > exchange rate is to dollars. It compiles, builds, and executes
    > correctly except for a few bugs. When building I get an error that
    > states;
    >
    > Linker Error (Severity 4)
    > Module "a" in file "c:\program files\miracle c\yendollarext.obj"
    > references unresolved external "_rewind"
    > at offset 0303H in segment "_text".
    >
    > I am not sure how to resolve this error.
    >


    What compiler are you using? I am not familiar with this error
    message. Compiler-specific error questions may be better posted to a
    newsgroup dealing with your compiler.

    > Also, I attempted to have the program create an error whenever someone
    > either "chooses" a number higher than 1 "else if(choice > '1')"
    > or attempts to use an alpha character
    >
    > "if (isalpha(yen))
    >
    > {
    >
    > printf("\n Sorry, that was not a valid option.\n\n");"
    >
    > break;
    >
    > in which the program shuts down without displaying the error message.
    > Any ideas? Here is the code, all help is appreciated.
    >
    > #include <stdio.h>
    > #define DRAINO rewind(stdin);fflush(stdout);
    > #define STOP rewind(stdin); getchar();
    >
    >
    >
    > int main( void )
    > {
    > double yen = 0.0;
    > double dollars = 0.0;
    > double yentoDollar = 0.0;
    >
    > int choice=0;
    >
    >
    > printf("\nYen to Dollar Currency Conversion\n\n");
    >
    > printf("Enter 1 to begin Conversionn\n");
    >
    > printf("Enter 0 to Quit the Program\n\n");
    >
    >
    > printf("Please select your option:\n\n");
    >
    > scanf("%d", &choice);
    >
    >
    > if (choice == 1)
    >
    > {
    >
    > printf("\nYou want to convert Yen into US dollars.\n\n");
    > printf("Please enter the current exchange rate between Dollars and
    > Yen.\n\n");
    > scanf( "%lf", &yentoDollar );
    >


    I may be wrong, but "%f" should used instead of "%lf" - you are not
    using a long double.

    >
    > if (isalpha(yentoDollar))
    >
    > {
    >
    > printf("Sorry, that was not a valid option.");
    > getchar ();
    > break;
    >
    > }


    isalpha() requires the library <ctype.h>. However, if the user enters
    a letter, scanf will not put anything into \yentoDollar\ and the
    condition if(isalpha(yentoDollar)) will always evaluate as false.
    Assuming you want the program to shut down on invalid input, the
    following would be a better method for data input and checking
    (tab-spacing mangled by google groups):

    char line_Buffer[100];
    ....
    fgets(line_Buffer, sizeof(line), stdin);
    if(sscanf(line_Buffer, "%f", &yentoDollar) != 1) {
    printf("Sorry, that was not a valid option.\n");
    return(1);
    }

    This will make your program exit if the user does not enter a float or
    double. You may also want to check if the user entered a negative
    number, and use a loop to continue prompting the user until valid
    input has been entered.

    >
    > printf("\nHow many Yen do you have?\n\n");
    > scanf( "%lf", &yen );
    > if (isalpha(yen))
    >
    > {
    >
    > printf("\n Sorry, that was not a valid option.\n\n");
    >
    > break;
    >
    > }


    Again, isalpha() should not be used, and "%f" should be used instead
    of "%lf". See above.

    > dollars = yen * yentoDollar;
    > printf( "\nThe conversion to US dollars of %.2f yen is %.2f
    > dollars\n", yen, dollars );
    >
    >
    > rewind(stdin);
    >


    Why are you calling rewind()? It seems unnecessary.

    > getchar();
    >
    > }
    >
    > else if (choice == 0)
    >
    > {
    > printf("Program will terminate after you click Enter.\n");
    > printf("\nHave a nice day!");
    > getchar ();
    > return 0;
    >
    > }
    >
    > else if(choice > '1')
    >


    Your variable 'choice' is an int, so you do not need the single-quotes
    around the number 1. Also, your program's behavior is the same if
    choice == 0 or choice > 1, so the two conditions can be combined into
    one. Futhermore, you leave open the possibility that \choice\ will be
    negative. I suggest you modify the condition like so:

    else if(choice <= 0 || choice > 1) {
    /* print message and exit */
    }

    > {
    >
    > printf("\nProgram will terminate after you click Enter.\n");
    > printf("\nHave a nice day!");
    > getchar ();
    > return 0;
    >
    > }
    >
    > }
    >
    > Thanks in advance!
    >
    > Mike


    Your welcome! I hope this helps.

    Best,
    Michael Hannon
     
    Michael Hannon, Oct 31, 2004
    #4
  5. Mike

    -berlin.de Guest

    Michael Hannon <> wrote:
    >> printf("\nYou want to convert Yen into US dollars.\n\n");
    >> printf("Please enter the current exchange rate between Dollars and
    >> Yen.\n\n");
    >> scanf( "%lf", &yentoDollar );
    >>


    > I may be wrong, but "%f" should used instead of "%lf" - you are not
    > using a long double.


    That's not the case. You need '%f" for a float and "%lf" for a double
    (and 'yentoDollar' is a double), at least with scanf() and the related
    functions fscanf() and sscanf(). IIRC, you would need "%Lf" for a long
    double. Please note that there's an asymmetry between the scanf() and
    the printf() functions, for printf() you need a "%f" both for float and
    double.
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, Oct 31, 2004
    #5
  6. Mike

    Mike Guest

    Re: Newbie Program That Started Off Easy - Simple Math w/ Interactive Menu

    CBFalconer wrote

    You can't rewind stdin.

    Thanks, that was easy to get rid of the error message, but how do I
    get the program to rewind to the beginning?
     
    Mike, Oct 31, 2004
    #6
  7. Mike

    -berlin.de Guest

    Re: Newbie Program That Started Off Easy - Simple Math w/ Interactive Menu

    Mike <> wrote:
    > CBFalconer wrote


    > You can't rewind stdin.


    > Thanks, that was easy to get rid of the error message, but how do I
    > get the program to rewind to the beginning?


    I guess by "rewinding to the beginning" you mean that you want to
    ask the user again what (s)he wants to do after you did a conversion.
    For that you must put everything in a loop, probably a do or while loop
    would be most suitable.

    You could e.g. run your code in a infinite loop like this:

    while( 1 ) { /* repeats infinitely since 1 is always true */

    do everything you did before in here

    }

    and do either a 'break' from that loop if the user want's to stop
    or exit() directly in that case.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, Oct 31, 2004
    #7
  8. Mike

    Mike Guest

    (Michael Hannon) wrote in message news:<>...
    > [forewarning - I am relatively new to C myself, and this is the first
    > time I've responded in c.l.c. Take my answers with a grain of salt,
    > and a google search for man \function name\ is a better reference than
    > I]
    >
    >
    > (Mike) wrote in message news:<>...
    > > Hello,
    > >
    > > This program uses C. It is meant to convert whatever the current
    > > exchange rate is to dollars. It compiles, builds, and executes
    > > correctly except for a few bugs. When building I get an error that
    > > states;
    > >
    > > Linker Error (Severity 4)
    > > Module "a" in file "c:\program files\miracle c\yendollarext.obj"
    > > references unresolved external "_rewind"
    > > at offset 0303H in segment "_text".
    > >
    > > I am not sure how to resolve this error.
    > >

    >
    > What compiler are you using? I am not familiar with this error
    > message. Compiler-specific error questions may be better posted to a
    > newsgroup dealing with your compiler.
    >
    > > Also, I attempted to have the program create an error whenever someone
    > > either "chooses" a number higher than 1 "else if(choice > '1')"
    > > or attempts to use an alpha character
    > >
    > > "if (isalpha(yen))
    > >
    > > {
    > >
    > > printf("\n Sorry, that was not a valid option.\n\n");"
    > >
    > > break;
    > >
    > > in which the program shuts down without displaying the error message.
    > > Any ideas? Here is the code, all help is appreciated.
    > >
    > > #include <stdio.h>
    > > #define DRAINO rewind(stdin);fflush(stdout);
    > > #define STOP rewind(stdin); getchar();
    > >
    > >
    > >
    > > int main( void )
    > > {
    > > double yen = 0.0;
    > > double dollars = 0.0;
    > > double yentoDollar = 0.0;
    > >
    > > int choice=0;
    > >
    > >
    > > printf("\nYen to Dollar Currency Conversion\n\n");
    > >
    > > printf("Enter 1 to begin Conversionn\n");
    > >
    > > printf("Enter 0 to Quit the Program\n\n");
    > >
    > >
    > > printf("Please select your option:\n\n");
    > >
    > > scanf("%d", &choice);
    > >
    > >
    > > if (choice == 1)
    > >
    > > {
    > >
    > > printf("\nYou want to convert Yen into US dollars.\n\n");
    > > printf("Please enter the current exchange rate between Dollars and
    > > Yen.\n\n");
    > > scanf( "%lf", &yentoDollar );
    > >

    >
    > I may be wrong, but "%f" should used instead of "%lf" - you are not
    > using a long double.
    >
    > >
    > > if (isalpha(yentoDollar))
    > >
    > > {
    > >
    > > printf("Sorry, that was not a valid option.");
    > > getchar ();
    > > break;
    > >
    > > }

    >
    > isalpha() requires the library <ctype.h>. However, if the user enters
    > a letter, scanf will not put anything into \yentoDollar\ and the
    > condition if(isalpha(yentoDollar)) will always evaluate as false.
    > Assuming you want the program to shut down on invalid input, the
    > following would be a better method for data input and checking
    > (tab-spacing mangled by google groups):
    >
    > char line_Buffer[100];
    > ...
    > fgets(line_Buffer, sizeof(line), stdin);
    > if(sscanf(line_Buffer, "%f", &yentoDollar) != 1) {
    > printf("Sorry, that was not a valid option.\n");
    > return(1);
    > }
    >
    > This will make your program exit if the user does not enter a float or
    > double. You may also want to check if the user entered a negative
    > number, and use a loop to continue prompting the user until valid
    > input has been entered.
    >
    > >
    > > printf("\nHow many Yen do you have?\n\n");
    > > scanf( "%lf", &yen );
    > > if (isalpha(yen))
    > >
    > > {
    > >
    > > printf("\n Sorry, that was not a valid option.\n\n");
    > >
    > > break;
    > >
    > > }

    >
    > Again, isalpha() should not be used, and "%f" should be used instead
    > of "%lf". See above.
    >
    > > dollars = yen * yentoDollar;
    > > printf( "\nThe conversion to US dollars of %.2f yen is %.2f
    > > dollars\n", yen, dollars );
    > >
    > >
    > > rewind(stdin);
    > >

    >
    > Why are you calling rewind()? It seems unnecessary.
    >
    > > getchar();
    > >
    > > }
    > >
    > > else if (choice == 0)
    > >
    > > {
    > > printf("Program will terminate after you click Enter.\n");
    > > printf("\nHave a nice day!");
    > > getchar ();
    > > return 0;
    > >
    > > }
    > >
    > > else if(choice > '1')
    > >

    >
    > Your variable 'choice' is an int, so you do not need the single-quotes
    > around the number 1. Also, your program's behavior is the same if
    > choice == 0 or choice > 1, so the two conditions can be combined into
    > one. Futhermore, you leave open the possibility that \choice\ will be
    > negative. I suggest you modify the condition like so:
    >
    > else if(choice <= 0 || choice > 1) {
    > /* print message and exit */
    > }
    >
    > > {
    > >
    > > printf("\nProgram will terminate after you click Enter.\n");
    > > printf("\nHave a nice day!");
    > > getchar ();
    > > return 0;
    > >
    > > }

    >
    > > }
    > >
    > > Thanks in advance!
    > >
    > > Mike

    >
    > Your welcome! I hope this helps.
    >
    > Best,
    > Michael Hannon
    >


    Hello,

    Thanks, I have changed the program. Do you by chance know how to add a
    Sentinel or how I can make this program re-start without using
    "rewind(stdin);"? I have listed the program below with changes.

    #include <stdio.h>

    int main( void )
    {
    double yen = 0.0;
    double dollars = 0.0;
    double yentoDollar = 0.0;
    int choice=0;


    printf("\nYen to Dollar Currency Conversion\n\n");

    printf("Enter 1 to Begin Conversion\n\n");

    printf("\n(Entering anything other than 1 will Terminate the
    Program!)\n\n");


    printf("Please select your option:\n\n");

    scanf("%d", &choice);


    if (choice == 1)

    {

    printf("\nYou want to convert Yen into US dollars.\n\n");
    printf("Please enter the current exchange rate of Yen to
    Dollars.\n\n");
    scanf( "%lf", &yentoDollar );

    printf("\nHow many Yen do you have?\n\n");
    scanf( "%lf", &yen );
    dollars = yen * yentoDollar;
    printf( "\nThe conversion to US dollars of %.2f yen is %.2f
    dollars\n", yen, dollars );


    rewind(stdin);

    getchar();

    }

    else if(choice <= 0 || choice > 1)

    {
    printf("Program will terminate after you Press Enter.\n");
    printf("\nHave a nice day!");
    getchar();
    exit ();

    }


    }

    Thanks,
    Mike
     
    Mike, Oct 31, 2004
    #8
  9. Mike

    CBFalconer Guest

    Re: Newbie Program That Started Off Easy - Simple Math w/ InteractiveMenu

    Mike wrote:
    > CBFalconer wrote
    >
    >> You can't rewind stdin.

    >
    > Thanks, that was easy to get rid of the error message, but how do I
    > get the program to rewind to the beginning?


    If it's inputting from stdin, you don't.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Oct 31, 2004
    #9
  10. Mike

    Mike Guest

    Re: Newbie Program That Started Off Easy - Simple Math w/ Interactive Menu

    -berlin.de wrote in message news:<>...
    > Mike <> wrote:
    > > CBFalconer wrote

    >
    > > You can't rewind stdin.

    >
    > > Thanks, that was easy to get rid of the error message, but how do I
    > > get the program to rewind to the beginning?

    >
    > I guess by "rewinding to the beginning" you mean that you want to
    > ask the user again what (s)he wants to do after you did a conversion.
    > For that you must put everything in a loop, probably a do or while loop
    > would be most suitable.
    >
    > You could e.g. run your code in a infinite loop like this:
    >
    > while( 1 ) { /* repeats infinitely since 1 is always true */
    >
    > do everything you did before in here
    >
    > }
    >
    > and do either a 'break' from that loop if the user want's to stop
    > or exit() directly in that case.
    >
    > Regards, Jens


    Thanks, that worked!
     
    Mike, Nov 1, 2004
    #10
    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. WJ
    Replies:
    2
    Views:
    493
  2. chirs
    Replies:
    18
    Views:
    776
    Chris Uppal
    Mar 2, 2004
  3. AciD_X
    Replies:
    4
    Views:
    8,113
    Jonathan Turkanis
    Apr 1, 2004
  4. Jake Barnes
    Replies:
    2
    Views:
    311
  5. VK
    Replies:
    15
    Views:
    1,180
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page