Welcome Guest.   Make a donation to an author on the site December 18, 2014, 10:43:36 PM  *

Please login or register.
Or did you miss your validation email?


Login with username and password (forgot your password?)
Why not become a lifetime supporting member of the site with a one-time donation of any amount? Your donation entitles you to a ton of additional benefits, including access to exclusive discounts and downloads, the ability to enter monthly free software drawings, and a single non-expiring license key for all of our programs.


You must sign up here before you can post and access some areas of the site. Registration is totally free and confidential.
 
The N.A.N.Y. Challenge 2013! Download dozens of custom programs!
   
   Forum Home   Thread Marks Chat! Downloads Search Login Register  
Pages: [1]   Go Down
  Reply  |  New Topic  |  Print  
Author Topic: Planetary Weight Computer System ... haha... Still learning C  (Read 4067 times)
stitched
Supporting Member
**
Posts: 7


View Profile Give some DonationCredits to this forum member
« on: March 05, 2009, 05:11:04 AM »

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

Formatted for C with the GeSHI Syntax Highlighter [copy or print]
  1. #include <stdio.h>
  2.  
  3. void moon(double dEarthWeight);
  4. void mars(double dEarthWeight);
  5. void jupiter(double dEarthWeight);
  6. void err_msg(int error);
  7.  
  8. int main( void )
  9. {
  10.  
  11.   char cPlanetSelect, cDoLoop = 'y';
  12.   double dEarthWeight;
  13.  
  14.   printf("Welcome to ACME Science Center Planetary Weight Computer!\n\n");
  15.  
  16.   do
  17.   {
  18.      printf("Please enter your weight (in pounds on Earth): ");
  19.      scanf("%lf", &dEarthWeight);
  20.  
  21.      if ( dEarthWeight <= -1 )
  22.      {
  23.         err_msg( 1 );
  24.         continue;
  25.      }
  26.  
  27.  
  28.  
  29.        printf("Enter the planet ( m for Moon, r for Mars or j for Jupiter): ");
  30.  
  31.        while  ( ( ( cPlanetSelect = getchar() ) != 'y' )
  32.                   && cPlanetSelect != 'Y'
  33.                   && cPlanetSelect != EOF )
  34.         {
  35.  
  36.            switch ( cPlanetSelect )
  37.            {
  38.  
  39.            case 'm':
  40.            case 'M':
  41.  
  42.            moon( dEarthWeight );
  43.            break;
  44.  
  45.            case 'r':
  46.            case 'R':
  47.  
  48.            mars( dEarthWeight );
  49.            break;
  50.  
  51.            case 'j':
  52.            case 'J':
  53.  
  54.            jupiter( dEarthWeight );
  55.            break;
  56.  
  57.  
  58.            case '\n':
  59.            case '\t':
  60.            case ' ':
  61.            case 'y':
  62.            case 'Y':
  63.  
  64.               break;
  65.  
  66.            case 'n':
  67.            case 'N':
  68.  
  69.               return ( 0 );              
  70.  
  71.               break;
  72.  
  73.  
  74.            default:
  75.  
  76.            err_msg( 2 );
  77.  
  78.            break;
  79.  
  80.           }
  81.        }
  82.  
  83. /*      printf("\nDo you want to check your weight on a different planet? (Y/N): ");
  84.       scanf("%c", &cDoLoop);
  85.  
  86.       if ( cDoLoop != 'y' && cDoLoop != 'n')
  87.       scanf("%c", &cDoLoop); */
  88.  
  89.  
  90.   }
  91.   while ( cDoLoop != 'n' || cDoLoop == 'N' );
  92.  
  93.   return ( 0 );
  94. }
  95.  
  96. void moon(double dEarthWeight)
  97. {
  98.   printf("\n\nOn the Moon, you weigh %6.2lf lbs\n", dEarthWeight / 6 );
  99.   printf("\n\nDo you want to check your weight on a different planet? (Y/N): ");
  100.  
  101.   return;
  102.  
  103. }
  104.  
  105. void mars(double dEarthWeight)
  106. {
  107.   printf("\n\nOn Mars, you weigh %6.2lf lbs\n", dEarthWeight * 0.39 );
  108.   printf("\n\nDo you want to check your weight on a different planet? (Y/N): ");
  109.  
  110.   return;
  111.  
  112. }
  113.  
  114. void jupiter(double dEarthWeight)
  115. {
  116.   printf("\n\nOn the Jupiter, you weigh %6.2lf lbs\n", dEarthWeight * 2.33 );
  117.   printf("\n\nDo you want to check your weight on a different planet? (Y/N): ");  
  118.  
  119.   return;
  120. }
  121.  
  122. void err_msg(int error)
  123. {
  124.  
  125.  
  126.   if ( error == 1)
  127.            printf("Your weight must be a positive number\n\n");
  128.   else
  129.   {
  130.            printf("\n\nInvalid Selection!\n");
  131.            printf("To Continue Selecting a Planet, Please Type\n");
  132.            printf("( m for Moon, r for Mars, or j for Jupiter): \n");
  133.            printf("Do you want to check your weight on a different planet? (Y/N): \n\n");            
  134.   }
  135.  
  136.    return;
  137.  
  138. }

