HELLO.
This is due in my CIS C Programming class soon. I thought it would be no sweat, but I ran into some trouble at the end because I cannot break out of a switch when the switch/case is inside of a while loop. If you have any code suggestions please let me know. I probably went about it wrong. Maybe I should have used a flowchart or something.
ok. whatever.. have a look. thanks.
#include <stdio.h>
void moon(double dEarthWeight);
void mars(double dEarthWeight);
void jupiter(double dEarthWeight);
void err_msg(int error);
int main( void )
{
char cPlanetSelect, cDoLoop = 'y';
double dEarthWeight;
printf("Welcome to ACME Science Center Planetary Weight Computer!\n\n");
do
{
printf("Please enter your weight (in pounds on Earth): "); scanf("%lf", &dEarthWeight
);
if ( dEarthWeight <= -1 )
{
err_msg( 1 );
continue;
}
printf("Enter the planet ( m for Moon, r for Mars or j for Jupiter): ");
while ( ( ( cPlanetSelect
= getchar() ) != 'y' ) && cPlanetSelect != 'Y'
&& cPlanetSelect != EOF )
{
switch ( cPlanetSelect )
{
case 'm':
case 'M':
moon( dEarthWeight );
break;
case 'r':
case 'R':
mars( dEarthWeight );
break;
case 'j':
case 'J':
jupiter( dEarthWeight );
break;
case '\n':
case '\t':
case ' ':
case 'y':
case 'Y':
break;
case 'n':
case 'N':
return ( 0 );
break;
default:
err_msg( 2 );
break;
}
}
/* printf("\nDo you want to check your weight on a different planet? (Y/N): ");
scanf("%c", &cDoLoop);
if ( cDoLoop != 'y' && cDoLoop != 'n')
scanf("%c", &cDoLoop); */
}
while ( cDoLoop != 'n' || cDoLoop == 'N' );
return ( 0 );
}
void moon(double dEarthWeight)
{
printf("\n\nOn the Moon, you weigh %6.2lf lbs\n", dEarthWeight
/ 6 ); printf("\n\nDo you want to check your weight on a different planet? (Y/N): ");
return;
}
void mars(double dEarthWeight)
{
printf("\n\nOn Mars, you weigh %6.2lf lbs\n", dEarthWeight
* 0.39 ); printf("\n\nDo you want to check your weight on a different planet? (Y/N): ");
return;
}
void jupiter(double dEarthWeight)
{
printf("\n\nOn the Jupiter, you weigh %6.2lf lbs\n", dEarthWeight
* 2.33 ); printf("\n\nDo you want to check your weight on a different planet? (Y/N): ");
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"); printf("Do you want to check your weight on a different planet? (Y/N): \n\n"); }
return;
}