Adding a Sentinel

From: Mike (azrent_at_gmail.com)
Date: 10/31/04


Date: 31 Oct 2004 08:22:19 -0800

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



Relevant Pages