ok so I have what seems like a simple problem...

simple code

int date;

cout << "Enter the date in MMDDYYYY format: ";
cin >> date;

problem happens when someone starts the int with a 0

for example cin >> 03081991 date = 24

if I add another object after date it gets the 81991...

I was wondering if there's a simple way to fix it, I know the problem is that the int starts with 0 causing the compiler to put it in... is it base 8? BUt is there a way to just tell the compiler to not do that? or a way to ignore the 0 without ignoring it if it's a 1?

I know you guys have a policy against fully writing code for people but I assure you this is <= 1% of the project/

What you do is use strings :

string data;
 cout << "Enter the date in MMDDYYYY format: ";
 cin >> date;
 asserCorrectFormat(data); //make sure valid format
 doStuff(data);

What you do is use strings :

string data;
 cout << "Enter the date in MMDDYYYY format: ";
 cin >> date;
 asserCorrectFormat(data); //make sure valid format
 doStuff(data);

I need to use an int for another part of the program.

Another part of the program uses / and % to format it and another part subtracts it from the current date to get age.

Plus the date is going to be part of a constructor so it need to be the same data type.


probably should have said it needs to stay an int.

int is a very poor choice for a data representation of Date. The right thing would certainly be to accept the user input as string -- as is and then parse it according to your needs.
I would even go as to creating a struct for holding three short int values.
Then I can always make a function parse_date(std::string date) which can parse the entered string and return me the struct.

listen guys, I realize that int is a poor data type choice, but the data type is required for the assignment.

If I were doing it I would have used a struct(since it works so well with time), but the prof wants it as an int.

SO can anyone help me with the problem I asked to solve and not just a way of avoiding it? or is the answer I'm looking for non-existent?

Or can someone at least point me to a site or something that might have the answer? I checked cplusplus and didn't find it, but maybe I was searching wrong, same with google, but again probably used the wrong search terms, and again searched these archives but probably used the wrong jargon.

If you could give me the correct jargon for my problem I'll try searching again... sorry for any rudeness on my part I should have put more info in the OP.

Member Avatar for iamthwee

listen guys, I realize that int is a poor data type choice, but the data type is required for the assignment.

If I were doing it I would have used a struct but the prof wants it as an int.

SO can anyone help me with the problem I asked to solve and not just a way of avoiding it? or is the answer I'm looking for non-existent?

Or can someone at least point me to a site or something that might have the answer? I checked cplusplus and didn't find it, but maybe I was searching wrong, same with google, but again probably used the wrong search terms, and again searched these archives but probably used the wrong jargon.

If you could give me the correct jargon for my problem I'll try searching again... sorry for any rudeness on my part I should have put more info in the OP.

I don't think this is doable, or if it is, it would be incredibly convoluted. A std::string or char array[] would be the datatype of choice here.

Go speak with your professor . . .

I mean what happens if the user types in something completely wrong, like letters of the alphabet. Garbage in = garbage out. . . However, that being said, it's entirely possible your professor is marking the assignment based on the assumption that the input is ALWAYS good?? That's the only logical explanation.

Agreed with iamthwee.
There is an intrinsic problem in using a single int as data type for Date.
You can sure go along and use int but then whenever you be needing to extract the individual elements from it (the month, day and year) you must always check if the integer is 8-digit long or 7 digit long and then do your parsing.
Note that throughout C, there is no distinction between 0123 and 123. It is same.

OK, that you.

The prof is assuming that the input will always be numbers and will always be in the correct format. We're not garbage proofing.

She gave a hint that there was a way to use % to solve this, but I can't directly use it in the istream (date%100000000) and by the time it is able to be manipulated in the program the damage has been done.

One of the other parts of the project was using division and modulus to format it:

month = date / 1000000;
day = (date % 1000000)/10000;
year = date % 10000;

Another was subtracting the inputed date from the current date to get the age of something.

These both work fine so long as the int didn't start with 0, but when the int starts with 0 it has the problem where it breaks into 2 objects, the first being what looks like a base 8 number while the other it's whatever cuts off after the first digit that is 8 or higher.

So yeah, it's assumed that date will always be a 7 to 8 digit positive int. I'm guess that when my partner and I had asked if we had to make a way to ignore the 0 inputed by MMDDYYYY she misheard.

So, there is no simple way to do this while keeping it an int?

Would there possibly be a way to convert it to an int from a string without re-encountering the conversion problem in my C++ compiler?

