Home | Blog | Software | Reviews and Features | Forum | Help | Donate | About us
topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • September 02, 2015, 09:51:40 AM
  • Proudly celebrating 10 years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Planetary Weight Computer System ... haha... Still learning C  (Read 4405 times)

stitched

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7
    • View Profile
    • Donate to Member
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.   :D

Code: C
  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. }
  139.  
« Last Edit: March 05, 2009, 05:51:31 AM by stitched »

stitched

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7
    • View Profile
    • Donate to Member
Re: Planetary Weight Computer System ... haha... Still learning C
« Reply #1 on: March 05, 2009, 05:27:46 AM »
ooops...
« Last Edit: March 05, 2009, 05:51:56 AM by stitched »

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 34,981
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Donate to Member
Re: Planetary Weight Computer System ... haha... Still learning C
« Reply #2 on: March 05, 2009, 05:45:05 AM »
you can edit your post your make the correction :)

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)

stitched

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7
    • View Profile
    • Donate to Member
Re: Planetary Weight Computer System ... haha... Still learning C
« Reply #3 on: March 05, 2009, 05:59:34 AM »
Thanks for the idea on using #define for constants... I should have known better. :P

I'll post the updates soon.  Never know when you need to know how much your butt weighs on the Moon!  or Uranus...   :P

stitched

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7
    • View Profile
    • Donate to Member
Re: Planetary Weight Computer System ... haha... Still learning C
« Reply #4 on: March 05, 2009, 08:45:42 AM »
It works!

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