topbanner_forum
  *

avatar image

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

Login with username, password and session length
  • Friday March 29, 2024, 5:44 am
  • Proudly celebrating 15+ 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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - stitched [ switch to compact view ]

Pages: [1]
1
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 [Select]
  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. }

Pages: [1]