Re: Adding a Sentinel
From: Raymond Martineau (bk039_at_freenet.carleton.ca)
Date: 10/31/04
- Next message: jacob navia: "Re: Get System Memory in C"
- Previous message: Richard Harter: "Re: dynamic allocation"
- In reply to: Mike: "Adding a Sentinel"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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", ¥toDollar );
>
>
> 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.
- Next message: jacob navia: "Re: Get System Memory in C"
- Previous message: Richard Harter: "Re: dynamic allocation"
- In reply to: Mike: "Adding a Sentinel"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|