Adding a Sentinel

M

Mike

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
 
R

Raymond Martineau

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.
 
M

Mike

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.


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


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.


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.


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

}


}
 
J

Jens.Toerring

Mike said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top