![]() |
incrementing character array elements value
hi everyone,
it's been quite a while since the last time i worked in c++. i have a question for ya.. lets say that i have a character array that i will input a date in, in this format DD/MM/YYYY. now, since this all is a character array, how can i make the program to increment only the days? also, could someone explain how does this work. if it was an integer type of variable, i understand, but how does it increment numeral values in a character array? |
If you want to increment a numeric value which is being presented as text, you need to convert the text to the appropriate numeric value, increment it, then convert it back to a textual representation. To restrict the operation to days, only, take the first two characters as a unit, convert to numeric, increment, convert back.
You might wish to investigate the various capabilities offered by the libraries. There are a number of date/time manipulations that one can do. |
you know, this actually crossed my mind, the converting the text to the numeric value thing. now i'm wondering if there's a really simple way to do this that i just can't remember, or is there a way to copy the numeric value elements from the character array into an integer array?
|
An alternative: store the values in a structure of integer types. Obviously, you will need to put some checks and balances in here... this is just to demonstrate the concept...
:
#include <iostream> |
yep, this does the trick aswell, thanks, this will be very helpful.
but i'm still curious on how to copy elements from one type of variable to another and then bring em back. thanx |
Conversion is required and there are a number of conversion functions in the libraries. One can also roll their own. For instance, the ASCII code for the number 1 is 0x31. For ASCII, digits 0 through 9, one can merely subtract the 0x30 for each digit. Other coding systems would require other methods of conversion. One has to keep track of the weight for each digit if the number has more than one digit. I would recommend you play around with it some, just as a learning exercise. Scanf performs the conversion for you on input. Investigate sscanf and sprintf for operations to memory. You might want to look up atoi and itoa, also. Not everything is standard. Check the docs.
|
the library function strtok can be used here if you want mess with it (it's really fuckin' weird).
you can set the '/' character as the delimiter and handle your stuff that way AS LONG AS you are positive that the input is being provided correctly (in the right format dd/mm/yy as you said). this should require error-checking for crap like: my birthdate is----> 666/5474N/r0x0rz there's lots of clever ways to implement this through a console, and i believe there's a <ctype> or <type> library that has functions to check user input. i may be totally wrong there, never used it. if you're working with a GUI, you may be able to limit user input through drop-down lists, radio buttons, etc. but strtok can be really useful in handling user input through a console and breaking it up just the way you want. using atoi and itoa like dawei said in conjunction with strtok can handle quite alot. |
Yes, atoi() and atof() can solve your problems.
Alternatively, you can do this by making a date class. It's private members will be three integers that represent the days, months and years. The public members can be functions that: --return a character array containing all these 3 elements --Change the values of each private member separately --Get each private value separately I think it will be easier with a class... |
| All times are GMT -5. The time now is 8:07 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC