CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2004
    Location
    Texas, earth, Mecca, soul
    Posts
    58

    Red face Logic interruption

    Greetings:
    i have been working on this code for some time now, but I am stuck in a logic cavity: The program is a simple astronomy app which consults a local flatfile database as well as works simple statistics mathematics such as variance, deviation, range, means, etc. The problem is, there are just two initial choices available for the user, but only the first is accesible, while the second exists only in theory, closing the console if anything but the first choice is selected. There are if, else errors I am sure, but after a few days of working just on this coding problem, I am honestly stumped, lost. I have consulted many sites and sources for research, but alas, am no further to an improvement. Truly, I need someone to explain "if, else if, else" use to me once and for all, as I fear it has never been made clear to me in school up to this point.
    (Note: code truncated for forum use \ code compiled in Dev-C++ compiler)

    Code:
        #include <stdio.h>
            #include <conio.h>
            #include <time.h>
            #include <windows.h>
            
            
            int main ()
            {
                
                
                
                int  stats;
                
                char choice;
                
                {
                printf("\nPlease select your area of interest: Galaxy Log OR Celestial Statistics.\n");
                printf("\n  (G)alactic Database \n  (C)elestial Statistics\n");
                printf("\nChoice:");
                choice=getche();
                }
            
                    
                    
                    if ('G' ==choice || 'g'==choice) 
            
                    
                    {
                    int num;
                    
                    printf("\nPlease make a selection from the list below by entering a number from 1-10: ");
                    printf("\n________________________________________\n");
                    printf("\n 1.The Sun         \n ");
                    printf("\n 2.Proxima Centauri\n ");
                    printf("\n 3.Barnard's Star  \n ");
                    printf("\n 4.Wolf 359        \n ");
                    printf("\n 5.Lalande 21185   \n ");
                    printf("\n 6.Sirius          \n ");
                    printf("\n 7.Luyten 726-8 AB \n ");
                    printf("\n 8.Ross 154        \n ");
                    printf("\n 9.Epsilon Eridani \n ");
                    printf("\n 10.Ross 248       \n ");
                    
                    scanf("%d", &num);
                    switch (num) 
                    
                    
                    {
                    
            /*-----------------------------------------*/
                case 1:
                     FILE *stream1;
                     char line1[1000];
                     
                     printf("\n You selected   The Sun ...\n" );
                     printf("\n________________________________________\n");
                     Sleep(1000);
                    if( (stream1 = fopen( "1_Sun.txt", "r" )) != NULL )
                     {
                     if( fgets( line1, 1000, stream1 ) == NULL)
                     printf( "fgets error\n" );
                     else  
                     printf( "%s", line1);
                     fclose( stream1 );
             
             
             
             Sleep(20000);                                   
                      
                     break;
                      
            /*-----------------------------------------*/
                case 2:
                     FILE *stream2;
                     char line2[1000];
                      printf("\n You selected Proxima Centauri ...\n" );
                      
                      printf("\n________________________________________\n");
                     if( (stream2 = fopen( "2_Proxima Centauri.txt", "r" )) != NULL )
                     {
                    if( fgets( line2, 1000, stream2 ) == NULL)
                     printf( "fgets error\n" );
                     else
                     printf( "%s", line2);
                    fclose( stream2 );
             Sleep(20000);                                   
                      
                     break;
                     
            /*-----------------------------------------*/   
             
                
                
                case 3:
                          FILE *stream3;
                     char line3[1000];
                      
                      printf("\n You selected Barnard's Star ...\n" );
                      printf("\n________________________________________\n");
                     if( (stream3 = fopen( "3_Barnard's Star.txt", "r" )) != NULL )
                     {
                    if( fgets( line3, 1000, stream3 ) == NULL)
                     printf( "fgets error\n" );
                     else
                     printf( "%s", line3);
                    fclose( stream3 );
             Sleep(20000);
                    
                    break;
                
                
            /*-----------------------------------------*/     
                case 4:
                    
                          FILE *stream4;
                     char line4[1000];
                      printf("\n You selected Wolf359 ...\n" );
                      
                      printf("\n________________________________________\n");
                     if( (stream4 = fopen( "4_Wolf359.txt", "r" )) != NULL )
                     {
                    if( fgets( line4, 1000, stream4 ) == NULL)
                     printf( "fgets error\n" );
                     else
                     printf( "%s", line4);
                    fclose( stream4 );
             Sleep(20000);
                    
                    break;
                
                
                
            
            
            /*-----------------------------------------*/     
                case 5:
                   
                             FILE *stream5;
                     char line5[1000];
                      printf("\n You selected Lalande 21185 ...\n" );
                      
                      printf("\n________________________________________\n");
                     if( (stream5 = fopen( "5_Lalande 21185.txt", "r" )) != NULL )
                     {
                    if( fgets( line5, 1000, stream5 ) == NULL)
                     printf( "fgets error\n" );
                     else
                     printf( "%s", line5);
                    fclose( stream5 );
             Sleep(20000);
                   
                   
                   
                    
                    
                    
                    break;
                
                
                
            
            
            /*-----------------------------------------*/     
                case 6:
                                  FILE *stream6;
                     char line6[1000];
                      printf("\n You selected Sirius ...\n" );
                      
                      
                      printf("\n________________________________________\n");
                     if( (stream6 = fopen( "6_Sirius.txt", "r" )) != NULL )
                     {
                    if( fgets( line6, 1000, stream6 ) == NULL)
                     printf( "fgets error\n" );
                     else
                     printf( "%s", line6);
                    fclose( stream6 );
             Sleep(20000);
                    
                    
                    
                    
                    break;
                
                
                
            
            
            /*-----------------------------------------*/    
                case 7:
                    
                                  FILE *stream7;
                     char line7[1000];
                      printf("\n You selected Luyten 726-8 ...\n" );
                      
                      printf("\n________________________________________\n");
                     if( (stream7 = fopen( "7_Luyten 726-8.txt", "r" )) != NULL )
                     {
                    if( fgets( line7, 1000, stream7 ) == NULL)
                     printf( "fgets error\n" );
                     else
                     printf( "%s", line7);
                    fclose( stream7 );
             Sleep(20000);
                    
                    
                    
                    break;
                
                
                
            
            
      
                    
                    
                    
                    
                    break;
                
                
                
                
            
            
            /*-----------------------------------------*/    
                default:
                    puts("I'm sorry, but have made an invalid selection.");
                   Sleep(2000);
                  
                  }                 
               
            
              
              
                                                                
                                                       else          
                                                       {
                                                        printf("\n You have chosen Celestial Statistics.\n");
                                                        printf("\n Celestial Statistics is an application\n");
                                                        printf("\n designed to calculate mean distances between bodies, \n");
                                                        printf("\n variations of the distance of stars, and more.    \n"); 
                                                        printf("\n First, please choose your mode of calculation:   \n"); 
                                                        printf("\n  (1)Mode \n  (2)Mean\n  (3)Variance \n  (4)Nothing \n ");
                                                        printf("\nChoice:");
                                                        stats=getche();
                                                           
                                                    
            
            
            
            
              
            
            
            
            
            
            
            Sleep(20000);
            
            
            
            
            
            
            
            
              
                  
            }}}}}}}}}}}}}
    "Nay, you love this present life!"

    Save your keystrokes!

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Logic interruption

    I'm not sure I understand your question. The code looks fine to me.

    Basically, if tests a condition. Control path through the code is "altered" based on that condition.

    Code:
    if (cond1)
    {
      // Execute this block if 'cond1' is true
    }
    else if (cond2)
    {
      // Execute this block if 'cond2' is true AND 'cond1' is false
    }
    else
    {
      // Execute this block if BOTH 'cond1' and 'cond2' are false
    }
    Viggy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured