/* stitched */
#include <stdio.h>
/* Define Constants to */
/* make error function calls more readable */
#define ERROR_BADWEIGHT 1
#define ERROR_NOT_SELECTION 2
/* Define Constants to */
/* make passing logic more understandable */
#define INPUTING_WEIGHT 1
#define INPUTING_PLANET 2
#define END_PROGRAM 3
/* Define Function Prototypes */
void moon(double dEarthWeight);
void mars(double dEarthWeight);
void jupiter(double dEarthWeight);
void err_msg(int error);
int exit_program();
/* main() function */
int main( void )
{
char cPlanetSelect, cDoMainLoop = 'y';
int iProcess = INPUTING_WEIGHT;
double dEarthWeight;
printf("Welcome to ACME Science Center Planetary Weight Computer!\n\n");
do
{
if ( iProcess == INPUTING_WEIGHT )
{
printf("Please enter your weight (in pounds on Earth): "); scanf("%lf", &dEarthWeight
);
if ( dEarthWeight <= -1 )
{
err_msg( ERROR_BADWEIGHT );
continue;
}
iProcess = INPUTING_PLANET;
continue;
}
if ( iProcess == INPUTING_PLANET )
{
printf("Enter the planet ( m for Moon, r for Mars or j for Jupiter): ");
while ( ( cPlanetSelect
= getchar() ) ) {
switch ( cPlanetSelect )
{
case 'm':
case 'M':
moon( dEarthWeight );
iProcess = exit_program();
break;
case 'r':
case 'R':
mars( dEarthWeight );
iProcess = exit_program();
break;
case 'j':
case 'J':
jupiter( dEarthWeight );
iProcess = exit_program();
break;
case 'y':
case 'Y':
iProcess = INPUTING_WEIGHT;
break;
case 'n':
case 'N':
iProcess = END_PROGRAM;
return(0);
break;
case '\n':
case '\t':
case ' ':
break;
default:
err_msg( ERROR_NOT_SELECTION );
break;
}
if ( iProcess == INPUTING_WEIGHT )
break;
}
}
}
while ( cDoMainLoop );
return(0);
}
int exit_program()
{
char cProc = 'Y';
printf("\n\nDo you want to check your weight on a different planet? (Y/N): ");
if ( cProc == 'Y')
return (INPUTING_WEIGHT);
else if
( cProc == 'N' )
return (END_PROGRAM);
return (0);
}
void moon(double dEarthWeight)
{
printf("\n\nOn the Moon, you weigh %6.2lf lbs\n", dEarthWeight
/ 6 );
return;
}
void mars(double dEarthWeight)
{
printf("\n\nOn Mars, you weigh %6.2lf lbs\n", dEarthWeight
* 0.39 );
return;
}
void jupiter(double dEarthWeight)
{
printf("\n\nOn the Jupiter, you weigh %6.2lf lbs\n", dEarthWeight
* 2.33 );
return;
}
void err_msg(int error)
{
if ( error == 1)
printf("Your weight must be a positive number\n\n"); else
{
printf("\n\nInvalid Selection!\n"); printf("To Continue Selecting a Planet, Please Type\n"); printf("( m for Moon, r for Mars, or j for Jupiter): \n"); }
return;
}