Thread: Need help with testing a condition

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    77

    Need help with testing a condition

    Hello to all,

    For the following code, I want to add a test so that if the user inputs anything that isn't an integer , he/she will receive an error message as opposed to a segmentation fault error which is happening now.

    Here is the code:

    Code:
    else if ((strcmp ( command, "show")) == 0 )
                        {
                            list_num = atoi(strtok(NULL, " \n\t")) ;
                                                                          
                            int x ;
                            int line_count = 1 ;
                                                 
                           for(x = 0; x<strlen(loadedSequences[list_num-1].data) ; ++x) // print 60 letters per line.
                                {   
                                    if(x==0)
                                        {
                                            printf("%10d",line_count) ;
                                        }
                                    if((x%10 == 0))
                                        {
                                            printf(" ") ;
                                        }
                                            
                                    if((x%60 == 0) && (x != 0))
                                        {
                                            printf("\n%10d ",(line_count+x)) ;
                                        }
                                     printf("%c", loadedSequences[list_num-1].data[x]) ;
                                     
                                }
                            
                            printf("\n") ;                       
                        }
    The code works fine otherwise. Any suggestions would be great.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What's wrong with
    Code:
    if (list_num != 0)
      /* print things */
    else
      /* print error */
    ?

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    77
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Condition variables
    By sethjackson in forum Windows Programming
    Replies: 16
    Last Post: 03-19-2008, 11:42 AM
  2. Looping condition
    By Chaplin27 in forum C++ Programming
    Replies: 3
    Last Post: 05-29-2005, 02:06 PM
  3. Condition testing question
    By Aerie in forum C Programming
    Replies: 21
    Last Post: 12-13-2004, 06:54 PM
  4. Race condition
    By Roaring_Tiger in forum C Programming
    Replies: 5
    Last Post: 10-24-2004, 09:42 PM
  5. C++ bit testing
    By Vicious in forum C++ Programming
    Replies: 3
    Last Post: 09-19-2004, 11:44 AM