Hey there! Sign in to join this conversationNew here? Join for free
    • Thread Starter
    Offline

    16
    ReputationRep:
    Hi all,

    I have a character I want to convert into an integer variable so I can more easily modify it further on in the program.

    The snippet of code SO FAR is provided below to make it more understandable:

    char date1[20];
    char day1[20];

    (User enters a date in the form DD/MM/YY, and is stored into date1)

    strncpy(day1, date1, 2); // takes first 2 characters of the date1 contents and stores it to a new variable called day1
    day1[2] = '\0'; // (appends a null character to the end to prevent random errors)

    Example case:

    User enters in 05/04/03 as date1.
    Program then will take "05" and store it in char variable day1.

    However I now want to convert the "05" stored as a character to the integer 5. How would I go about doing this? I would like to do it WITHOUT needing another variable, as I already have too many variables.

    I know its hardly the best solution - maybe there is a better way - removing the /'s from date1, and storing the integers directly from there in snippets?

    Thanks
    Offline

    0
    ReputationRep:
    Look into the standard library function atoi().
    • Community Assistant
    • PS Helper
    Offline

    15
    ReputationRep:
    Community Assistant
    PS Helper
    (Original post by Benjy100)
    No no you must look into the Lagrange Multiplier Algorithmic Matrix function M:<beta - $10391$> and reroute the network triangulating device so that you can access the desired information and make the desired changes without any of the files corrupting.
    Ha. I don't use C but I assume if it's anything like java you could do something like int(date1).

    Edit: I found it quite funny to be honest.
    • Thread Starter
    Offline

    16
    ReputationRep:
    (Original post by mikecsh)
    Look into the standard library function atoi().
    Have already looked into that, but can't seem to make it work?
    • Thread Starter
    Offline

    16
    ReputationRep:
    (Original post by k9markiii)
    Ha. I don't use C but I assume if it's anything like java you could do something like int(date1).
    AFAIK that just grabs the ASCII (int) value of date1.
    • Thread Starter
    Offline

    16
    ReputationRep:
    (Original post by Benjy100)
    (Original post by Benjy100)
    No no you must look into the Lagrange Multiplier Algorithmic Matrix function M:<beta - $10391$> and reroute the network triangulating device so that you can access the desired information and make the desired changes without any of the files corrupting.
    Yes yes I am presuming the 'Ha' is addressed to me. Twas' all in good fun, and so now I will bid you all farewell and delete my post. If you would be so kind as to delete your quote of me from your post I would be grateful.

    Good day to all.
    Your posts are extremely insightful, and contribute much needed information to the discussion at hand. Hopefully people on your own threads will be just as nice and return the favour to you.
    Offline

    0
    ReputationRep:
    (Original post by Sgt.Incontro)
    Have already looked into that, but can't seem to make it work?
    Post your code, what you expect to happen and what you are observing..
    Offline

    0
    ReputationRep:
    (Original post by mikecsh)
    Post your code, what you expect to happen and what you are observing..
    First, try this:

    char string[] = {'0','9'};

    printf("atoi = %i", atoi(string));

    It should print 'atoi = 9'.

    In your program, you want to convert the string/char-array day1 to an integer, so you should change it to:

    printf("atoi = %i", atoi(day1));

    If you are not getting the output you expect, it implies that day1 does not contain the data you expect, so you should proceed to debug that part of your program by inspecting the contents of the array..
    • Thread Starter
    Offline

    16
    ReputationRep:
    (Original post by mikecsh)
    Post your code, what you expect to happen and what you are observing..
    Cannot post my full code publicly for various reasons.

    I have PMed it to you.

    EDIT: I realise that this is very inefficient, so I am wondering if there is any better way to implement this sort of logic. Wondering what your opinion will be.
    Offline

    0
    ReputationRep:
    Replied to your PM.

    I presume that this is a learning exercise for you so what you're doing is a valid first approximation. You don't need to pre-allocate so much memory for your arrays though, as you know you will never use more than 3 chars in each. In general though, the best way to handle ANY date manipulation is by using a battle tested library. Dates and times are one of the most misunderstood topics in commercial programming. My advice is to use your exercises as valuable learning experiences, but if you're ever in the situation where you're writing software "for real", read enough about date calculations to realise that it is _hard_ and that you _will_ screw it up if you do it yourself, then chose an appropriate library*.

    If you ignore the fact that this blog post focusses on Objective-C, you will get an overview for how hard dates are: http://realmacsoftware.com/blog/work...-date-and-time

    * unless you are the poor sod tasked with implementing said library...
    • Thread Starter
    Offline

    16
    ReputationRep:
    (Original post by mikecsh)
    Replied to your PM.

    I presume that this is a learning exercise for you so what you're doing is a valid first approximation. You don't need to pre-allocate so much memory for your arrays though, as you know you will never use more than 3 chars in each. In general though, the best way to handle ANY date manipulation is by using a battle tested library. Dates and times are one of the most misunderstood topics in commercial programming. My advice is to use your exercises as valuable learning experiences, but if you're ever in the situation where you're writing software "for real", read enough about date calculations to realise that it is _hard_ and that you _will_ screw it up if you do it yourself, then chose an appropriate library*.

    If you ignore the fact that this blog post focusses on Objective-C, you will get an overview for how hard dates are: http://realmacsoftware.com/blog/work...-date-and-time

    * unless you are the poor sod tasked with implementing said library...
    Thanks so much for your reply.

    However, I have a further question:

    Since this is an assignment, some of the mark will be towards efficiency I am guessing. So I am thinking about scrapping said program, as it is ~40 lines just to store & convert the variables into a usable format.

    I was thinking something along these lines:

    & using a numerical array??

    Name:  Screenshot from 2013-03-26 17:00:46.jpg
Views: 55
Size:  69.6 KB

    EDIT: No longer need any assistance *at the moment*, and no longer need the answer to the question I asked above. Thank you so much to all those who helped.
 
 
 
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • Poll
    Brussels sprouts
    Useful resources
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

    Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

    Quick reply
    Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.