Thread: C Ranges

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    7

    Unhappy C Ranges

    Hope some one can help: I am new to programming and need to learn how to set up a range. Hope I am wording this right but, I am working on a project that requests user input(an integer) now after the input I would like to search a range of integers and if the user input is in that "range" conduct a "printf". If the integer is not in the range then conduct another "printf"...etc.

    Example:

    char X[]="Nebraska";
    char XX[]="Texas";
    char XXX[]="California";
    char XXXX[]="Arkansas";
    int ask;

    range=(1-100) //This is that range I need//

    int main()

    {
    scanf( "%i", &ask);

    if ("%i", ask) = (1-25) //This is like the comparison I need to do//
    printf("Nebraska\n");

    if ("%i", ask) = (26-50) //" "//
    printf("Texas\n");

    if ("%i", ask) = (51-75) //" "//
    printf("California\n");

    if("%i", ask) = (76-100) //" "//
    printf("Arkansas\n");

    if ("%i", ask) > 100
    printf("This is out of range\n");

    return 0;
    }

    Sorry in advance if I didn't explain my problem correctly but, this should give
    you an idea of what I am trying to accomplish. Plus I am 2 weeks into
    C programming (Well trying to learn all I can off the internet at least).

    ~EvoTone~

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Like this -

    Code:
    scanf( "%d", &ask);
    
    if ( ask>0 && ask <=25 )
    {
      whatever
    }

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    7
    Thanks for the help but, I tryed it like you wrote it and I get

    *syntax error of the && inputs

    Any other help would be appreciated.

    ~EvoTone~

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by EvoTone
    Thanks for the help but, I tryed it like you wrote it and I get

    *syntax error of the && inputs

    Any other help would be appreciated.

    ~EvoTone~
    It's always helpful if you paste your *exact* relevant code, to show us. If you typed it correctly, then try making the following change:

    Code:
    scanf( "%d", &ask);
    
    if ( (ask>0) && (ask <=25) )
    {
      whatever
    }
    The C language has some quirks in it's order of operations. It's always a good idea to wrap any quantity (term), that must be evaluated, in parenthesis, to ensure that the order of operations on that quantity or term, is evaluated in the order you expect.

    Try the above, and let us know, OK?

    Adak

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Adak
    The C language has some quirks in it's order of operations.
    It may, but there not for that example.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if(JULIAN>335 && <=365)
    Read that outloud. If Julian is greater than 335 AND ______ is less than or equal to 365 ...

    You also don't even have to have half of those checks in.
    Code:
    if( x < 0 )
        error, do something...
    else
    if( x < 10 )
        between 0 and 9
    else
    if( x < 20 )
        between 10 and 19
    ...
    But it's up to you.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > If there is another way to make this operation work please take the time to explain it,
    Do you know about structures yet?

    > Instead of using 12 if statements in a row, why not make it one switch statement?
    Because standard C doesn't have ranges on cases.
    365 lines of switch/case is hardly clear code.

    Oh, and the cascasded indent on your if - not pretty.
    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.

  8. #8
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Or you could try ...

    Code:
    /****************************
    *C Program Code::Blocks v1.0*
    *Written by: EvoTone        *
    *Date: Sept., 2006          *
    *****************************/
    #include <stdio.h>
    
    typedef struct
    {
        const int lower;
        const int upper;
        const char * month;
    } DATE_RANGE;
    
    static const DATE_RANGE date_range[]
    {
        {   0,  31, "January"   },
        {  31,  59, "February"  },
        {  59,  91, "March"     },
        {  91, 121, "April"     },
        { 121, 152, "May"       },
        { 152, 182, "June"      },
        { 182, 213, "July"      },
        { 213, 244, "August"    },
        { 244, 274, "September" },
        { 274, 305, "October"   },
        { 305, 335, "November"  },
        { 335, 365, "December"  }
    };
    
    int main(void)
    {
        int i;
        int julian; /*input value from user, needing conversion.*/
    
        /*Intro. & Instructions*/
        printf("This is a Julian to Calendar Date conversion program\n");
        printf("you may input any Julian date and the corresponding\n");
        printf("Calendar Date will be calculated for you\n\n");
    
                                 /*Input Section*/
        printf("Please enter the Julian date: (whole number format)\n");
        scanf("%i", &julian);
        printf("Well silly we know it's Year: 2006!\n\n");
    
    
                                /*Calculation Section*/
        for (i = 0; i < (sizeof(date_range) / sizeof(date_range[0])); i++)
        {
            if ((julian > date_range[i].lower) && (julian <= date_range[i].upper))
            {
                printf("And we know it's: %s\n", date_range[i].month);
                break;
            }
        }
    
        if( i == ( sizeof(date_range) / sizeof(date_range[0]) ) )
            printf("Don't mess with me stupid!!...Julian Dates are only 1-365!\n");
    
        return (0);
    
    }
    Just a thought ...

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    7

    SKeane

    Code:
    /* ***************************
    *C Program Code::Blocks v1.0         *
    *Written by: EvoTone                       *
    *Date: Sept., 2006                           *
    **************************** */
    #include <stdio.h>
    
    typedef struct /*This structs DATE_RANGE w/ 3 constants num/num/char*/
    {
        const int lower; /*Defines a number as lower*/
        const int upper; /*Defines a number as "upper"*/
        const char * month; /*Defines month names*/
    } DATE_RANGE; /*Name of the struct*/
    
    static const DATE_RANGE date_range[]/*Defines struct as array,date_range[]*/
    {
        {   0,  31, "January"   },   /*Defines lower/upper/month variables in struct*/
        {  31,  59, "February"  }, /*"           "*/
        {  59,  91, "March"     },   /*"           "*/ 
        {  91, 121, "April"     },     /*"           "*/ 
        { 121, 152, "May"       },   /*"           "*/ 
        { 152, 182, "June"      },    /*"           "*/ 
        { 182, 213, "July"      },      /*"           "*/ 
        { 213, 244, "August"    },    /*"           "*/ 
        { 244, 274, "September" },  /*"           "*/ 
        { 274, 305, "October"   },     /*"           "*/ 
        { 305, 335, "November"  },   /*"           "*/ 
        { 335, 365, "December"  }     /*"           "*/ 
    };
    
    int main(void)
    {
        int i;
        int julian; /*input value from user, needing conversion.*/
    
        /*Intro. & Instructions*/
        printf("This is a Julian to Calendar Date conversion program\n");
        printf("you may input any Julian date and the corresponding\n");
        printf("Calendar Date will be calculated for you\n\n");
    
                                 /*Input Section*/
        printf("Please enter the Julian date: (whole number format)\n");
        scanf("%i", &julian);
        printf("Well silly we know it's Year: 2006!\n\n");
    
    
                                /*Calculation Section*/
        for (i = 0; i < (sizeof(date_range) / sizeof(date_range[0])); i++) /*Loop*/
        {
            if ((julian > date_range[i].lower) && (julian <= date_range[i].upper))
            {
                printf("And we know it's: %s\n", date_range[i].month);
                break;
            }
        }
    
        if( i == ( sizeof(date_range) / sizeof(date_range[0]) ) )
            printf("Don't mess with me stupid!!...Julian Dates are only 1-365!\n");
    
        return (0);
    
    }
    I think I'm getting what this is supposed to do but, there is syntax errors at line 15 and 48 that I can't figure out. I hadn't previously fooled with "struct" or setting an "array" in this manner so, even after reading up on it, I can't fix it. Little help please. I have added notes to places, telling the thought process I am taking in my attempt to explaining this.
    ~EvoTone~
    Last edited by Ken Fitlike; 09-26-2006 at 02:38 PM. Reason: fixed [code][/code] tags

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    7

    Talking Thanks!!

    Thanks dwks
    static const DATE_RANGE date_range[] =
    {
    That fixed the two errors.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Have you been programming in Java?
    Code:
    static const DATE_RANGE date_range[]
    {
    ->
    Code:
    static const DATE_RANGE date_range[] =
    {
    That's pretty much how I'd do it, too, except as Quzah said, if you order the dates correctly, you only have to store their upper bounds.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    For some reason in the cut'n'paste operation the "=" went missing. I did actually compile, run and test the code (honest).

    Of course you aren't dealing with leap years, so your program will only work approximately 3/4 of the time. This year's OK, next year's OK, then in 2008 it will break.
    Last edited by SKeane; 09-27-2006 at 04:13 AM.

  13. #13
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    Completely untested, but even that complete code has redundancy in it. Take a look at this:

    Code:
    /* ***************************
    *C Program Code::Blocks v1.0         *
    *Written by: EvoTone                       *
    *Date: Sept., 2006                           *
    **************************** */
    #include <stdio.h>
    
    typedef struct /*This structs DATE_RANGE w/ 2 constants num, char */
    {
        const int lower; /*Defines a number as lower*/
        const char * month; /*Defines month names*/
    } DATE_RANGE; /*Name of the struct*/
    
    static const DATE_RANGE date_range[]/*Defines struct as array,date_range[]*/
    {
        {   0,  "January"   },   /*Defines lower/upper/month variables in struct*/
        {  31,  "February"  }, /*"           "*/
        {  59,  "March"     },   /*"           "*/ 
        {  91, "April"     },     /*"           "*/ 
        { 121, "May"       },   /*"           "*/ 
        { 152, "June"      },    /*"           "*/ 
        { 182, "July"      },      /*"           "*/ 
        { 213, "August"    },    /*"           "*/ 
        { 244, "September" },  /*"           "*/ 
        { 274, "October"   },     /*"           "*/ 
        { 305, "November"  },   /*"           "*/ 
        { 335, "December"  }     /*"           "*/ 
    };
    
    int main(void)
    {
        int i;
        int julian; /*input value from user, needing conversion.*/
    
        /*Intro. & Instructions*/
        printf("This is a Julian to Calendar Date conversion program\n");
        printf("you may input any Julian date and the corresponding\n");
        printf("Calendar Date will be calculated for you\n\n");
    
                                 /*Input Section*/
        printf("Please enter the Julian date: (whole number format)\n");
        scanf("%i", &julian);
        printf("Well silly we know it's Year: 2006!\n\n");
    
    
                                /*Calculation Section*/
        for (i = 0; i < (sizeof(date_range) / sizeof(date_range[0])); i++) /*Loop*/
        {
            if (julian > date_range[i].lower)
            {
                printf("And we know it's: %s\n", date_range[i].month);
                break;
            }
        }
    
        if (i  > 365 )
            printf("Don't mess with me stupid!!...Julian Dates are only 1-365!\n");
    
        return (0);
    
    }
    You only need to test for either the upper or the lower bound, and as the loop breaks out afterwards it will not continue.

    As has been pointed out, modifications to the hardcoded values are required to cater for leap years (bearing in mind that altough 2000 was fortunately a leap year, 2100 will not be).
    I think you can put a signature here.

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    7

    Thumbs up To all who lent a hand..Thank You!!

    You only need to test for either the upper or the lower bound, and as the loop breaks out afterwards it will not continue.
    You are right I found that I can test for the upper value but, the lower doesn't seem to work due to the fact that "January" is displayed everytime julian is greater than i.

    Thanks guys!! I have learned a lot.... I am still to unexperienced to worry about leap years yet but, I have enough with the code still needing to define the days of the month compared to the days of a calendar year. After all I am trying to convert a "Julian Date" into a 200x/January/2 format. I'm sure as I head into this next phase of the project I will have more questions. For now thanks to all who helped out.

    ~EvoTone~

    __________________________________________________ _____________

    If you wake up every morning and you can think of nothing else but, programming. You are a programmer!

  15. #15
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    When you put back in the missing "=" after static const DATE_RANGE date_range[] it will compile, but it doesn't actually work, as all values of "julian" (bar zero) give the answer january!

    Nice try though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with matrix size declaration
    By angelica in forum C++ Programming
    Replies: 13
    Last Post: 04-11-2008, 09:03 AM
  2. Asserts and their ranges
    By Matamoros123 in forum C++ Programming
    Replies: 8
    Last Post: 10-07-2006, 12:32 PM
  3. iterators cursors and ranges in STL
    By superchritch in forum C++ Programming
    Replies: 0
    Last Post: 09-20-2005, 09:03 AM
  4. data type ranges, what is the largest?
    By timberwolf5480 in forum C++ Programming
    Replies: 9
    Last Post: 10-16-2003, 05:47 PM
  5. vector<double> ranges
    By correlcj in forum C++ Programming
    Replies: 3
    Last Post: 11-03-2002, 12:49 PM