Solved

how to determine a number ending with 12

Posted on 2004-10-22
192 Views
Last Modified: 2010-04-01
Hi experts,

I have 2 columns as following:

char row_elem[50][50];

row_elem[6]                          row_elem[9]

17896412                              1/12/2004
18889712                              10/12/2004
19996512                              12/5/2004
98765409                              1/5/2004

I want to check row_elem[6], if ends with 12, then add 1 day in row_elem[9]. So the results should be

17896412                              1/13/2004
18889712                              10/13/2004
19996512                              12/6/2004
98765409                              1/5/2004

How can I do that ? Thanks
0
Question by:justinY
    7 Comments
     
    LVL 12

    Expert Comment

    by:stefan73
    Hi justinY,
    if(!memcmp("12",&row_elem[6],2)){
        ...
    }


    Cheers!

    Stefan
    0
     
    LVL 16

    Expert Comment

    by:imladris
    You can check for a number ending in 12 by dividing by 100 and taking the remainder:

    if(row_elem[6]%100==12)

    0
     

    Author Comment

    by:justinY
    Thanks, then how do I add one to the day ?
    0
     
    LVL 16

    Expert Comment

    by:imladris
    Sorry, your row_elem are character; I missed that. A comparison would be better in that case.

    The memcmp that Stefan relevantly proposes is a good test. It requires that the numbers all be the same length.
    0
     

    Author Comment

    by:justinY
    So, if I have

    17896412                          1/13/2004
    78912                               2/13/2004

    then I cannot use memcmp(), right ?
    0
     
    LVL 14

    Accepted Solution

    by:
    If the strings vary in length you can do:

    if (!memcmp("12", &row_elem[6][strlen(row_elem[6])-2], 2)) {

    }

    or

    if (atoi(row_elem[6])%100 == 12) {
    }

    0
     
    LVL 12

    Expert Comment

    by:stefan73
    > how do I add one to the day ?

    Use a time_t variable (which is the number of seconds since 01/01/1970), and add 60*60*24 = 86400. You can then convert it back to a struct tm* by localtime().

    That eliminates lengthy calendar operations, like checking for end-of-month, leap years and such.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Article by: SunnyDark
    This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
    Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

    933 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now