Re: Adding a Sentinel

From: Raymond Martineau (bk039_at_freenet.carleton.ca)
Date: 10/31/04


Date: Sun, 31 Oct 2004 13:10:43 -0500

On 31 Oct 2004 08:22:19 -0800, azrent@gmail.com (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.



Relevant Pages

  • Re: Adding a Sentinel
    ... >>input as sentinel. ... > From what I can tell, you aren't using any loop statements. ... printf("\n(Entering anything other than 1 will Terminate the ... dollars = yen * yentoDollar; ...
    (comp.lang.c)
  • Re: MSDN volatile sample
    ... I have stuied your optimization code and I think the ... optimization approach you are using to optimize while loop to for loop is a ... condition if while loop -- in this sample is variable Sentinel may be changed ... if not zero jump to STARTFOR address ...
    (microsoft.public.vc.language)
  • Re: sentinel control loops
    ... When an unknown number of data items are to be processed, ... recognise the end of the data is needed. ... One solution is to define a sentinel value ... end loop; ...
    (comp.lang.c)
  • Re: Sentinels and nested if
    ... >> For example I ask the user for a letter of the alphabet and the output ... >> the sentinel character is used. ... Then my coding is off. ... Now the loop iterates only once then quits even though the sentinel is ...
    (comp.lang.cpp)
  • Re: Parsing challenge to reduce blanks
    ... > string already contained your sentinel character, ... You consider NULL a ascii text character so it cant be used as a masking ... Is there any "sentinel" character that you dont object to using for a ... of the trailing blanks, BFD.. ...
    (comp.lang.fortran)