![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
|
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? |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Programmer
|
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?
|
|
|
|
|
|
#4 |
|
Programming Guru
![]() ![]() ![]() |
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>
using namespace std;
int main (void)
{
struct date
{
int month;
int day;
int year;
};
date myDate;
myDate.month = 6;
myDate.day = 23;
myDate.year = 2006;
cout << "Original Date: " << myDate.month << "/" << myDate.day << "/" << myDate.year << endl;
myDate.day += 1;
cout << "New Date: " << myDate.month << "/" << myDate.day << "/" << myDate.year << endl;
char x;
cin >> x;
}
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#5 |
|
Programmer
|
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 |
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#7 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
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.
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
|
|
#8 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 239
Rep Power: 3
![]() |
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...
__________________
Project::Soulstorm (personal homepage) |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|