Adding a Sentinel

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

  1. Mike

    Mike Guest

    I am writing this program that first asks the user to type today's
    exchange rate between U.S. dollars and Japanese yen, then reads U.S.
    dollar value and converts each. I am attemtping to use 0 or negative
    input as sentinel. Any ideas?

    #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,
    Mike
     
    Mike, Oct 31, 2004
    #1
    1. Advertising

  2. On 31 Oct 2004 08:22:19 -0800, (Mike) wrote:

    >I am writing this program that first asks the user to type today's
    >exchange rate between U.S. dollars and Japanese yen, then reads U.S.
    >dollar value and converts each. I am attemtping to use 0 or negative
    >input as sentinel. Any ideas?


    From what I can tell, you aren't using any loop statements. If you want to
    implement looping with a sentinel, you must use either a while, or a
    do-while loop.

    In addition, you are using the scanf function. This is not recommended for
    use in a program containing a sentinel return value, as it's very easy to
    produce an infinite loop. While your program can potentially run for a
    couple of loops before returning to a state which can be controlled by the
    user, it will look like the program is collecting input from an unknown
    void.

    A better solution (even if it is a bit larger) is to collect the input from
    the fgets() function, and filter that through sscanf.

    >
    >#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");


    First off, indentation. It's much harder to check what's wrong if the
    indentation is non-standard.

    >
    >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))


    The isalpha() function will not function as you intend it to, as it's
    parameter expects a character value instead of a numerical value. This is a
    very important distinction in C, as datatypes are not always compatable as
    they were in some other applications.

    >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')


    You are comparing the integer value of choice with the character value of
    '1'. Given that these are two different datatypes, I'm not sure you want
    to do this.

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


    Given that these two conditions perform the same result, you should combine
    them into one conditional to reduce redundant code. It will also look
    neater as well.
     
    Raymond Martineau, Oct 31, 2004
    #2
    1. Advertising

  3. Mike

    Mike Guest

    (Raymond Martineau) wrote in message news:<>...
    > On 31 Oct 2004 08:22:19 -0800, (Mike) wrote:
    >
    > >I am writing this program that first asks the user to type today's
    > >exchange rate between U.S. dollars and Japanese yen, then reads U.S.
    > >dollar value and converts each. I am attemtping to use 0 or negative
    > >input as sentinel. Any ideas?

    >
    > From what I can tell, you aren't using any loop statements. If you want to
    > implement looping with a sentinel, you must use either a while, or a
    > do-while loop.
    >
    > In addition, you are using the scanf function. This is not recommended for
    > use in a program containing a sentinel return value, as it's very easy to
    > produce an infinite loop. While your program can potentially run for a
    > couple of loops before returning to a state which can be controlled by the
    > user, it will look like the program is collecting input from an unknown
    > void.
    >
    > A better solution (even if it is a bit larger) is to collect the input from
    > the fgets() function, and filter that through sscanf.
    >
    > >
    > >#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");

    >
    > First off, indentation. It's much harder to check what's wrong if the
    > indentation is non-standard.
    >
    > >
    > >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))

    >
    > The isalpha() function will not function as you intend it to, as it's
    > parameter expects a character value instead of a numerical value. This is a
    > very important distinction in C, as datatypes are not always compatable as
    > they were in some other applications.
    >
    > >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')

    >
    > You are comparing the integer value of choice with the character value of
    > '1'. Given that these are two different datatypes, I'm not sure you want
    > to do this.
    >
    > >
    > >{
    > >
    > >printf("\nProgram will terminate after you click Enter.\n");
    > >printf("\nHave a nice day!");
    > >getchar ();
    > >return 0;

    >
    > Given that these two conditions perform the same result, you should combine
    > them into one conditional to reduce redundant code. It will also look
    > neater as well.


    First off, thanks for the help. This was my first time using a forum
    and it has been a great experience. My assignment is due tonight, I
    added a loop and changed the redundant code. This works, but did I add
    a Sentinel? I am not sure what this Sentinel is yet.

    #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);
    }

    while (choice == 1)

    {
    printf("\nYou want to convert Japanese 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( "\nConverted Japanese Yen to US dollars.\n\n");
    printf("%.2f Japenese Yen equals $%.2f US dollars.\n\n", yen,
    dollars);


    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 <= 0 || choice > 1)

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

    }


    }
     
    Mike, Nov 1, 2004
    #3
  4. Mike

    -berlin.de Guest

    Mike <> wrote:
    > My assignment is due tonight, I
    > added a loop and changed the redundant code. This works, but did I add
    > a Sentinel? I am not sure what this Sentinel is yet.


    Neither am I. The term "sentinel" doesn't seem to make too much
    sense in the context of the program you're trying to write. Perhaps
    it would become clearer if you would post the relevant part of the
    text of your assignment.
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, Nov 1, 2004
    #4
    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. d.j.

    sentinel

    d.j., Apr 22, 2004, in forum: C++
    Replies:
    3
    Views:
    1,815
    John Harrison
    Apr 22, 2004
  2. rzed
    Replies:
    2
    Views:
    291
  3. Will McGugan

    iter(callable, sentinel)

    Will McGugan, Jul 27, 2006, in forum: Python
    Replies:
    3
    Views:
    454
    Simon Forman
    Jul 27, 2006
  4. Ben Finney

    Sentinel values for special cases

    Ben Finney, Oct 27, 2006, in forum: Python
    Replies:
    7
    Views:
    344
    Gabriel Genellina
    Oct 28, 2006
  5. shan

    sentinel control loops

    shan, Oct 20, 2005, in forum: C Programming
    Replies:
    13
    Views:
    916
    Mark McIntyre
    Oct 24, 2005
Loading...

Share This Page