473,513 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding a Sentinel

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
Nov 14 '05 #1
3 2335
On 31 Oct 2004 08:22:19 -0800, az****@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.
Nov 14 '05 #2
bk***@freenet.carleton.ca (Raymond Martineau) wrote in message news:<ej********************************@4ax.com>. ..
On 31 Oct 2004 08:22:19 -0800, az****@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.


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

}
}
Nov 14 '05 #3
Mike <az****@gmail.com> 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 ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1687
by: rzed | last post by:
I am working with PythonCard in one of my apps. For its purposes, it uses an .ini file that is passed to ConfigParser. For my app, I also need configuration information, but for various reasons,...
3
2345
by: d.j. | last post by:
This program will not let me use the charater as the sentinel please advise... The error I get is can not convert char to char* # include <iostream.h> int main() { char stname ; //student...
13
16508
by: shan | last post by:
Hi to everybody here is my simple doubt What is meant by sentinel control loops ?
47
3309
by: Pierre Barbier de Reuille | last post by:
Please, note that I am entirely open for every points on this proposal (which I do not dare yet to call PEP). Abstract ======== This proposal suggests to add symbols into Python. Symbols...
3
2851
by: Will McGugan | last post by:
Hi, I've been using Python for years, but I recently encountered something in the docs I wasnt familar with. That is, using two arguements for iter(). Could someone elaborate on the docs and...
7
3715
by: Ben Finney | last post by:
Howdy all, Ned Batchelder blogged about a debate over checking function parameters, and to what extent dynamic typing should be relied upon. I was one of many who commented, but I wrote what...
1
3484
by: knviesky | last post by:
"Write a program that will read in integers from the keyboard and decide if the numbers are all the same or not. Use sentinel logic with a 9999 as the sentinel value. Use a flag. Note that you do NOT...
3
2954
by: kcdpas | last post by:
I am new to C#. Is any one know how to add a series numbers via Console.Write to an open ended array? I think a while loop could accomplish that when sentinel value entered but having trouble to do...
1
7271
by: Sergiu Ignat | last post by:
Hello, I try to deploy an application through ClickOnce. I use .NET Framework 3.5SP1. In Publish/Prerequisites I require the entire framework (not only the client profile) and checked "Download...
0
7260
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7160
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7099
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5685
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5086
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4746
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3233
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.