* Program3sp2009.pdf (165.11 KB - downloaded 280 times.)
« Last Edit: March 05, 2009, 05:51:31 AM by stitched » Logged
stitched
Supporting Member
**
Posts: 7


View Profile Give some DonationCredits to this forum member
« Reply #1 on: March 05, 2009, 05:27:46 AM »

ooops...
« Last Edit: March 05, 2009, 05:51:56 AM by stitched » Logged
mouser
First Author
Administrator
*****
Posts: 33,770



see users location on a map View Profile WWW Read user's biography. Give some DonationCredits to this forum member
« Reply #2 on: March 05, 2009, 05:45:05 AM »

you can edit your post your make the correction smiley

it's a good start.

first to answer your question about breaking out of a loop: this happens not infrequently.  a common solution is to have a flag before the switch statement like:
bool needtobreak=false;

then you have your switch statement, where if you wanted to break from loop you would simply do:
needtobreak=true;

and then after your switch statement you would have

if (needtobreak)
  break;



Now some more general comments:

You are using some styles that would be considered ok in the early days but are frowned upon these days.
For example, using numeric error codes.  You dont want to use any magical numbers in your coder, it makes it very hard to read as it gets bigger.  A better solution might be to define at the begining of your file:
#define ERROR_BADWEIGHT 1
then in your code:
err_msg(ERROR_BADWEIGHT)
and in void err_msg(int error you wouild have:
if ( error == BADWEIGHT)



you are also repeating a lot of code like with dif functions for each planet.  it would be a good excercise to try to make it more generic, with at the very least a single function like:
PrintWeightOnPlanet(char* planetname, double weightconversionfactor)
Logged
stitched
Supporting Member
**
Posts: 7


View Profile Give some DonationCredits to this forum member
« Reply #3 on: March 05, 2009, 05:59:34 AM »

Thanks for the idea on using #define for constants... I should have known better. tongue

I'll post the updates soon.  Never know when you need to know how much your butt weighs on the Moon!  or Uranus...   tongue
Logged
stitched
Supporting Member
**
Posts: 7


View Profile Give some DonationCredits to this forum member
« Reply #4 on: March 05, 2009, 08:45:42 AM »

It works!

Formatted for C with the GeSHI Syntax Highlighter [copy or print]
  1. /* stitched */
  2.  
  3.  
  4. #include <stdio.h>
  5.  
  6. /* Define Constants to */
  7. /* make error function calls more readable */
  8. #define ERROR_BADWEIGHT     1
  9. #define ERROR_NOT_SELECTION 2
  10.  
  11. /* Define Constants to */
  12. /* make passing logic more understandable */
  13. #define INPUTING_WEIGHT  1
  14. #define INPUTING_PLANET  2
  15. #define END_PROGRAM      3
  16.  
  17. /* Define Function Prototypes */
  18. void moon(double dEarthWeight);
  19. void mars(double dEarthWeight);
  20. void jupiter(double dEarthWeight);
  21. void err_msg(int error);
  22. int exit_program();
  23.  
  24. /* main() function */
  25. int main( void )
  26. {
  27.  
  28.   char cPlanetSelect, cDoMainLoop = 'y';
  29.   int iProcess = INPUTING_WEIGHT;
  30.   double dEarthWeight;
  31.  
  32.   printf("Welcome to ACME Science Center Planetary Weight Computer!\n\n");
  33.  
  34.   do
  35.   {
  36.  
  37.      if ( iProcess == INPUTING_WEIGHT )
  38.      {
  39.  
  40.         printf("Please enter your weight (in pounds on Earth): ");
  41.         scanf("%lf", &dEarthWeight);
  42.  
  43.         if ( dEarthWeight <= -1 )
  44.         {
  45.            err_msg( ERROR_BADWEIGHT );
  46.            continue;
  47.         }
  48.  
  49.         iProcess = INPUTING_PLANET;
  50.         continue;
  51.      }
  52.  
  53.  
  54.      if ( iProcess == INPUTING_PLANET )
  55.      {
  56.  
  57.        printf("Enter the planet ( m for Moon, r for Mars or j for Jupiter): ");
  58.  
  59.        while  ( ( cPlanetSelect = getchar() ) )
  60.         {
  61.  
  62.            switch ( cPlanetSelect )
  63.            {
  64.  
  65.            case 'm':
  66.            case 'M':
  67.  
  68.               moon( dEarthWeight );
  69.               iProcess = exit_program();
  70.            break;
  71.  
  72.            case 'r':
  73.            case 'R':
  74.  
  75.               mars( dEarthWeight );
  76.               iProcess = exit_program();
  77.            break;
  78.  
  79.            case 'j':
  80.            case 'J':
  81.  
  82.               jupiter( dEarthWeight );
  83.               iProcess = exit_program();
  84.            break;
  85.  
  86.            case 'y':
  87.            case 'Y':
  88.               iProcess = INPUTING_WEIGHT;
  89.               break;
  90.  
  91.            case 'n':
  92.            case 'N':
  93.               iProcess = END_PROGRAM;
  94.               return(0);
  95.            break;
  96.  
  97.  
  98.            case '\n':
  99.            case '\t':
  100.            case ' ':
  101.            break;
  102.  
  103.  
  104.            default:
  105.  
  106.            err_msg( ERROR_NOT_SELECTION );
  107.  
  108.            break;
  109.  
  110.           }
  111.  
  112.           if ( iProcess == INPUTING_WEIGHT )
  113.              break;
  114.  
  115.  
  116.        }
  117.      }
  118.  
  119.  
  120.   }
  121.   while ( cDoMainLoop );
  122.  
  123.  
  124.   return(0);
  125. }
  126.  
  127. int exit_program()
  128. {
  129.  
  130.   char cProc = 'Y';
  131.  
  132.   printf("\n\nDo you want to check your weight on a different planet? (Y/N): ");
  133.   cProc = getchar();
  134.  
  135.   if ( cProc == 'Y')
  136.         return (INPUTING_WEIGHT);
  137.   else if
  138.      ( cProc == 'N' )
  139.        return (END_PROGRAM);
  140.  
  141.   return (0);
  142.  
  143. }
  144.  
  145. void moon(double dEarthWeight)
  146. {
  147.   printf("\n\nOn the Moon, you weigh %6.2lf lbs\n", dEarthWeight / 6 );
  148.  
  149.   return;
  150.  
  151. }
  152.  
  153. void mars(double dEarthWeight)
  154. {
  155.   printf("\n\nOn Mars, you weigh %6.2lf lbs\n", dEarthWeight * 0.39 );
  156.  
  157.   return;
  158.  
  159. }
  160.  
  161. void jupiter(double dEarthWeight)
  162. {
  163.   printf("\n\nOn the Jupiter, you weigh %6.2lf lbs\n", dEarthWeight * 2.33 );
  164.  
  165.   return;
  166. }
  167.  
  168. void err_msg(int error)
  169. {
  170.  
  171.  
  172.   if ( error == 1)
  173.            printf("Your weight must be a positive number\n\n");
  174.   else
  175.   {
  176.            printf("\n\nInvalid Selection!\n");
  177.            printf("To Continue Selecting a Planet, Please Type\n");
  178.            printf("( m for Moon, r for Mars, or j for Jupiter): \n");
  179.   }
  180.  
  181.    return;
  182.  
  183. }
« Last Edit: May 31, 2009, 11:39:12 AM by stitched » Logged
Pages: [1]   Go Up
  Reply  |  New Topic  |  Print  
 
Jump to:  
   Forum Home   Thread Marks Chat! Downloads Search Login Register  

DonationCoder.com | About Us
DonationCoder.com Forum | Powered by SMF
[ Page time: 0.091s | Server load: 0.27 ]