Member Avatar for iamthwee

OK, that you.

The prof is assuming that the input will always be numbers and will always be in the correct format. We're not garbage proofing it.

She gave a hit that there was a way to use % to solve this, but I can't directly use it in the istream (date%100000000) and by the time it is able to be manipulated in the program the damage has been done.

One of the other parts of the project was using division and modulus to format it:

month = date / 1000000;
day = (date % 1000000)/10000;
year = date % 10000;

Another was subtracting the inputed date from the current date to get the age of something.

these both work fine so long as the int didn't start with 0, but when the int starts with 0 it has the problem where it breaks into 2 objects, the first being what looks like a base 8 number while the other it's whatever cuts off after the first digit that is 8 or higher.

So yeah, it's assumed that date will always be a 7 to 8 digit positive int. I'm guess that when my partner and I had asked if we had to make a way to ignore the 0 inputed by MMDDYYYY she misheard.

So, there is no simple way to do this while keeping it an int?

Would there possibly be a way to convert it to an int from a string without re-encountering the conversion problem in my C++ compiler?

Young man, you're still trying to look to solve a problem that we are now assuming doesn't exist.

It's like saying:-

" I've built a bike, it's almost ready, but I want to put stabilizers on it . . .just in case the riders can't ride the bike and may fall off. Then it will be perfect!!"

But . . . wait. . . what happens if the riders don't have any legs??

Assume the riders of your bike, can ride like bmx champs . . . Get it??

I don't understand your problem, the code works fine:

#include <iostream>
int main()
{
    using namespace std;
    int month,day,year,date;
    cin>>date;
    month = date / 1000000;
    day = (date % 1000000)/10000;
    year = date % 10000;

    cout<<month<<" "<<day<<" "<<year;
}

I don't understand your problem, the code works fine:

#include <iostream>
int main()
{
    using namespace std;
    int month,day,year,date;
    cin>>date;
    month = date / 1000000;
    day = (date % 1000000)/10000;
    year = date % 10000;

    cout<<month<<" "<<day<<" "<<year;
}

I know that that code works fine, the only problem was when the input started with a 0 as in:

03081991

did you input something like that?

date would be assigned 24 and the next cin would be assigned 81991

ok, I'm just going to assume the prof misheard our question and that there is no simple way to fix this problem( or even use a wacky work around) without switching the data type.

thank you all, sorry for wasting your time.

Yes, I tried that, it works fine.
Here is the code that I used:

#include <iostream>
using namespace std;
int main()
{
    int month,day,year,date;
    cout<<"Enter a date:";
    cin>>date;
    month = date / 1000000;
    day = (date % 1000000)/10000;
    year = date % 10000;

    cout<<"Date Entered:"<<date<<endl
    <<"Month:"<<month<<endl
    <<"Day:"<<day<<endl
    <<"Year:"<<year<<endl;
}

And this was the interaction at the terminal:

siddhant3s@x10n:~$ ./test
Enter a date:03081991
Date Entered:3081991
Month:3
Day:8
Year:1991
siddhant3s@x10n:~$

Yes, I tried that, it works fine.
Here is the code that I used:

#include <iostream>
using namespace std;
int main()
{
    int month,day,year,date;
    cout<<"Enter a date:";
    cin>>date;
    month = date / 1000000;
    day = (date % 1000000)/10000;
    year = date % 10000;

    cout<<"Date Entered:"<<date<<endl
    <<"Month:"<<month<<endl
    <<"Day:"<<day<<endl
    <<"Year:"<<year<<endl;
}

And this was the interaction at the terminal:

siddhant3s@x10n:~$ ./test
Enter a date:03081991
Date Entered:3081991
Month:3
Day:8
Year:1991
siddhant3s@x10n:~$

OK then the problem is with my compiler? Heh... figures...

using microsoft visual studio 5.0.... remade for use in... 1997 -.-
Wish LCCC would update this... or let use open source.


Anyway, ok sorry for all this trouble, thank you for your patience

Member Avatar for iamthwee

OK then the problem is with my compiler? Heh... figures...

using microsoft visual studio 5.0.... remade for use in... 1997 -.-
Wish LCCC would update this... or let use open source.


Anyway, ok sorry for all this trouble, thank you for your patience

Yeah, either way, you're at school so it doesn't matter for the purpose of your assignment.

Assume the user doesn't enter dates with any leading zeros.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.