Shortening statements

This is a discussion on Shortening statements within the C Programming forums, part of the General Programming Boards category; I have this if statement Code: if (strcmp(command, "p") == 0 || strcmp(command, "+") == 0 || strcmp(command, "-") == ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174

    Shortening statements

    I have this if statement

    Code:
          if (strcmp(command, "p") == 0
                        || strcmp(command, "+") == 0
                        || strcmp(command, "-") == 0
                        || strcmp(command, "\n") == 0
                        || strcmp(command, ".") == 0
                        || (stringIsNum(command) == YES
                            && stringToNum(command) > 0)) {
                lineNumber = printCommand(head, command, lineNumber);
            
            }
    Which is clearly getting a little ridiculous, but I'm also going to have to add a few more strcmp's later, and on top of that, I need to be able to accept a letter and its capital. Is there a way to shorten this while still making it clear, or is the way it is now the best approach?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,439
    For testing single letters, the first 5 could be reduced to
    if ( strchr( "p+-\n.", command[0]) != NULL

    > I need to be able to accept a letter and its capital.
    Use toupper() or tolower() right at the start, if in general you don't care about case.

    But if later on, you need "a" and "A" to be separate commands, then you might like to enforce that right at the start by saying "Not recognised P" when they should have typed p.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    Quote Originally Posted by Salem View Post
    For testing single letters, the first 5 could be reduced to
    if ( strchr( "p+-\n.", command[0]) != NULL

    > I need to be able to accept a letter and its capital.
    Use toupper() or tolower() right at the start, if in general you don't care about case.

    But if later on, you need "a" and "A" to be separate commands, then you might like to enforce that right at the start by saying "Not recognised P" when they should have typed p.
    I just implemented that and it worked just as I hoped, except for one thing - it still goes into the if statement if I enter input such as px, which it shouldn't.
    To fix this I'd add

    Code:
    if ( strchr( "p+-\n.", command[0]) != NULL && command[1] == '\0')
    but I also need to accept numbers which have multiple digits into the loop.

    EDIT:

    Actually, all it took was some simple Boolean logic.

    Code:
    if ((strchr( "p+-\n.", command[0]) != NULL
                        && command[1] == '\0')
                        || (stringIsNum(command) == YES
                        && stringToNum(command) > 0))
    Thanks for your help Salem
    Last edited by Mentallic; 10-13-2012 at 04:31 AM.

  4. #4
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    Replace this code

    Code:
    if (strcmp(command, "p") == 0
                  || strcmp(command, "+") == 0
                  || strcmp(command, "-") == 0
                  || strcmp(command, "\n") == 0
                  || strcmp(command, ".") == 0
                  || (stringIsNum(command) == YES
                      && stringToNum(command) > 0)) {
          lineNumber = printCommand(head, command, lineNumber);
    }
    with this

    Code:
    if (textisvalidcommand(command)) {
        lineNumber = printCommand(head, command, lineNumber);
    }
    and, of course, write the proper textisvalidcommand() function.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    Good idea, will do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. shortening this short code? : binary to int casting
    By OldGit in forum C Programming
    Replies: 21
    Last Post: 02-25-2009, 11:10 AM
  2. Shortening long if statements
    By Extropian in forum C Programming
    Replies: 13
    Last Post: 08-05-2005, 02:24 AM
  3. Shortening the code
    By vasanth in forum C Programming
    Replies: 10
    Last Post: 08-28-2004, 12:22 AM
  4. Here's my code, any ways in shortening it?
    By TheLoneWolf32 in forum C++ Programming
    Replies: 5
    Last Post: 01-21-2003, 12:53 AM
  5. Shortening main
    By pdstatha in forum C Programming
    Replies: 1
    Last Post: 04-03-2002, 03:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21