In the program below I need to get month to hold the first two digits of date, day two hold the digits 3 & 4, & year to hold the last four digits. Any idea on how I can accomplish that?

#include <iostream>
#include <string>
#include "functions.h"

int main()
{
    std::cout << "Please enter a number. ";
    int date = 0;
    std::cin >> date;
    int digits = getNrDigits( date );

    if ( digits == 8 )
    {
        std::cout << "Number of digits is good" << std::endl;

        int month = 1;
        int day = 2;
        int year = 3;
    }   
    else
    {
        std::cout << "Invalid number of digits" << std::endl;
    }
    return 0;
}

// FUNCTIONS

int getNrDigits( int my_int )
{
    int local_int = my_int;
    int count = 0;

    while ( local_int > 0 )
    {
        local_int /= 10;
        count++;
    }
    return count;
}

Use the / and % operators to perel off and isolate digits. For example, if x = 12345678, to isolate the 345, you would first divide by 1000 to get rid of the 678 and end up with 12345, then to get rid of the 12 part, you would mod (% operator) 12345 by 1000, like so.

int x = 12345678;
int y = ((x / 1000) % 1000); // y is now 345

Refine this process to isolate the desired digits.

As a side issue, have you tested the getNrDigits function thoroughly? Seems to me that 01172013 and 11172013 are both valid. Does getNrDigits accept both as valid?

As a side issue, have you tested the getNrDigits function thoroughly? Seems to me that 01172013 and 11172013 are both valid. Does getNrDigits accept both as valid?

As long as the numbers are recorded as int and not as string or array of chars, getNrDigits provides the correct answer. 01172013 as integer is held as 1172013, so, the number of digits is 7 and never as 8. If you really want to preserve the 0 at the beginning of an integer, you have to use string or array of chars as container. But, if the day is 7, one should use 07 in the integer, meaning 11072013.

But, Garrett85, VernonDozier has a point there: what if the month is before October? Your program should accept 7 digits as well.

To break your MMDDYYYY integer in three integers, here is an algorithm:

int year, month, day, date;     // with date read from console
year = date % 10000;            // YYYY
day = (date / 10000) % 100;     // DD or 0D transformed in D
month = date / 1000000;         // MM or M

I hope this is what you are looking for.

Edited 3 Years Ago by CGSMCMLXXV: just a simple inversion

As long as the numbers are recorded as int and not as string or array of chars, getNrDigits provides the correct answer.

cout << getNrDigits(0) << '\n';

VernonDozier, this program will be assuming that the user is entering a 0 if the month is before October. Thanks for the help everyone, I'll try and emplement some of your suggestions. Only now I need to change date to a char **arg variable so now I'M going to have to twik it even more. This program is to be a date validation program. I recently wrote the whole thing in bash and it worked. I thought it would be easier to simply move the program from a bash copy to a C++ copy, I was wrong!

Thanks it worked gusy. Now I will be receiving date as a command line argument (linux). I've never done that with c++ and from what I've been able to find via the internet I need to use int main( int arg, char **arg2 ) any idea on how I can get my date value into my variable date from the command line? Thanks again guys.

You have the program to something that uses a char* variable to hold the date string? If so, test whether arg equals 2. If not, give an error message and abort. Also test whether arg2[1] equals 8. Again, give an error and abort if not:

if (arg != 2 || strlen(arg2[1]) != 8)
{
    std::cout << "usage: ./exec_prog_name MMDDYYYY" << std::endl;
    return 0;
}

arg2[0] contains the name of the executable. You don't care about it. arg2[1] is a char* and now contains your 8 character date. Parse the month, day, and year from arg2[1] and continue with the program you have already have that works.

cout << getNrDigits(0) << '\n';

Ha! Good catch! If you want more:

cout << getNrDigits(-1) << '\n';

He wanted to count the digits for strictly positive integers. If you want a more generalized code, then the function should look like:

int getNrDigits( int my_int )
{
    int local_int = my_int;
    int count = 0;

    if ( my_int == 0 ) return 1;

    while ( local_int != 0 )
    {
        local_int /= 10;
        count++;
    };

    return count;
}

As I said in my original post, that was something he could start with. ;)

About passing MMDDYYYY as argument, one needs to convert char* to int, providing a guard for char* content before conversion.

Thanks gusy but I am getting these two errors. Any idea?

DateValidation.cpp:8:24: error: invalid conversion from ‘char*’ to ‘int’ [-fpermissive]
DateValidation.cpp:9:34: error: ‘strlen’ was not declared in this scope

Here's the program.

#include <iostream>
#include <string>
#include "functions.h"

int main( int argOne, char **ArgList )
{
    //std::cout << ArgList[1] << std::endl;
    int number = ArgList[1];
    int digits = strlen( ArgList[1] );
    std::cout << digits << std::endl;

/*  if ( digits == 8 )
    {
        std::cout << "Number of digits is good" << std::endl;

        int month = 1;
        int day = 2;
        int year = 3;
    }   
    else
    {
        std::cout << "Invalid number of digits" << std::endl;
    }*/
    return 0;
}

// FUNCTIONS

int getNrDigits( int my_int )
{
    int local_int = my_int;
    int count = 0;

    while ( local_int > 0 )
    {
        local_int /= 10;
        count++;
    }
    return count;
}

Thanks guys, you've been a big help. Got this small part of the program working now.

This question has already been answered. Start a new discussion instead.