Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 23rd, 2006, 11:19 AM   #1
n00b
Programmer
 
Join Date: Sep 2005
Posts: 69
Rep Power: 3 n00b is on a distinguished road
Send a message via MSN to n00b
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?
n00b is offline   Reply With Quote
Old Jun 23rd, 2006, 11:26 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Jun 23rd, 2006, 11:36 AM   #3
n00b
Programmer
 
Join Date: Sep 2005
Posts: 69
Rep Power: 3 n00b is on a distinguished road
Send a message via MSN to n00b
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?
n00b is offline   Reply With Quote
Old Jun 23rd, 2006, 11:47 AM   #4
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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."
Infinite Recursion is offline   Reply With Quote
Old Jun 23rd, 2006, 12:04 PM   #5
n00b
Programmer
 
Join Date: Sep 2005
Posts: 69
Rep Power: 3 n00b is on a distinguished road
Send a message via MSN to n00b
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
n00b is offline   Reply With Quote
Old Jun 23rd, 2006, 12:31 PM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Jun 24th, 2006, 1:07 AM   #7
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
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.
bl00dninja is offline   Reply With Quote
Old Jun 24th, 2006, 3:44 AM   #8
Soulstorm
Hobbyist Programmer
 
Soulstorm's Avatar
 
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 239
Rep Power: 3 Soulstorm is on a distinguished road
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)
Soulstorm is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:37 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC